rsync for System Administrator
The name rsync stands for remote synchronization. It is a widely used command-line utility for synchronizing files and directories between two locations. It is particularly efficient for transferring data over a network and is often used for backup, mirroring, and remote file transfer.
How to install rsync
# For Debian-based distributions, including Ubuntu
sudo apt-get install rsync
# For Fedora-based distributions, such as CentOS
sudo dnf install -y rsync
# For macOS
brew install rsync
rsync Useful Options and Parameters
-
-v or –verbose : Increase verbosity, providing more detailed output during the transfer.
-
-a or –archive : Archive mode. The archive mode instructs Rsync to perform archive operation and, recursively copy directories and its children, preserve all file attributes such as file permissions, ownership, symlinks, and timestamps.
-
-r or –recursive : Recursively copy directories.
-
-z or –compress : Compress file data during the transfer to reduce bandwidth usage.
-
–exclude=PATTERN : Exclude files or directories matching the specified pattern.
-
–include=PATTERN : Include files or directories matching the specified pattern.
-
–delete : This option will delete all directories and files that are not available in the source but available in the destination.
-
–dry-run : Perform a trial run without making any actual changes.
-
-u or –update : Skip files on the destination side that are newer than the source files so only older files are updated.
-
-h or –human-readable : Output numbers in a human-readable format.
-
–progress : Show progress during the transfer.
-
-e or –rsh=COMMAND : Specify which remote shell to use.
-
–bwlimit=RATE : Limit the bandwidth to increase network efficiency.
rsync Command Syntax
Basic Syntax
rsync [OPTIONS] SOURCE DESTINATION
-
[OPTIONS] : This is the section where can include
rsync
options, such as-a -v -r
etc. -
SOURCE : It is the path of the source directory or file.
-
DESTINATION : It is the path of the target directory.
Basic Syntax for Remote Shell
rsync [OPTIONS] -e "SSH_COMMAND" SOURCE DESTINATION
The -e option is used to specify the remote shell. In most cases, it used ssh to connect to the remote host using the rsync remote update protocol.
Basic Syntax for Connecting to a Rsync Daemon
# Pull data from remote to local
rsync [-options] rsync://user@x.x.x.x[:PORT]SOURCE DESTINATION
#OR
rsync [-options] user@x.x.x.x::SOURCE DESTINATION
# Push data local to remote
rsync [-options] SOURCE rsync://user@x.x.x.x[:PORT]DESTINATION
#OR
rsync [-options] SOURCE user@x.x.x.x::DESTINATION
rsync Command Example
Common rsync command : Skip all subdirectory
rsync source-dir/* destination-dir/
rsync with subdirectories
rsync -r source-dir/* destination-dir/
Combine rsync Commands
rsync -av --exclude='*.txt' source-dir/* destination-dir/
rsync Command with --delete
rsync -av --delete source-dir/ destination-dir/
The --delete
option allows to delete files from the destination directory that no longer exist in the source directory.
rsync Backup Example
-
Create directory at
/opt/script
-
Open file called
rsync-backup.sh
>>vim /opt/script/rsync-backup.sh
add below lines
#!/bin/bash
rsync -az --password-file=/root/secret/rsync /opt/backup/ rsync://username@ip-or-domain:/path-to-store
# Inside password file /root/secret/rsync store password only
12345
# Password file must be chmod 600
# Can add cron if want
0 2 * * * sudo /usr/bin/bash /opt/script/rsync-backup.sh
-
Assume that all backup are stored at /opt/backup