Backup Script for Recent Files
I decided to write a script to backup only the recent files. There are solutions based on unison that work periodically for all files, but as I’m changing projects, I need to configure new backups for these projects as well. This is cumbersome and error prone, it’s easy to forget to add new artifacts to backup scripts and lose them in a state of emergency.
Therefore I decided that a small bash script that works with rsync and find works better. It should monitor all of my home directory and backup the recent files.
The following script does that.
#!/bin/bash
if [ "x$1" = "x" ] ; then
TARGET=/media/augustus/backup-recent-`hostname`
else
TARGET=$1
fi
PERIOD=15
mkdir -p $TARGET
#delete files older than $PERIOD days
find $TARGET -ctime +$PERIOD -print -delete
#copy files newer than $PERIOD under ~. Ignore files under .hg
for d in ~/*/ ; do
find $d -path '*/.hg/*' -prune -o -type f -ctime -$PERIOD -print -exec rsync -aRv {} $TARGET/ \;
done
It checks whether a command line option is given as the target, otherwise sets the target itself. /media/augustus/
is an NFS mount but you should be able to put anything there.
PERIOD
is the number of days that the script considers recent. Currently it backups files changed in the last 15 days.
It deletes the older files from backup. As there are other solutions for older files, I don’t want to keep them here as this script will run from cron
every 2 hours.
The script only checks the directories under home dir, so files that are immediately in the home dir are not backed up.
It doesn’t copy files under .hg
directories. You should be able to put many -prune
options like the example to prevent irrelevant files away from the backup.