Automatic backup from Ubuntu Server with Rsync

Scenario:

2 Ubuntu Server: 1 is named as tendo-Srv with an ip address of 192.168.1.190, from which we want to take backup and 1 is named as tendo-backup with an ip address of 192.168.1.177, on which we want to place our backup. But we want all this process automatic, at a defined time/ interval.

Generate the ssh key pair on tendo-Srv:

ssh-keygen

Copy the public key to the tendo-backup:

scp ~/.ssh/id_rsa.pub arbab@192.168.1.177:

Connect to the tendo-backup:

ssh arbab@192.168.1.177

Append the public key to authorized_keys:

cat id_rsa.pub >> ~/.ssh/authorized_keys

We got an error saying “~/.ssh/authorized_keys: No such file or directory” it means that there is no .ssh directory for this user (this user has never used ssh before). Simply create an empty .ssh directory with 700 permissions:

mkdir ~/.ssh
chmod 700 ~/.ssh

Now,try to append the public key to authorized_keys once again:

cat id_rsa.pub >> ~/.ssh/authorized_keys

Remove the uploaded copy of authorized_keys:

rm id_rsa.pub

Edit the ssh server configuration:

sudo nano /etc/ssh/sshd_config

Make sure that public key authentication is enabled (it should be enabled by default), these entries must be set to yes also:

RSAAuthentication yes
PubkeyAuthentication yes

Reload the ssh configuration:

sudo service ssh reload

Create a directory,where we want to keep the backup on tendo-backup server and set a permission on it:

sudo mkdir /backup
sudo chown arbab:arbab /backup/

Disconnect from the tendo-backup:

exit

Try to connect to tendo-backup server again, and this time, it will not ask for the password 🙂

ssh arbab@192.168.1.177

Create a separate directory and file, where we want to keep the rsync log on tendo-Srv and set the permission on it as well:

sudo mkdir /rsynclog
sudo touch /rsynclog/rsynclog.log
sudo chmod 0777 /rsynclog/rsynclog.log

Now, copy the files from tendo-Srv to tendo-backup with Rsync:

rsync -e ssh --progress --partial --delete -avz  --log-file=/rsynclog/rsynclog.log /data/ arbab@192.168.1.177:/backup

Where /data/ is a directory on local server(tendo-Srv) and /backup/ is directory on remote server(tendo-backup) & rsynclog.log is a log file at /rsynclog location, where it will save the log every time after execute the command.

Check the log file:

cat /rsynclog/rsynclog.log

Check the backup on the tendo-backup server:

cd /backup/
ls

Edit the Cron file (Special thanks to Fernando Flores, to correct the syntax of this command):

sudo crontab -user arbab -e

16

We need to configure it in such a way that it automatically take the backup at 8 p.m. everyday:

00 20 * * * rsync -e ssh --progress --partial --delete -avz --log-file=/rsynclog/rsynclog.log /data/ arbab@192.168.1.177:/backup

Hope this will help you!

Please Remember me in your prayers!

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: