Offsite Snapshot Backups using rsync

Creating simple backup snapshots using rsync and mv

Summary

Create the illusion of mutliple backed up copies, but in reality only the changes are different thereby saving space on the HDD. Create a cron job to run the following script once a day. The script will keep 4 days of backups, edit the numbers to your liking. Utilizing some built in rsync features it will only store 1 copy of the file on disk unless it has changed.

The script

 
#!/bin/bash
rm -rf backup.3
mv /backup/backup.2 /backup/backup.3
mv /backup/backup.1 /backup/backup.2
mv /backup/backup.0 /backup/backup.1
rsync -a --delete --link-dest=../backup.1 -e "ssh -p 75" root@192.168.1.50:/home/ /backup/backup.0/

The last line is the most important. The format is

 rsync -a --delete --link-dest=../backup.1 <source directory> <destination>

If it was on the same host then it would be something like

 rsync -a --delete --link-dest=../backup.1 /home/ /backup/backup.0/

The link-dest argument states to just create hard links for files that have not changed.

Specifying directories to be included

You could get more fancy with what directories you want to backup. You can tell the script to sync different directories, here is where rsync gets a little funny. Here is the new line with the include directive.
rsync -a --delete --link-dest=../backup.1 -e "ssh -p 75" root@192.168.1.50:/ /backup/backup.0/ --include-from=include.txt

Then in the same directory as the script you would place a file include.txt

+ /etc/
+ /etc/<strong>
+ /root/
+ /root/</strong>
+ /home/
+ /home/<strong>
+ /var/
+ /var/www/
+ /var/www/</strong>
+ /var/lib/
+ /var/lib/<strong>
+ /backup/
+ /backup/</strong>
- *

Notice how for each directory there are two lines, and at the end, there is a exclude statement.

References

http://www.mikerubel.org/computers/rsync_snapshots/

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <drupal5>, <drupal6>, <javascript>, <php>, <sql>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options