Rsync (remote synchronize) is a fast and versatile command-line utility for synchronizing files and directories between two locations. It’s widely used for backups, mirroring, and data migration due to its efficiency in transferring only the changed parts of files. This guide will explore how to use rsync for various synchronization tasks, both locally and remotely.
The basic syntax for rsync is:
rsync [options] SOURCE DESTINATION
Here are some commonly used options:
-a, --archive: Archive mode; this is a quick way to tell rsync to enable recursive, copy symlinks, preserve permissions, preserve modification times, preserve group, preserve owner, and preserve device files. It’s often the most useful option.-v, --verbose: Increases verbosity, showing what files are being transferred and providing progress information.-h, --human-readable: Output numbers in a human-readable format.To copy or synchronize files and directories locally, you can use the following command:
rsync -avh /path/to/source /path/to/destination
rsync -avh /home/user/documents /mnt/backup/documentsFor more detailed information, refer to the official rsync manpage: Manpage
To synchronize files from your local machine to a remote server:
rsync -avh /path/to/local/source user@remote_host:/path/to/remote/destination
rsync -avh ~/my_project user@example.com:/var/www/htmlTo synchronize files from a remote server to your local machine:
rsync -avh user@remote_host:/path/to/remote/source /path/to/local/destination
rsync -avh user@example.com:/var/www/html ~/backups/websiteTo ensure the destination exactly mirrors the source (i.e., delete files on the destination that are no longer on the source), use the --delete option:
rsync -avh --delete /path/to/source /path/to/destination
--delete with extreme care, as it will permanently remove files from the destination.To exclude specific files or directories from the synchronization, use the --exclude option:
rsync -avh --exclude 'node_modules/' --exclude '*.log' /path/to/source /path/to/destination
--exclude options.Before executing a potentially destructive rsync command (especially with --delete), it’s highly recommended to perform a dry run using the --dry-run or -n option. This will show you what rsync would do without actually making any changes.
rsync -avhn --delete /path/to/source /path/to/destination
rsync is incredibly versatile and can be used for a variety of tasks:
rsync is a powerful and flexible tool for anyone needing to synchronize files and directories efficiently. Its ability to handle local and remote transfers, coupled with options for fine-grained control over the synchronization process, makes it an essential utility for system administrators, developers, and anyone managing data. By integrating rsync into your workflow, you can ensure your data is always where it needs to be, reliably and efficiently.