The commands below use a folder ~/backup/
as storage. Files are copied with the following features:
rsync --relative
: Tree structure relative to/
is preserved (subdirectories created if needed).realpath --no-symlinks
: Symlinks not resolved, so in case you use the backup to restore files, they will end up where they were found.
--recursive
: Subfolders too--perms
: Permissions preserved--copy-links
: Don't backup links, but the files they are pointing to
Backup a file or folder (basic concept):
# Backup one file
rsync --perms --copy-links --relative $(realpath --no-symlinks file-to-backup) ~/backup/
# Backup a directory and all inside it
rsync --perms --copy-links --relative --recursive $(realpath --no-symlinks folder-to-backup) ~/backup/
# Backup the current directory
rsync --perms --copy-links --relative --recursive $(realpath --no-symlinks .) ~/backup/
To restore a the backup, while saving a copy of whatever was replaced (just in case, because rsync doesn't do confirmations):
rsync --verbose --recursive --backup-dir=/tmp/backup-just-in-case ~/backup/ /