rsync

Updated: April 28, 2024

Rsync is a remote and local file copying tool.

When local file copying just omit host: on both arguments.


Table of Contents

ARGUMENTS

Normal Options
	-a, --archive 				# like -r but also preserves folder and file settings
	-b, --backup 				# backup used with --backup-dir
		--backup-dir="backup $(date+\%Y-\%m-\%d)"  # name of backupfile
	-c, --checksum 				# skip files based on checksum instead of mtime and size.
	-d, --dirs 					# transfer directories without recursing.
	-e, --rsh=COMMAND 			# specify remote shell to use.
		--rsync-path=PROGRAM 	# specify the rsync to run on a remote machine
		--existing				# skip creating new files on receiver.
		--ignore-existing		# skip updating files that exist on receiver.
		--remove-source-files	# sender removes sync'd files (non-dir).
		--del 					# alias for --delete-during.
		--delete 				# delete extraneous files from dest dirs.
		--delete-before 		# receiver deletes before transferring (default).
		--delete-during 		# receiver deletes during transfer.
		--delete-after			# receiver deletes after transfer.
		--delete-excluded 		# also delete excluded files from dest dirs.
		--delete-errors			# delete even if there are I/O errors.
		--force					# force deletion of dirs even if not empty.
		--max-delete=NUM		# do not delete more than NUM files.
		--partial				# keep partially transferred files. (To cont later use --append)
		--partial-dir=DIR 		# put a partially transferred file into a folder (This is much better than just --partial)
		--delay-updates			# put all updated files into place at end.
	-g, --group 				# preserve group.
	-h, --human-readable		# output numbers in a format a human can read.
	-i, --itemize-changes		# output a change summary for all updates.
	-m, --prune-empty-dirs 		# prunes empty folders.  Folders that have content are not copied.
	-n, --dry-run 				# dry run.
	-o, --owner					# preserve owner (super user only).
	-p, --perms 				# preserve permissions.
		--executability			# preserve executability.
	-r, --recursive 	 		# recurse into directories.
	-t, --times 				# timestamp, retain original creation and modification data.
	-u, --update 				# skip files that are newer on the receiver.
        --inplace               # update destination files in-place
        --append                # append data onto partial files. (implies --inplace)
	-v 							# verbose, use more v's when trouble shooting.
	-x, --one-file-system		# do not cross filesystem boundaries.
	-z 							# use compression during data transfer.  Used a lot for remote systems.
	-4, --ipv4					# prefer IPv4
	-6, --ipv6					# prefer IPv6


	-E, --extended-attributes	# copy extended attributes, resource forks.
	-H, --hard-links 			# link together corresponding files on the receiving side.
	-I, --ignore-times			# do not skip files that match size and time.
	-K, --keep-dirlinks			# keeps symlinks but folder names may change.
	-O, --omit-dir-times		# omit directories when preserving times.
		--super					# receiver attempts super user activities.
	-P	--progress --partial	# creates partial files and shows progress
	-R, --relative 				# use relative path names, preserves full path on backups. 				
	-S, --sparse				# try to handle sparse files efficiently, conflicts with --inplace.
	-W, --whole-file			# send file as is, do not use rsync algorithm.

Run As A Daemon
		--daemon 				# run as an rsync daemon.
		--address=ADDRESS 		# bind to the specified address.
		--bwlimit=KBPS			# limit I/O bandwidth.
		--config=FILE 			# specify alternate rsyncd.conf file.
		--no-detach				# do not detach from parent.
		--port=PORT 			# listen on alternate port number.
		--log-file=FILE 		# override the "log file" setting
		--sockopts=OPTIONS 		# specify custom TCP options
	-v, --verbose 				# increase verbosity.
	-4, --ipv4 					# prefer IPv4
	-6, --ipv6 					# prefer IPv6

LOCALLY (NO SSH)

``sh rsync orig_dir/* backup/ # given both on desktop, essentially acts like an advanced copy but able to keep syncing orig to backup. mkdir orig_dir/more_pics/ # added a subdir, but wasnt included in backup. rsync -r orig_dir/ backup/ # this will include subdir and files so * no longer needed. rsync -r orig_dir backup/ # leaving off / causes orig_dir itself to be placed in backup. -a –archive # better to use as it includes -r plus preserves folder & file settings -av –dry-run # need to use dryrun with -v in order to see anything rsync -av –delete –dry-run orig_dir/ backup/ # WARNING delete will mirror the directories. In this case backup will become exactly as orig_dir is by removing files in backup that orig_dir does not have and add all that is in orig_dir.


### REMOTE (SSH) {#remote}

```sh
-z    # when using remote you may use -z to compress as well as:
-P    # will show the progress.
rsync -zaP ~Projects/website remoteuser@ip:~/public/    # sync local machine to remote machine.
# rsync -zaP ~source|origin sshremote@ip:remote_dir|backup
# make special note of no tail on origin, this means entire copy of orig_dir will be saved into remote.
rsync -zaP remoteuser@ip:~/public/ ~Projects/website    # sync backup (website) from server to local machine.

BACKUP

ZIP (NOT SAME AS -z)

This command will create backup as a zip file and then send over the network as a zip file.

zip /zippedfiles/archive.zip /Dir1/ && rsync -av --delete /zippedfiles/ /Dir2/

FILES THAT SHOULD/NT BE BACKED UP

BACKUP DONT BACKUP
/usr/local /var/tmp
/opt /var/run/
/root /var/lock/
/var/www /var/spool/
/var/backups/ /var/cache
/var/lib/choose /

Sample Backup

sudo rsync -aAXv --delete --exclude=/dev/* --exclude=/proc/* --exclude=/sys/* --exclude=/tmp/* --exclude=/run/* --exclude=/mnt/* --exclude=/media/* --exclude="swapfile" --exclude="lost+found" --exclude=".cache" --exclude="Downloads" --exclude=".ecryptfs" /source /destination

Backup to External Drive

rsync -av --partial-dir --delete --exclude="DS_Store" --progress /Volumes/media/TV\ Shows /Volumes/SamsungT7/TV\ Shows/
rsync -av --partial-dir --delete --exclude="DS_Store" --progress /Volumes/media/Movies/ /Volumes/SamsungT7/Movies/

Full backup of system root directory.

rsync -avAXHS --progress --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} / /path/to/backup/folder

Exclusions can be united but then you have to cd / to run it.

--exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found}

Sample Restore

sudo rsync -aAXv --delete --exclude="lost+found" /backup /system