Rsync Files and Folders With Linux


Using Rsync With Linux

Rsync is a very good package which allows use Linux users to synchronize files and folders on the same, or different machines. The concept behind Rsync is that is only updates files that have changed in date and file size. So, from a backup point of view, it can keep an accurate live backup when you run the command to synchronize the files.

Using Rsync On Same Computer

pi@raspberrypi$ rsync -avz /home/pi/test1/ /home/pi/test2/

Using Rsync On Different Computers

1) Generate SSH keys on the computer with the host(master) files
pi@raspberrypi$ ssh-keygen
Note: Do not set passwords for auto connections, but, you can if you want to be prompted for password
2) Move the id_rsa.pub file to the guest(slave) machine where you want the backups (ie. machine on local network). 
There are many ways to perform this task. The whole idea is that the id_rsa.pub file will end up as the authorized_keys files in the .ssh folder of the other machine.
Method A:
pi@raspberrypi$ ssh-copy-id -i ~/.ssh/id_rsa.pub pi@192.168.0.115:~/.ssh
Method B:
pi@raspberrypi$ scp ~/.ssh/id_rsa.pub pi@192.168.0.115:~/.ssh/authorized_keys
3) Login to the slave machine and change file permissions.
pi@raspberrypi$ chmod 600 authorized_keys
4) Use Rsync to create backups
pi@raspberrypi$ rsync -avz /home/pi/test1/ pi@192.168.0.115:/home/pi/test2/
Note: Rsync can be run manually or with a cron job that runs at your desired schedule.

Potential Problems:

Permissions. When you sync the files to the slave machine the folder should have adequate permissions. For example, if you login as pi@192.168.0.115 the files will transfer into the desired folder if the files or group is owned by pi. If you login as pi and attempt to move files into a folder owned by root, the sync will not happen.