Ivor O’Connor

July 25, 2009

Automatic Backup With Rsync And Cron On Ubuntu

Filed under: BACKUP, CRON, mint 14 xfce, rsync, SSH, ubuntu — Tags: , , , , — ioconnor @ 6:28 pm

Keeping a directory automatically backed up somewhere is always useful. Every few months this is needed on one machine or another. Here are the steps:

  1. First set up ssh keys so passwords are no longer needed.
    1. Test things by verifying a ‘ssh user@yourserver.com’ does require a password.
    2. Make some ssh keys on your client and then move the public key to the server
      1. cd ~/.ssh
      2. ls
      3. ssh-keygen -t dsa (Keep the defaults by just pressing enter a few times.)
      4. ssh-copy-id -i ~/.ssh/id_dsa.pub user@yourserver.com
    3. Test things by verifying a ‘ssh user@yourserver.com’ no longer requires a password.
  2. Determine the directory to be backed up and where it will be backed up and test the command to be used.
    1. time rsync -atvz ~/local-directory-of-your-choosing/ user@youserver.com:remote-directory-of-your-choosing/
    2. log in the server and verify the files are there…
  3. Put the command in cron. Because cron is broken in ubuntu you can’t simply do “crontab -e” anylonger. (How can something so basic be broken?!) Instead follow these instructions:
    1. vi ~/some_file and put in the cron commands
    2. @hourly rsync -atvz ~/local-directory-of-your-choosing/ user@youserver.com:remote-directory-of-your-choosing/
    3. crontab ~/some_file
    4. crontab -l

Now simply test it out…

UPDATED ON 2013-02-02: Used “ssh-copy-id -i ~/.ssh/id_dsa.pub user@yourserver.com” to reduce 10 or so steps and make things vastly simpler.
UPDATED ON 2013-02-05: I came across this article today and realized I should probably update this to use the “–delete” and “-e ssh” commands. I am already a big user of the delete command but I don’t often use ssh because my backups are local for the most part.



  1. Awesome guide, thanks! Worked perfectly.

    Comment by Ian — August 4, 2009 @ 7:26 pm

  2. […] You probably need to set up your ssh keys first as described in a past post here. […]

    Pingback by My GIT Tutorial For Ubuntu: Part 2 « Ivor O’Connor — August 11, 2009 @ 7:09 pm

  3. Awesome. Works like a charm. I appreciate it!

    Comment by GRL — November 15, 2009 @ 6:46 pm

  4. nice tuto it was very useful!

    Comment by Lennin Padilla — December 10, 2009 @ 6:55 pm

    • I’m pleased people find this tutorial useful. However I’m surprised people don’t find the GIT tutorials more useful. Rsync with cron tends to get out of hand. Over time you create multiple files that you trash. The rsync’d backup directory keeps all those trashed files and this often becomes a nuisance. GIT automatically removes these copies. Furthermore it keeps all the revisions. I use GIT to keep all my Ubuntu machines totally in sync with each other. Maybe I should work on a clear and easy write up making it simple for others to do the same…

      Comment by ioconnor — December 10, 2009 @ 7:41 pm

  5. I searched for ages attempting to do this.. this post really helped alot.

    I’m on Ubuntu 10.10

    Comment by klight2003 — March 26, 2011 @ 10:03 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: