De Help Desk punt NL
De Helpdesk > De Helpdesk > Kennisbank

Backups using Rsync, Bash & Cron

Oplossing 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 -av bit: 'a' means archive, or copy everything recursively preserving things like permissions, ownership and time stamps. The 'v' is verbose, so it tells you what its doing, either in the terminal, or in this case, in the log file. --progress gives you more specific info about progress.

--delete checks for changes between source and destination, and deletes any files at the destination that you've deleted at the source. --log-file saves a copy of the rsync result to a date-stamped file on my desktop.

--exclude leaves out any files or directories you don't want copied. In my case, the .gvfs directory in Hardy Heron was a pain as even with sudo it errored and wouldn't copy properly, so I excluded it (Its not necessary to copy it anyway) If you don't use Hardy yet, or any distro using the latest Gnome, skip this line, or upgrade!

/home is the directory I want copied. /home copies the directory and its contents, /home/ would just copy the contents

/media/HomeBackup is the separate drive. Change this to whatever your backup location is. You can actually have this drive off-site and use ssh, but that will be a tutorial for another day!

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:

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 on your Desktop and make it executable by typing
sudo chmod +x /home/your-username/Desktop/

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/ /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/

What this all means is:

1. The number of minutes after the hour (0 to 59)
2. The hour in military time (24 hour) format (0 to 23)
3. The day of the month (1 to 31)
4. The month (1 to 12)
5. The day of the week(0 or 7 is Sun, or use name)
6. 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!



If from my cygwin prompt I execute:
rsync -avx folder user@host:folder
I don't have any problems, however as soon as I put this into a bash as described above I get the following:
building file list ... done
rsync: mkdir "folder/#015"
failed: No such file or directory (2)
rsync error: error in file IO (code 11) at
/home/lapo/packaging/tmp/rsync-2.6.9/main.c(529) [receiver= 2.6.9]
rsync: connection unexpectedly closed (8 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at
/home/lapo/packaging/tmp/rsync-2.6.9/io.c(453) [sender=2.6.9]
./rsync_win: line 5:

There is no such folder in my source folder?

Any help would be appreciated.


to run this as root i think you should:

create the script but do NOT start the line in it with the "sudo"

save the scrip and change the owner to > chown root:root

then create the cron job by using:

sudo crontab -eu root

This will create cronjob for user root=the scrip will have to be owend by root to be able for the root to execute it

and if root si going to execute the scrip there should not be need to start the line in the script with the "sudo".
Was dit artikel bruikbaar? ja / nee
Gerelateerde artikelen Rsync server1 naar server2
Rsync SSH inlog zonder wachtwoord in te geven Fedora of Centos
Nagios installeren op CentOS 7
Wachtwoord MariaDB en Mysql herstellen
Commands to use a tape device
Linux: Logitech G15 with Ubuntu 9.04 Jaunty
Amanda - Creating Successful Backups
Postfix fout send mail root@localhost
Scan CentOS for Malware, Viruses, and Rootkits
Mount USB drive op de Raspberry PI
Artikel details
Artikel ID: 60
Categorie: Backup
Datum toegevoegd: 16-Dec-2009 19:00:03
Aantal bekeken: 1297
Beoordeling (Stemmen): Artikel beoordeeld 3.1/5.0 (325)

« Ga terug