Installing RSYNC
Instructions for installing and configuring Remote Synchronization (RSYNC) for data synchronization
rsync (Remote Synchronization) is a cross-platform command-line utility for synchronizing data with minimal bandwidth usage. It is widely used for backups, migrations, and mirroring data between servers.
For example, with rsync you can move projects from shared hosting to a VPS, synchronize directories between multiple dedicated servers, or keep up-to-date copies of files across your infrastructure.
Installing rsync
For CentOS:
yum -y install rsync
For Debian/Ubuntu:
sudo aptitude install rsync
rsync usage examples
Local synchronization
sudo rsync -avh --delete /var/www/ /root/backup/www/
This command syncs the contents of /var/www/
with /root/backup/www/
, removing files that no longer exist in the source.
Syncing to a remote server
sudo rsync -avh --delete /var/www/ root@192.168.1.50:/root/backup/www/
Files will be copied to another server over SSH.
Note
For synchronization to work, rsync must be installed on both servers.
Syncing from a remote server to local
sudo rsync -avh root@192.168.1.50:/var/www/ /root/backup/www/
Syncing over SSH with a custom port
sudo rsync -avh -e "ssh -p 1111" root@192.168.1.50:/var/www/ /root/backup/www/
Excluding directories
sudo rsync -avh --exclude='dir/' --exclude='dir/tmp/' /var/www/ /root/backup/www/
This lets you exclude unnecessary subdirectories from synchronization.
Moving files from a remote server (removing source files)
sudo rsync -avh --remove-source-files root@192.168.1.50:/var/www/ /var/www/
Copying MySQL data to another disk
rsync -vrplogDtH --progress /var/lib/mysql/ /newhdd/var/lib/mysql/
Useful rsync options
- -v — verbose mode
- -r — recursive copy
- -a — archive mode (preserves permissions, symlinks, and structure)
- -b — create backups
- -c — compare files by checksum
- --delete — remove files not present in the source
- -h — human-readable output
- -n — dry run (test without making changes)
- -p — preserve permissions
- -z — compress data during transfer
- -H — preserve hard links
- -x — stay on one file system
The full list of options is available in the manual:
man rsync