Skip navigation.

Simple Backup in Linux Using Rsync and SSH

Backup to a spare disk
Assume your home directory is /home/foo, the directory for backup is /backup

1. Create backup script: /home/foo/cron/local-backup.sh
#!/bin/sh
LPATH=/home/foo
RPATH=/backup
LOGFILE=/home/log/backup
rsync -av --delete -R $LPATH $RPATH >> $LOGFILE


2. "crontab -e" to create the cron job: perform the backup at 5AM everyday
# m h  dom mon dow   command
00 05 * * * /home/foo/cron/local-backup.sh


Backup to another machine
Assume your have two machines: "HostA" and "HostB".
User name in HostA is "foo" and username in HostB is "bar".
You want to backup files in HostA to HostB

1.Create SSH key in HostB
bar@HostB: ssh-keygen -t dsa -b 1024 -f /home/bar/cron/HostB-rsync-key

Generating public/private dsa key pair.
Enter passphrase (empty for no passphrase): [press enter here]
Enter same passphrase again: [press enter here]
Your identification has been saved in /home/bar/cron/HostB-rsync-key.
Your public key has been saved in /home/bar/cron/HostB-rsync-key.pub.
The key fingerprint is:
2e:28:d9:ec:85:21:e7:ff:73:df:2e:07:78:f0:d0:a0 bar@HostB

bar@HostB: chmod 600 /home/bar/cron/HostB-rsync-key 


2. Upload public key to HostA
bar@HostB: scp /home/bar/cron/HostB-rsync-key.pub foo@HostA:/home/foo/.ssh/


3. Save key in HostA
foo@HostA: cat /home/foo/.ssh/HostB-rsync-key.pub 
            >> /home/foo/.ssh/authorized_keys


4. Create backup script in HostB: /home/bar/cron/rsync-HostA-backups.sh
RSYNC=/usr/bin/rsync
SSH=/usr/bin/ssh
KEY=/home/bar/cron/HostB-rsync-key
RUSER=foo
RHOST=HostA
RPATH=/home/bar
LPATH=/backup
LOGFILE=/home/foo/log/backup

$RSYNC -az -e "$SSH -i $KEY" $RUSER@$RHOST:$RPATH $LPATH >> $LOGFILE


5. In HostB, "crontab -e" to create a cron job: perform the backup at 5AM everyday
# m h  dom mon dow   command
00 05 * * * /home/bar/cron/rsync-HostA-backups.sh


Links
rsync web page
Using Rsync and SSH
Cron Tutorial
Newbie:Intro to cron

AIGLX + Beryl in Debian (NVDIA drivers)Gaming in Linux

Comments

Anonymous 27. April 2009, 09:22

Anonymous writes:

Hello
RPATH isn't RPATH=/home/foo and in log file is /home/bar ? I mean if you run the script on machine B ?

Regards
http://www.nicewallpapers.info

How to use Quote function:

  1. Select some text
  2. Click on the Quote link

Write a comment

Comment
(BBcode and HTML is turned off for anonymous user comments.)

Type the two words displayed in the image below:


Smilies