rsync
Guide for synchronizing files and directories between systems using rsync over SSH.
Local to remote
Section titled “Local to remote”-
Install
Section titled “Install”Ensure rsync is installed on both the local (source) and remote (destination) systems.
Terminal window sudo apt update && sudo apt install rsync -y -
Basic sync
Section titled “Basic sync”Synchronize a local directory to a remote server. The
-aflag preserves permissions/links, and-vprovides verbose output.Terminal window rsync -av {{RSYNC_LOCAL_PATH_VAR}} {{USERNAME_VAR}}@{{SERVER_IP_VAR}}:{{RSYNC_REMOTE_PATH_VAR}} -
Sync with compression and progress
Section titled “Sync with compression and progress”Use
-zto compress data during transfer and-Pto show a progress bar and allow resuming partial transfers.Terminal window rsync -avzP {{RSYNC_LOCAL_PATH_VAR}} {{USERNAME_VAR}}@{{SERVER_IP_VAR}}:{{RSYNC_REMOTE_PATH_VAR}} -
Delete extraneous files
Section titled “Delete extraneous files”Use the
--deleteflag to remove files on the destination that no longer exist on the source, keeping them perfectly in sync.Terminal window rsync -av --delete {{RSYNC_LOCAL_PATH_VAR}} {{USERNAME_VAR}}@{{SERVER_IP_VAR}}:{{RSYNC_REMOTE_PATH_VAR}}
Remote to local (pull)
Section titled “Remote to local (pull)”-
Pull files
Section titled “Pull files”Download a directory from a remote server to your local machine.
Terminal window rsync -av {{USERNAME_VAR}}@{{SERVER_IP_VAR}}:{{RSYNC_REMOTE_PATH_VAR}} {{RSYNC_LOCAL_PATH_VAR}} -
Custom SSH port
Section titled “Custom SSH port”If the remote server uses a non-standard SSH port, specify it using the
-eflag.Terminal window rsync -av -e 'ssh -p 2222' {{USERNAME_VAR}}@{{SERVER_IP_VAR}}:{{RSYNC_REMOTE_PATH_VAR}} {{RSYNC_LOCAL_PATH_VAR}}
Maintenance
Section titled “Maintenance”-
Dry run
Section titled “Dry run”Always perform a dry run before executing a command with
--deleteto see what changes will be made without actually transferring files.Terminal window rsync -av --dry-run {{RSYNC_LOCAL_PATH_VAR}} {{USERNAME_VAR}}@{{SERVER_IP_VAR}}:{{RSYNC_REMOTE_PATH_VAR}} -
Exclude files
Section titled “Exclude files”Exclude specific files or directories (like
node_modulesor.git) from being synchronized.Terminal window rsync -av --exclude '{{PATTERN_VAR}}' {{RSYNC_LOCAL_PATH_VAR}} {{USERNAME_VAR}}@{{SERVER_IP_VAR}}:{{RSYNC_REMOTE_PATH_VAR}}