Rsync

From David's Wiki
\( \newcommand{\P}[]{\unicode{xB6}} \newcommand{\AA}[]{\unicode{x212B}} \newcommand{\empty}[]{\emptyset} \newcommand{\O}[]{\emptyset} \newcommand{\Alpha}[]{Α} \newcommand{\Beta}[]{Β} \newcommand{\Epsilon}[]{Ε} \newcommand{\Iota}[]{Ι} \newcommand{\Kappa}[]{Κ} \newcommand{\Rho}[]{Ρ} \newcommand{\Tau}[]{Τ} \newcommand{\Zeta}[]{Ζ} \newcommand{\Mu}[]{\unicode{x039C}} \newcommand{\Chi}[]{Χ} \newcommand{\Eta}[]{\unicode{x0397}} \newcommand{\Nu}[]{\unicode{x039D}} \newcommand{\Omicron}[]{\unicode{x039F}} \DeclareMathOperator{\sgn}{sgn} \def\oiint{\mathop{\vcenter{\mathchoice{\huge\unicode{x222F}\,}{\unicode{x222F}}{\unicode{x222F}}{\unicode{x222F}}}\,}\nolimits} \def\oiiint{\mathop{\vcenter{\mathchoice{\huge\unicode{x2230}\,}{\unicode{x2230}}{\unicode{x2230}}{\unicode{x2230}}}\,}\nolimits} \)

See Archwiki:rsync and rsync(1) - Linux man page

Getting Started

rsync -ah <src> <dst> --info=progress2
Flags
  • -a archive mode (recursive, preserves symlinks, permissions, times, group, owner, devices)
    • Excludes hard links (-H), ACLs (-A), and extended attributes (-X)
  • --no-i-r or --no-inc-recursive scans all files at the beginning rather than during the transfer
  • --progress shows per-file progress
  • --info=progress2 shows overall progress during the transfer

System Backups

  • Create a script and add it to your crontab -e

Basic Backup

#!/bin/bash

TARGET_FOLDER=<your target for backup>
EXCLUSIONS='--exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"}'

sudo rsync -aAXh ${EXCLUSIONS} / "${TARGET_FOLDER}" --info=progress2
Flags
  • -a archive mode
  • -A preserve ACLs (access control lists)
  • -X preserve extended attributes
  • -q quiet

Exclude

Add --exclude to your call.

Notes
  • Exclude typically uses a pattern. If you want to exclude from the root of the transfer, prepend /.
    E.g
    rclone --exclude "/project/node_modules" project /some/other/dir/
  • Add --exclude before --stats and --delete if you want it to work.

You can also use --exclude-from and point to a text file list of exclusions.

Delete

StackOverflow Differences between rsync delete

Be very careful with deleting. By default rsync only deletes files on the target which have the same name as files on the source.

Below are the relevent flags for deleting files on the target and the source.

The following two options deletes extraneous files on the target.

  • --delete Delete files on the target directory which are not in the source.
  • --delete-excluded Delete files on the target directory which are excluded in the transfer.

The following options concern replacing files on the target with the same name.

  • --delete-before Deletes files on the target before new replacement is copied over. (default)
  • --delete-during Deletes files on the target as replacement files with the same name are being copied over.
  • --del Alias for --delete-during
  • --delete-delay Marks files for deletion during transfer. Waits until the transfer is complete to delete files.
  • --delete-after Checks for files and deletes files after transfer after the transfer.

The following option deletes files from the source directory.

  • --remove-source-files Deletes files which have been synced.