Making backups regularly is one of the most relevant tasks for a Linux user. In the fourth chapter of ‘Backup methods’ I will show you one of the most popular copying tools: rsync.

rsync can copy from a source to a destination, based on last modified time or file size. Destination folder does not need to exist.

rsync [<options>] <SOURCE> <DEST>

Options

  • --recursive, -r: recursive into directories.
  • --times, -t: preserve modification times.
  • --dry-run, -n: perform a trial run (no changes made).
  • --perms, -p: preserve permissions.
  • --acls, -A: preserve ACLs (How to set permissions for specific users). Implies --perms.
  • --archive, -a: archive mode, is a shortcut for recursive into directories, copy symlinks as symlinks, preserve permissions, preserve modification times, preserve group, preserve owner (using “sudo”), preserve device files (using “sudo”) and preserve special files.
  • --remove-source-files: remove source files after transfer.
  • --verbose, -v: increase verbosity.
  • -rsh=<COMMAND>, -e <COMMAND>: specify the remote shell to use. See Using with SSH.
  • --compress, -z: compress file data during the transfer.
  • --checksum, -c: update based on checksum, not modification date or file size.
  • --update, -u: update based only on modification date, not file size.
  • --backup --backup-dir=<DIR>, -b --backup-dir=<DIR>: Put copied files inside <DIR>.
  • --chown:<user>:<group> change the user and group of the copied files. You need superuser privileges to use this parameter.
  • --exclude "PATTERN": exclude files from syncing based on a pattern (e.g.: "*.pdf").
  • More options on man page (man rsync).

Copying locally

# Only copies one level (skips directories)
rsync origin/* dest/
# Recursive copy
rsync -r origin dest
# Copy the source folder content to the target directory
rsync -r origin-folder/ dest-folder/
# Copy the source folder itself and its content to the target directory
rsync -r origin-folder dest-folder/
# Preserve last modification time
rsync -tr origin/ dest/
# Create a folder inside dest/ (called 'bck') for copied files
rsync -r -b --backup-dir=bck origin/ dest/

Using with SSH

Check rsync and SSH: synchronize a local folder with a remote one.

If you have any suggestion, feel free to contact me via social media or email.