Linux rsync: How to Synchronize Files Locally and over SSH

rsync is a tool that copies files and directories to local or remote systems and transfers changes. The default behavior is to copy one-way from the source to the destination, and files that exist only in the destination are not deleted unless --delete is specified.

Before Starting

The examples are run in the Linux shell. First, check if it is installed with rsync --version. On Ubuntu, if it is not installed, you can install it with the following command. For remote transfers, rsync is required on both systems, and if using SSH (Secure Shell), remote access and write permissions on the destination path are also needed.

sudo apt update
sudo apt install rsync

If you input the source and target paths in reverse, you can overwrite or delete the data you want. Before handling important data, make sure to secure a separate backup, and especially when using the delete option, first check the expected changes with --dry-run. Synchronization is different from a backup that restores a previous point because mistakes or corruption in the source can also be reflected in the target.

Local directory copy and the difference of the slash

The following is an example of creating a new practice folder under the home directory and preparing one file. If there is an existing folder with the same name, use a different name.

mkdir -p "$HOME/rsync-demo/source" "$HOME/rsync-demo/dest"
printf 'hello\n' > "$HOME/rsync-demo/source/hello.txt"
rsync -av "$HOME/rsync-demo/source/" "$HOME/rsync-demo/dest/"
ls -l "$HOME/rsync-demo/dest/"

If hello.txt appears in the result of the last command, the contents of the source directory have been copied into the target. Running the same rsync command again usually does not re-transfer ordinary files that have not changed. The basic comparison uses file size and modification time, so it is not a complete verification that the contents are the same.

The / at the end of the source means that the contents inside the directory, not the directory itself, are copied to the target. If the target directory already exists and you omit the slash, the source directory name will be created under the target.

rsync -av "$HOME/rsync-demo/source" "$HOME/rsync-demo/dest/"

Running this command will create dest/source/hello.txt. The previously created dest/hello.txt will remain as it is. The difference between the two paths is especially important when using the delete option. If there are spaces in the path, enclose it in quotes as in the example.

Frequently Used Options and Pre-Checks

  • -a: This is archive mode. It requests recursive copying and preservation of time, permissions, owner, group, and symbolic links. Preservation of ownership may be limited depending on permissions and the target file system's support. ACLs and extended attributes are not included in -a.
  • -v: Shows detailed operation output.
  • -n or --dry-run: Shows the expected operations without actually making any changes.
  • -z: Uses compression during transfer. The effectiveness varies depending on network conditions and file types and it is not always necessary for local copies.

For example, you can check as follows before the actual copy. The output shows expected changes, and if files change or errors occur between a trial and actual execution, the actual result may differ.

rsync -avn "$HOME/rsync-demo/source/" "$HOME/rsync-demo/dest/"

If you need to check whether files with the same size and modification time actually have different content, you may consider --checksum comparison, but it can be costly since both files are read. For the detailed meaning of the options, refer to rsync official manual.

Deleting Files That Exist Only in the Destination

--delete deletes items from the synchronization target directory that do not exist in the source. If you accidentally specify the wrong destination or directory level, necessary files may also be deleted, so first check the target path and the trailing slash of the source and review the results of a trial run.

The following example can list items that exist only in the destination, like the dest/source/ created above, for deletion. Run it only when there are no files to keep in the practice folder.

rsync -avn --delete "$HOME/rsync-demo/source/" "$HOME/rsync-demo/dest/"

Check the deleting entries in the output, and only remove -n to execute it for real if the deletion range is correct and backups for recovery are ready.

rsync -av --delete "$HOME/rsync-demo/source/" "$HOME/rsync-demo/dest/"

This command changes only the destination and does not delete files from the source. Files that existed only in the destination may not be recoverable without the usual file recovery procedures. Prepare separate version backups and restoration verification for operational data.

Excluding Files or Directories

To exclude the cache directory from the source from the transfer target, specify it as follows. Because the content of the source is the transfer root, the leading slash refers to cache at that root. First, run a trial to ensure that only the desired items are excluded.

rsync -avn --exclude='/cache/' "$HOME/rsync-demo/source/" "$HOME/rsync-demo/dest/"

If the result is correct, remove -n and copy it. The default exclusion rule generally protects the exclusion items of the target in --delete, but using --delete-excluded or changing the filter rules may change the behavior. Check complex patterns in a separate folder before applying them to actual data.

Synchronize with a remote server via SSH

In an environment ready for SSH access, the following command sends the contents of the local source to the target directory on the remote server. Replace user, server.example.com, and paths with actual values, and prepare the remote target directory in advance. When connecting for the first time, verify the server's host key fingerprint through a trusted channel.

rsync -avn -e ssh "$HOME/rsync-demo/source/" user@server.example.com:/home/user/backup/

After reviewing the expected transfer list, remove -n to transfer. Conversely, to retrieve from the remote server to the local machine, set the remote path as the source location.

rsync -avn -e ssh user@server.example.com:/home/user/backup/ "$HOME/rsync-demo/dest/"

If the SSH port differs from the default, for example, use -e 'ssh -p 2222'. For remote access, it is recommended to use an account with limited permissions and SSH key authentication. Do not directly input the password in the command because it may be exposed in process information or shell history; therefore, the sshpass -p example in the previous article is not used. In automated tasks, manage key protection and access scope together.