Putorius
Linux Quick Tips

Using rsync to Synchronize Directories Over SSH

Q: How can I synchronize a local directory with a remote directory on another linux machine.  I have a Fedora system and would like to have it automatically synchronize a specific directory with a remote directory to create an online backup.

A:  With Linux there are usually multiple ways to skin a cat. Using RSYNC would be my first thought and would work perfect for what you're trying to do. Then placing rsync into a cronjob will allow you to run the backups daily, or whenever you see fit.

Using rsync is simple.  Let's say you wanted to sync your home directory (/home/user) on a computer called Home, with a remote folder called backup (/backup) on a system called Work.

The first step I would take is to setup SSH keys.  This will allow your user to login via SSH without using a username or password which is preferred for scripting tasks.  You can use our "How to setup SSH Key Authentication" article here for instructions.

Now it is as easy as:

 rsync -a --update -e ssh /home/user user@work:/backup 

The above command tells rsync to use archive mode (-a) with the update option (--update), to connect using SSH (-e ssh) and copy everything from the local directory (/home/user) to the remote directory on the specified host with the specified username (user@work:/backup).

Archive mode (-a) for rsync include the following options:

and exclude some options as well:

The update option (--update) forces rsync to skip any files which exist on the destination and have a modified time that is newer than the source file. (If an existing destination file has a modification time equal to the source file's, it will be updated if the sizes are different.)

The -e option allows you to specific the remote shell, in this case SSH.

Using this technique will sync the files in the local directory with the remote directory, BUT, will not delete any files deleted in the local directory.  For example if you delete a file called paper.txt from your local home directory, it will NOT delete it from the remote directory.  There is however a delete option (--delete) which will allow this functionality.  Therefore using the following command will truly synchronize (delete files in the remote directory that no longer exist in the local directory) the two folders.  I would refrain from using this if you're create a backup.  This will allow you to retrieve files accidentally deleted in your local directory.

 rsync -a --update --delete -e ssh /home/user user@work:/backup 

The rsync command comes with many options, to learn more I suggest you start at the man pages.

Once you have rsync working the way you want, you can place it into crontab to have it run automatically at midnight, or anytime you want.

Exit mobile version