Rsync Command uses with examples
The rsync command is a powerful and versatile tool for synchronizing files and directories between different locations in Linux. Whether you’re managing backups, deploying software, or just transferring data between systems, rsync offers a wide range of options and features that make it an indispensable tool for system administrators and power users alike.
In this blog post, we’ll take a closer look at the rsync command, exploring its various options, use cases, and best practices.
Installing rsync
Before we dive into the details of using rsync, let’s first make sure it’s installed on our system. Most modern Linux distributions should come with rsync pre-installed, but if you’re using an older or more minimalist distribution, you may need to install it manually.
To check if rsync is already installed on your system, you can run the following command:
sudo apt-get install rsync
Once rsync is installed, you’re ready to start using it.
Basic usage
The most basic usage of rsync is to copy files and directories from one location to another. The basic syntax for copying files with rsync is as follows:
rsync source destination
/path/to/source
to the directory /path/to/destination
, you would run the following command:rsync /path/to/source /path/to/destination
Advanced options
In addition to the basic usage described above, rsync offers a wide range of advanced options and features. Here are some of the most commonly used options:
-
-a
: This option stands for “archive” and is a shorthand for several other options that are commonly used when synchronizing files, including-r
(recursive),-l
(copy symlinks as symlinks),-p
(preserve permissions), and-t
(preserve modification times). This is typically the most useful option to use when synchronizing files.-v
: This option stands for “verbose” and causes rsync to output more detailed information about what it’s doing. This can be useful for debugging or just getting a better sense of what rsync is doing.-z
: This option stands for “compress” and causes rsync to compress data before sending it over the network. This can significantly speed up transfers over slow or congested networks.--delete
: This option causes rsync to delete files from the destination that do not exist in the source. This can be useful for keeping the destination in sync with the source.--exclude
: This option allows you to specify files or directories that should be excluded from the synchronization. You can use this option multiple times to exclude multiple files or directories.--progress
: This option causes rsync to output progress information during the transfer, including the percentage completed and the transfer speed.
Examples
Let’s take a look at some real-world examples of how to use rsync.
Synchronizing a local directory with a remote server
Suppose you have a directory /path/to/local
on your local machine that you want to synchronize with a directory `/path
Example 1: Syncing two local directories
Suppose you have two directories on the same machine that you want to keep in sync, /path/to/source
and /path/to/destination
. To synchronize these directories, you can run the following command:
rsync -av /path/to/source/ /path/to/destination/
The -a
option ensures that rsync preserves all file attributes, including permissions, ownership, timestamps, and symlinks. The -v
option enables verbose output, so you can see which files are being transferred. The trailing slashes after the directory paths ensure that rsync copies the contents of the directories, rather than the directories themselves.
Example 2: Syncing a local directory with a remote server
Suppose you have a directory on your local machine that you want to synchronize with a directory on a remote server. To do this, you can use the following command:
rsync -av /path/to/local user@remote:/path/to/remote
In this example, /path/to/local
is the source directory on your local machine, user
is the username for the remote server, remote
is the hostname or IP address of the remote server, and /path/to/remote
is the destination directory on the remote server. You’ll be prompted to enter the password for the remote user account.
Example 3: Syncing a remote directory with a local directory
Suppose you have a directory on a remote server that you want to synchronize with a directory on your local machine. To do this, you can use the following command:
rsync -av user@remote:/path/to/remote /path/to/local
In this example, user
is the username for the remote server, remote
is the hostname or IP address of the remote server, /path/to/remote
is the source directory on the remote server, and /path/to/local
is the destination directory on your local machine. You’ll be prompted to enter the password for the remote user account.
Example 4: Syncing a directory and excluding certain files
Suppose you want to synchronize a directory, but exclude certain files or directories from the transfer. To do this, you can use the --exclude
option. For example, to synchronize a directory /path/to/source
, but exclude all files with the extension .log
, you can use the following command:
rsync -av --exclude '*.log' /path/to/source/ /path/to/destination/
In this example, *.log
is a wildcard pattern that matches all files with the extension .log
. The --exclude
option tells rsync to exclude any files that match this pattern.
Example 5: Dry run mode
Before you perform an actual synchronization, it’s often a good idea to run a dry run to see what changes will be made. To do this, you can use the --dry-run
option, like this:
rsync -av --dry-run /path/to/source/ /path/to/destination/
This will show you a list of all the files that would be copied, deleted, or modified, without actually performing any changes.
Rsync Bash Script for Weekly Backup:
#!/bin/bash # Set the source and destination directories src_dir="/path/to/source" dest_dir="/path/to/backup" # Set the name of the backup file backup_name="backup_$(date +%Y-%m-%d_%H-%M-%S)" # Use rsync to take an incremental backup of the source directory rsync -avz --delete --backup --backup-dir="$dest_dir/$backup_name" "$src_dir/" "$dest_dir/latest/" # Remove any old backups find "$dest_dir" -maxdepth 1 -type d -name "backup_*" -mtime +7 -exec rm -rf {} \;
To set up a weekly cron job to run this script automatically, follow these steps:
- Open the crontab configuration file by running
crontab -e
in the terminal. - Add the following line at the end of the file to schedule the script to run every Sunday at 3 AM:
0 3 * * 0 /bin/bash /path/to/backup_script.sh
- Save and close the file.
This cron job will run the backup script every Sunday at 3 AM. You can modify the cron schedule by changing the values in the cron expression (0 3 * * 0
), which represents the minute, hour, day of the month, month, and day of the week when the job will run. For example, to run the job every day at 3 AM, you could change the cron expression to 0 3 * * *
.
Note that you may need to modify the permissions of the backup script to make it executable using chmod +x backup_script.sh
.
These are just a few examples of how you can use rsync to synchronize files and directories. With its wide range of options and features, rsync is a versatile tool that can be used in many different scenarios.
Subroto Mondal
Latest posts by Subroto Mondal (see all)
- Installing and Configuring OpenShift: A Step-by-Step Guide for Linux Experts with Terminal Code - February 19, 2023
- Sed Command in Linux with Practical Examples - February 19, 2023
- Rsync Command uses with examples - February 19, 2023