Quick backup

Preston Hagar prestonh at gmail.com
Mon Sep 17 14:53:41 UTC 2012

On Sun, Sep 16, 2012 at 2:11 PM, Johnny Rosenberg
<gurus.knugum at gmail.com> wrote:
> I wrote a backup Bash-script many years ago. I could just run a script that reads that
> file and copy only those files to their corresponding places on that
> USB drive (reminding me to turn it on if I didn't - something that my
> present backup script already does).
> Kind regards
> Johnny Rosenberg
> ジョニー・ローゼンバーグ

Below is a basic version the script I use both personally and for
several servers for backups.  It uses cp -al which creates hard links
to do daily snapshots and rsync to only copy changed files.  What hard
links give you is that if a file is unchanged, it doesn't create a
second copy.  That means each daily snapshot only takes up the space
(roughly) of the day's changes.

rsync will only copy files that have changed on the source to the
destination, so it sounds pretty much exactly like what you want,
without the log file hacking.

(Side Note:  I'm pretty sure rsync has hard linking capabilities in it
now.  I have had this script around so long, it didn't when I first
wrote it.  It works great as is, but you likely could just do the
whole thing with rsync)


# Daily backup script for Preston

exec > /var/log/backup.log

echo "*****Backup Started: " `date`

# this is the mount point for your USB drive
# here is where you would want to put mount checking code.  I
currently don't have that part written.


#> Rotate snapshots
echo "Rotating snapshots"
cd $backup_dir/

# I do 7 days worth of snapshots, just add more mv commands and up the
rm command to get more snapshots
rm -rf 7
mv 6 7
mv 5 6
mv 4 5
mv 3 4
mv 2 3
mv 1 2

# Copy the current to the snapshot head
# this will create hard links and from then on, only take up disk
space for things that are different files
# the touch command will set the backup directory's modified date (but
not the files inside) to today, so
# when you do a ls, the date next to the directory (1,2,3,etc.) will
be the date it was created.

cp -al $backup_dir/current $backup_dir/1
touch $backup_dir/current
touch -r $backup_dir/current $backup_dir/1

#> Backup to current
# Here is where I use rsync to backup the directories I want to my backup drive.
# If you don't have too many files, you could likely do this in one big
# rsync command (rsync /var/data/samba for example) but I have found
# that rsync can sometimes overload a system or choke when the file
list is too large, so I tend to
# split it up into several commands if possible and reasonable.

echo "Archiving to backup folder"
rsync -av --delete /var/data/samba/backups $backup_dir/current/
rsync -av --delete /var/data/samba/files $backup_dir/current/
rsync -av --delete /var/data/samba/Pictures $backup_dir/current/
rsync -av --delete /var/data/samba/digital\ scrapbook $backup_dir/current/
rsync -av --delete /var/data/samba/scans $backup_dir/current/

echo "*****Backup Completed: " `date`

# end of script

Hope that helps out.

More information about the ubuntu-users mailing list