Enquire Now

Thanks for the like! We're thrilled you enjoyed the article. Your support encourages us to keep sharing great content!

Linux Opensource

Rsync Command in Linux

The ‘Rsync’ command is a strong and flexible tool for syncing files in Linux. It not only copies files and folders efficiently but also keeps them in sync under different conditions. If you want to back up your data regularly, create exact copies of directories, or keep remote servers updated, rsync is capable of handling these tasks.

It supports various options to customize how files are synced, enabling users to optimize their data transfers for speed and bandwidth. Whether you’re dealing with large volumes of data or just a few files, rsync offers a reliable solution to ensure everything is aligned across locations.

Local Directory Synchronization

To use rsync in the easiest way, you can make the files in two folders on the same computer exactly the same. This is called synchronizing the folders. You do this by using the -av options.

The -a stands for archive mode, which keeps the files’ details, like time stamps and permissions, just like they are. 

The -v is for verbose, meaning it will show you more information about what rsync is doing while it works. This helps you to see that everything is copied properly and no file properties are lost in the process.

Syntax:

rsync -av /source/directory/ /destination/directory/

This command quickly makes a copy of everything in the source directory and ensures it matches with everything in the destination directory, keeping them synchronized.
Example :

rsync -av /usr/local/app-data /backup/app-data

Remote File Synchronization via SSH

You can use SSH to safely connect yourcomputer to another server. This allows you to sync or match a folder on your computer with a folder on that server, making sure both have the same files.
Syntax:

rsync -avz -e ssh /local/source/ user@remote-server:/remote/destination/

Where -e ssh option specifies the use of SSH for secure data transfer, making it ideal for remote server synchronization.
Example :

rsync -avz -e ssh /usr/local/app-data congo@remote-server:/usr/local/app-data

Run (Preview) Mode

Before you make any real changes, it’s wise to first do a dry run or preview of the synchronization process. By doing this, you can clearly see what changes are going to happen. It is a useful step because it helps you avoid unexpected surprises or results, ensuring everything goes smoothly when you implement the actual changes.
syntax :

rsync -av --dry-run /source/directory/ /destination/directory/

Example :

rsync -av --delete /usr/local/app-data /backup/app-data

When you use this command, rsync will match the destination to the source. This means it will copy over the necessary files to make both locations the same. Additionally, any files that only exist in the destination and not in the source will be removed.

Filtering Out Unwanted Files and Folders

At times, you may not want to include certain files or folders when syncing your data. To make sure these are left out, you can use the –exclude option.
Syntax :

rsync -av --exclude 'file.txt' /source/directory/ /destination/directory/

–exclude option allows you to specify files or directories that should be skipped during synchronization.
**Example : 

rsync -av --exclude 'file.txt' /usr/local/app-data /backup/app-data

Displaying Real-Time Transfer Status

To monitor the progress of your synchronization and check the transfer speed, use the –progress option. This feature helps you see how things are moving along and how fast data is being transferred.
Synatx :

rsync -av --progress /source/directory/ /destination/directory/ 

Example :

rsync -av --progress /usr/local/app-data /backup/app-data

Maintain File Permissions and Ownership

Maintaining file permissions and ownership is very important in different situations, especially when working with system files. Using the –chown option, you can change who owns the files you are copying. This helps ensure that the files have the correct owner, which is important for managing access and security in many cases.
Syntax :

rsync -av --chown=USER:GROUP /source/directory/ /destination/directory/

Example :

rsync -av --chown=congo:administrator /usr/local/app-data /backup/app-data

Remote File Syncing with Rsync Daemon Mode

Rsync can also run in daemon mode, which means it can help you sync files over a network using a chosen port. This is really useful for managing files on remote servers. With daemon mode, rsync listens for connections to copy and update files easily between your computer and a server that’s far away. This setup makes sure your files are kept updated and organized without needing to be physically present at the server location. It’s a great tool for anyone working with remote servers regularly.

Syntax :

rsync -avz /source/directory/ rsync://remote-server:8873/destination/

Example :

rsync -avz /usr/local/app-data rsync://remote-server:6262/backup/app-data

Creating Incremental Backups with Hard Links

Creating backups with rsync can be optimized for efficiency and storage space. By using hard links, you can create incremental backups while preserving disk space.
Syntax :

rsync -av --link-dest=/backup/prev-backup /source/directory/ /backup/new-backup/

Example :

rsync -av --link-dest=/backup/app-data-old /usr/local/app-data /backup/app-data

Sync Only What You Need

If you want to prevent certain files or patterns from being synced, you can create and use an exclude file, such as exclude.txt. In this file, you list every item you wish to omit from the synchronization process. This way, only the files and patterns not on the list will be synced.

Syntax :

rsync -av --exclude-from=exclude.txt /source/directory/ /destination/directory/

Example :

rsync -av --exclude-from=exclude.txt /usr/local/app-data /backup/app-data
Sridhar S

Author

Sridhar S

Cloud Admin - Chadura Tech Pvt Ltd, Bengaluru

Related Posts