Lgo.png

 Going Linux

   for computer users who just want to use Linux to get things done


Automating backups using rsync, bash and cron

Updated: 17-May-2008

A listener to the Going Linux podcast, Daniel, has contributed this article for us to publish. Originally published as a tutorial on the Linux Basement site, Daniel thought that our listeners and readers might find it valuable as well, since we discussed Linux backup applications in Episodes 29 and 31.

Rsync is a command line program for Linux that performs a backups. Bash is a scripting language for Linux that allows you to automate the commands you run in the command line. And Cron is a Linux program that schedules programs to run, based on time. You can see the potential for these three programs to work together to provide automated backups!

Thanks to Daniel for providing this tutorial...


Backups

I wanted to backup my home directory using rsync to a separate drive, and to make it happen automatically.

I spent ages doing research into the various commands, and found everything I needed to know, but not all in the same place. While it was fun for me, others may just want to know how to do it immediately. So here goes!

The rsync command
sudo rsync -av --progress --delete --log-file=/home/your-username/Desktop/$(date +%Y%m%d)_rsync.log --exclude "/home/your-username/.gvfs" /home /media/HomeBackup

The details of the command
The bash script

I was just pasting this command into Terminal each day, but wanted something automatic, so step one was a bash script. Very easy, just open a new document in your favourite text editor, and type #!bin/bash followed by the command itself on a new line. So:

#!/bin/bash
sudo rsync -av --progress --delete --log-file=/home/your-username/Desktop/$(date +%Y%m%d)_rsync.log --exclude "/home/your-username/.gvfs" /home /media/HomeBackup

Save that as rsync-shell.sh on your Desktop and make it executable by typing:
sudo chmod +x /home/your-username/Desktop/rsync-shell.sh
or by right-clicking the file, select Properties, Permissions and then checking the Execute box.

You can now double click that .sh file, choose Run in Terminal, it will ask you for your password and run, then leave a log file on your desktop. Or, you can make a cron job to do it for you!


The cron job

My biggest obstacle with this was the sudo bit. rsync won't be able to backup all files, or delete any, without root privileges. I didn't want to have to be there when it runs to type in my password, but after a bit of searching I found out how to make a root cron job.

Copy your .sh file to /root by typing:
sudo cp /home/your-username/Desktop/rsync-shell.sh /root

Then type:
sudo crontab -e

You'll see a line which reads: # m h dom mon dow command

Under that, type:
0 22 * * * /root/rsync-shell.sh

What this all means is:

  1. The hour in military time (24 hour) format (0 to 23)
  2. The day of the month (1 to 31)
  3. The month (1 to 12)
  4. The day of the week(0 or 7 is Sun, or use name)
  5. The command to run

So at 22:00 (10pm) every day root will run the shell script, without prompting you for sudo password (because its running as root already).

Now press Control-X, then type "Y", then press Enter.

You'll see: crontab: installing new crontab

And you're done!

"I hope this helps!" (I borrowed this line from Verbal, but I am sure he's made it Creative Commons!)


Site Created with theMaker for Linux

Theme music for the Going Linux podcast is generously provided by Mark Blasco. http://www.podcastthemes.com
Creative Commons License Going Linux Podcast by Larry Bushey is licensed under a Creative Commons Attribution 4.0 International License.