Skip to content

rsync

Guide for synchronizing files and directories between systems using rsync over SSH.



  1. Ensure rsync is installed on both the local (source) and remote (destination) systems.

    Terminal window
    sudo apt update && sudo apt install rsync -y
  2. Synchronize a local directory to a remote server. The -a flag preserves permissions/links, and -v provides verbose output.

    Terminal window
    rsync -av {{RSYNC_LOCAL_PATH_VAR}} {{USERNAME_VAR}}@{{SERVER_IP_VAR}}:{{RSYNC_REMOTE_PATH_VAR}}
  3. Use -z to compress data during transfer and -P to 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}}
  4. Use the --delete flag 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}}
  1. 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}}
  2. If the remote server uses a non-standard SSH port, specify it using the -e flag.

    Terminal window
    rsync -av -e 'ssh -p 2222' {{USERNAME_VAR}}@{{SERVER_IP_VAR}}:{{RSYNC_REMOTE_PATH_VAR}} {{RSYNC_LOCAL_PATH_VAR}}
  1. Always perform a dry run before executing a command with --delete to 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}}
  2. Exclude specific files or directories (like node_modules or .git) from being synchronized.

    Terminal window
    rsync -av --exclude '{{PATTERN_VAR}}' {{RSYNC_LOCAL_PATH_VAR}} {{USERNAME_VAR}}@{{SERVER_IP_VAR}}:{{RSYNC_REMOTE_PATH_VAR}}