Efficient Rotating Daily backups with rsync

Systems administrators typically use the date command in combination with cp -R to provide backup functionality. In a cronjob, the command


cp -R /path/to/backup /backup/destination/backup-`date +%u`

So what will this do? It will create seven distinct directories, one for each day of the week (1 through 7). When overwriting your old backups, you can either delete them first, or set cp to overwrite them. Neither is really optional.

Instead, if your backups don't change a lot, you can use rsync to help speed things up a bit. rsync is an open-source utility that provides fast incremental file transfer.

Instead of using cp, use rsync with two additional parameters: -av and --delete.


rsync -av --delete /path/to/backup /backup/destination/backup-`date +%u`

This has the advantage of needing to copy fewer files, since only the changes are copied over. Your backup from last week is likely to have 90% of files in common with the latest backup: so why waste time deleting the files which are the same.

Where can you go with this? Well, you can set up rsync to transfer files to a remote system without needing passwords. This is incredibly useful for doing your rotating backups to a remote system.

This is not a substitute for a long-term backup strategy. I also wouldn't recommend it for log files or similar groups of rapidly-rotated files. It is a fairly efficient solution for short-term backup needs, and provides a useful and quick way to have an offsite copy of your backup for disaster recovery or those oops moments. It's also useful for the files of CMS-powered sites, as user-submitted content tends only to accumulate.

I use this for backing up a

I use this for backing up a website I help admin.

We do a daily rotation, a weekly rotation, a monthly rotation and a yearly rotation.

While not completely impervious to fault - it effectively gives us 2 complete remote copies of the data. In the event of a page failing we can restore a copy from a previous day. In the event of a page being deleted and not noticed we have a copy of the 4 previous week images, the 12 previous month images, or the 4 previous year images. Overkill most likely, but as you pointed out - it only keeps one copy of each file/state.

Great script.

I suggest you look at

I suggest you look at duplicity rather than just rsync. It also allows for incremental backups, as well as gpg signing and encrypting.

rernst's picture

I didn't mean to imply that

I didn't mean to imply that this was the *best* way to do backups, nor that it was a good long-term solution. Duplicity is a good system. However, using rsync in this simple way does have the advantage of being extremely nontechnical-friendly. I mean 'nontechnical' in 'those people who can barely operate an FTP client'.

Using rsync in this manner produces plain vanilla copies of the files that don't require knowing how to unpack a tar archive and apply a diff in order to access them.

rsync seem great, I just

rsync seem great, I just hope it can support more smart setting, just like some windows program, such as east-tec backup, more easy to create tasks (support script ie better) to automatically make backups of all important files and program settings.

Post new comment

All comment submissions must follow the Comment Policy. Your words remain your own and you are responsible for them. If you don't like the captcha, Login to a user account. You can login with OpenID too..
The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <embed> <blockquote> <p> <iframe> <div> <span> <tt>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
G
D
d
i
Y
1
t
Enter the code without spaces and pay attention to upper/lower case.

ryanernst.com RSS feed

Powered by Drupal, an open source content management system

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer