HOW TO RSYNC via SSH (Backup)

17 Jun

HOW TO RSYNC via SSH (Backup)

http://www.tanbir.net/how-to-rsync-via-ssh-backup/

June 17, 2013 by Tanbir A.

A very easy copy & paste tutorial on how to backup your very important files from your VPS, to another VPS, or linux server.

This shows to to make a secure connection between servers via SSH, so a password is no longer required between these two servers, and only those two servers to talk to each other.

Make note of your servers, (this tutorial shows how to back up one VPS, to another VPS), the main server you wish to backup, and the backup server.

Main
xxx.xxx.xxx.xxx

Backup
xxx.xxx.xxx.xxx

Follow the commands in sequence to start making backups, and changing Blue text to the appropriate user, or IP Address.

Main> ssh-keygen -t rsa -f .ssh/id_rsa

-t is the encryption type
-f tells where to store the public/private key pairs. In this case, the .ssh directory on home is being used

A password will be asked; leave this part blank, just pressing <enter>
Now, go the .ssh directory, and you will find two new files: id_dsa and id_dsa.pub. The last one is the public part. Now, copy the public key to the server machine

Main> cd .ssh
Main> scp id_rsa.pub user@Backup:~/.ssh/id_rsa.pub

Of course, this time you will need to enter the password.
Now, login into the server machine and go to the .ssh directory on the server side

Main> ssh user@Backup
Backup> cd .ssh

Now, add the client’s public key to the know public keys on the server

Backup> cat id_rsa.pub >> authorized_keys
Backup> chmod 644 authorized_keys
Backup> rm id_rsa.pub
Backup> exit

Some useful Backup commands:

  1. rsync -ravz -e “ssh” user@Main:/home /root/backup/$(date +”%d-%m-%Y”)/
  2. rsync -ravz -e “ssh” user@Main:/etc /root/backup/$(date +”%d-%m-%Y”)/
  3. rsync -ravz -e “ssh” user@Main:/var /root/backup/$(date +”%d-%m-%Y”)/
  4. rsync -ravz -e “ssh” user@Main:/var/lib/mysql /root/backup/database/$(date +”%l%p-%d-%m-%Y”)/

To make this process completely automated, crate some cron jobs for these for every hour, day, week, or month, dependent on you needs of backups.
As well as making another cron job to delete backup files older then 30 days, to keep backup space for daily backups!

find /path/to/files/* -ctime +30 -exec rm -rf {} \+

———————————————————————

Full Credit to http://codedpenguin.com