Using the Rsync utility on a VPS
08:49, 28.04.2022
Rsync is a piece of software that allows you to synchronize files remotely or locally on VPS and VDS. The key feature of the utility is the highest copy speed, achieved by ignoring files that have not changed after the last synchronization. Rsync is a built-in utility for most existing Linux distributions, and at the same time it has practically no free analogs in terms of speed and convenience – this is due to the popularity among webmasters around the world.
Basic Rsync syntax
Remote synchronization using Rsync works according to a simple syntax, which schematically looks like this:
# rsync -options <source> <destination>
<Options> are utility parameters, <source> means the directory or file that is the source, and <destination> is the receiver.
Let's have a look at the most frequently used utility options:
- -v – debugging mode;
- -q – prohibition of error output;
- -r – recursive copy mode;
- -a – archiving with a saving of symlinks;
- -z – data compression;
- -b – creating backups;
- -h – data output in human-readable format.
This is just a basic set of parameters that can be activated when working with the Rsync utility – you can find much more detailed guides and manuals in the official documentation.
Synchronization with a remote system
If you have local access to the server it is enough to specify two directories via the command line, the source, and the receiver:
rsync -avzhHl /path/of/source/folder /path/to/destination/folder
If data backup needs to be performed remotely, then add the account name and the IP address of the target node to the command:
rsync -avzhHl /path/of/source/folder root@192.168.56.1:/path/to/destination/folder
Correct and safe operation for remote sync requires configuring access using keys, the utility allows it.
How to set up automatic directory synchronization
To automate the process, you need to use the cron task scheduler, which is also available on any Linux-based OS. First, you need to create a script:
vi rsync_to_cron.sh !/bin/sh RSYNC=/usr/bin/rsync SSH=/usr/bin/ssh KEY=/root/.ssh/id_rsa RUSER=root RHOST=192.168.56.1 RPATH=/remote/dir LPATH=/local/dir $RSYNC -az -e "$SSH -i $KEY" $RUSER@$RHOST:$RPATH $LPATH
Then we create a task for cron:
crontab -e 0 22 * * * /root/scripts/rsync_to_cron.sh
Well, to track the synchronization process, you can always use the -progress key:
rsync -avzhHl --progress /path/of/source/folder root@192.168.56.1:/path/to/destination/folder
Usage example
The simplest example of using the Rsync utility for remote synchronization would be the command:
rsync -a --delete-after /data/ /backup/
Here we give the command to synchronize the /data directory with the /backub directory. The delete-after key will clear the /backup directory of all files that are missing in the /data directory. The -a parameter allows you to speed up the process since only modified files will be copied.
The utility allows you to copy data in both directions, while the Rsync syntax allows you to activate many additional modes and options. This concludes our material and thanks for your attention. If you need to rent a VPS – contact us, our experts will help you choose the best tariff.