10 Practical Examples of Rsync Command in Linux

10 practical examples of rsync command in linux

Rsync Command in Linux:

'Rsync' Command is a powerful and versatile tool for file synchronization in Linux. It is not only efficiently copies files and directories but also synchronizes them in various scenarios. Whether you're looking to backup your data, mirror directories, or maintain remote server synchronization, rsync can do all of that.

These 10 practical commands provide you with a solid foundation for efficiently managing your data, whether it's for backups, remote synchronization, or data mirroring.

If you are interested in learning, Request you to go through the below recommended tutorial.

DevOps Full Course Tutorial for Beginners - DevOps Free Training Online
Docker Full Course Tutorial for Beginners - Docker Free Training Online
Kubernetes Full Course Tutorial for Beginners - Kubernetes Free Training Online
Ansible Full Course Tutorial for Beginners - Ansible Free Training Online
Openstack Full Course Tutorial for Beginners - Openstack Free Training Online

Let's Get Started.

You can also watch this tutorial video on our youtube channel.

1. Basic Local Directory Synchronization:

The most basic use of rsync involves synchronizing the contents of one directory with another on the same system. The -av options enable archive mode and verbose output, ensuring that file attributes are preserved during synchronization.

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

This command efficiently copies and synchronizes the contents of the source directory with the destination directory.

Example:

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

2. Synchronize with a Remote Server over SSH:

One of the standout features of rsync is its ability to synchronize data between local and remote systems securely. Using SSH for secure communication, you can sync a local directory with a remote directory on a different server.

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

The -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

3. Dry Run (Preview) Mode:

Before making actual changes, it's often a good practice to perform a dry run or preview of the synchronization to see what changes would be made. This is helpful in avoiding unexpected results.

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

The --dry-run flag shows you what changes rsync would make without actually applying them.

Example:

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

4. Synchronize and Delete Extra Files on the Destination:

Sometimes, you may want to ensure that the destination directory exactly mirrors the source, including removing any extra files not present in the source. The --delete option comes to the rescue.

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

With this command, rsync will synchronize the source with the destination and remove any files in the destination that are not in the source.

Example:

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

5. Exclude Files and Directories:

There might be situations where you need to exclude specific files or directories from the synchronization process. You can achieve this using the --exclude option.

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

The --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

6. Show Progress and Transfer Speed:

If you want to keep an eye on the progress of your synchronization, as well as the transfer speed, use the --progress option.

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

This command provides you with real-time updates on the progress of your rsync operation and the speed at which data is being transferred.

Example:

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

7. Preserve File Permissions and Ownership:

Preserving file permissions and ownership is crucial in many scenarios, especially when working with system files. The --chown option allows you to set the ownership of the copied files.

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

With this command, you can ensure that file permissions and ownership are retained in the destination directory.

Example:

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

8. Use Rsync Daemon for Remote Sync:

rsync can also operate in daemon mode, allowing for remote synchronization through a custom port. This is particularly useful when working with remote servers.

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

In this example, we use rsync's daemon mode to synchronize data with a remote server over port 6262.

Example:

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

9. Backup Rotation 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.

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

This command creates a new backup while linking to the previous one, saving space by only copying changes.

Example:

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

10. Exclude Multiple Patterns from Sync:

When you need to exclude multiple files or patterns from synchronization, you can utilize an exclude file (e.g., exclude.txt) that lists the items to be excluded.

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

This approach allows you to maintain a list of excluded files and patterns for easy management.

Example:

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

That's it for this post. Keep practicing and have fun. Leave your comments if any.

Post a Comment

0 Comments