2

I want to backup a particular file in another folder every time the file changed. Ideally, the backup-file should have a timestamp when backed. For example:

Directory workfile

~/workfile

Directory backupfile

~/backup/workfile-2014-11-24-10:30
~/backup/workfile-2014-11-24-11:10
~/backup/workfile-2014-11-24-12:40
~/backup/workfile-2014-11-24-15:42
~/backup/workfile-2014-11-24-16:30

I would like to have a solution without using the GUI.

sourav c.
  • 44,715

3 Answers3

0

Im not entirely sure of what the use case is here. But instead of having "backups" made of files according to a specific time-stamp isn't it better just to store changes between versions?!

If you'r files are non binary, like text or source code using git

if they are binary i would use rsnapshot, as it will automatically create a parallel folder structure with versions of each file but only keeping a copy of files that actually have changes in them (saving space)

SAVING it in a folder according to the time of the snapshot. for example day2/hour3

storing the date in the filename itself seems redundant. rsnapshot keeps that info both through the folder structure it creates for backups and you can also see it on the date of the file itself.

GIT will show you the commit date for each change it has commited for each file.

tomodachi
  • 14,832
0

Not tested, but I would approach the problem in this way:

The script:

#!/bin/bash
BACKUP_LOCATION='backup'
FILE_TO_MONITOR='workfile'
PREVIOUS_MD5='000'

if [ ! -d "$BACKUP_LOCATION" ]
then
    mkdir -p "$BACKUP_LOCATION"
    echo "Init backup dir..."
fi

while true
do
MY_TIMESTAMP=`date +"%Y-%m-%d-%H:%M:%S"`
MY_MD5=`md5sum "$FILE_TO_MONITOR"`
if [ "$MY_MD5" != "$PREVIOUS_MD5" ]
    then
    cp -f "$FILE_TO_MONITOR" "${BACKUP_LOCATION}/${FILE_TO_MONITOR}-${MY_TIMESTAMP}"
    PREVIOUS_MD5=$MY_MD5
    echo "File changed at $MY_TIMESTAMP."
    fi
done

The procedure: run this script as daemon. The output you can redirect it to a log file even.

Frantique
  • 8,493
0

In my opinion corn job is better alternative than any infinite loop. I am assuming the ~/backup folder already exists. One can set a corn job to run the following script at an interval of n minutes depending on their need. It will serve the purpose.

Run the script at an interval of 5 minutes

open user's crontab (cron configuration file) from a terminal as, crontab -e. Add the following line to run the script at an interval of 5 minutes,

*/5 * * * * /path/to/script

Don't forget to give the script the execution permission : chmod u+x /path/to/script

The script

#!/bin/bash
msrc="/home/$USER"
mdst="/home/$USER/backup"
msrcfile="workfile"
mnewfile="$msrcfile"-$(date +"%Y-%m-%d-%H:%M")
checkfile="$(for i in "$mdst"/*; do echo "$i"; done | xargs ls -1t > /dev/null 2>&1 | head -1)"

if [[ "$msrc"/"$msrcfile" -nt "$checkfile" ]]; then cp "$msrc"/"$msrcfile" "$mdst"/"$mnewfile" fi

From man bash:

CONDITIONAL EXPRESSIONS
file1 -nt file2
          True if file1 is newer (according to modification date) than file2, or if  file1
          exists and file2 does not.

From man ls:

-t     sort by modification time, newest first
sourav c.
  • 44,715