So,
One of the things I was wondering about, and had never really done in linux, was backing up my files. Working as a Programmer and Web Developer, I have issues arising all of the time with bits of code that have gone missing as a result of accidents. I decided I needed a better backup system, and what better way than to make one which simply backs everything up without your input!
I decided that I would back my sourcecode files “/home/ajc/src” and my documents “/home/ajc/doc” every week. That way a loss could not be too catastrophic.
The tool which I chose to use was rsync. Many modern distributions come with this built in (I use ubuntu) and if they don’t this will certainly be in the repositories.
First things first. Set up the destination folder for your files:
#mkdir /media/vince/data_Backup in my case.
Once that is done you are ready to learn rsync.
The command which I use for my backups is as follows (it has a few extra options which I will explain)
#sudo rsync -av --progress --delete --log-file=/home/ajc/doc/$(date +%Y%m%d)_rsync.log /home/ajc/src /media/vince/data_Backup
As you can see this line of code deals with my source folder. The options are: “-a” for archive “v” means verbose so you will see detailed output “–progress” gives a progress update in terminal “–delete” will delete any items in the destination which you have removed from the source “–log-file” specifies the location for a log file to be kept. Other than that for those of you unfamiliar with bash script “$(date +%Y%m%d)” is used to inject the current date. So the filename for today would be 20080515_rsync.log
Go ahead and change the source file and destination file (last two sections of the command) and run it on your system. You will see that for a reasonable amount of data this is a quick process!
Now you know the command thats all well and good, it means that you can backup things when you want. However, we all know that this is easy to forget! So how about scheduling this task through a cron job?
You must begin by converting this command into a bash script. This is easier than it may sound. Just create the following (with your paths) in a text editor and save it to your home directory.
#! /bin/bash
sudo rsync -av –progress –delete –log-file=/home/ajc/doc/$(date +%Y%m%d)_rsync.log /home/ajc/src /media/vince/data_Backup
Next we must make the file executable
#sudo chmod +x /home/ajc/rsync-demo.sh
And then you have a file which you can click to run. However, we want automation, so here comes the cron bit
move the file to the root directory. (So that cron doesn’t ask you for a password when it runs)
#sudo mv /home/ajc/rsync-demo.sh /root/rsync-demo.sh
Then we want to open the crontab so we can schedule a job
#sudo crontab -e
here you should have several headings: m (minute of the hour) h (hour) dom (date of month) mon (month) dow (day of week) command. Now as you can see from this we have quite a wide range of choice as to when this script will be run. I chose 10pm every friday as the one I wanted. So the sequence was:
0 22 * * 5 /root/rsync-demo.sh
So 0 minutes, 22 hours, any date of the month, any month of the year, day 5 (friday) and the path to my script.
Simple. Now crontab will have opened in your default text editor so issue the command to save and exit (mines nano so Ctl + O then Ctl + X) and you will see a line confirming that the job has been added.
Done. Next time I will show you how to automate a backup to dvd!