Rsync

Copies files from a source to destination. Syncs directories without copying already existing files and updating modified files. Works over SSH.

Great online tool that generates rsync commands!

remote -> local:

rsync -rav --delete username@remote.host:/remote.dir /local.dir

local -> local:

rsync -rav --delete /local.dir_1 /local.dir_2

local -> remote:

rsync -rav --delete /local.dir username@remote.host:/remote.dir

Options/flags:

-a

equals to use of following options -rlptgoD

  • recursively copy files and directories
  • copy symlinks as symlinks
  • preserve permissions
  • preserve group
  • preserve modification time
  • preserve ownership

(basically -a options makes rsync a fully fledged backup tool)


–delete 

deletes files from the destination directory if they are removed from the source directory


-r or -recursive

syncs files and directories recursively


-v

Verbose output – gives more details


-u or -update

Will not overwrite target files if they have been changed since last sync


-q or –quiet

Suppresses message output


-P or –progress 

Shows detailed progress of the file transfer


-z

Compresses files which can reduce network load and speed up file transfer


-o or –owner

Preserves the owner

Sets the owner of the destination file to be the same as the source file, but only if the receiving rsync is being run as the superuser


-g or –group

Preserves the group

If the receiving program is not running as the super-user (or if –no-super was specified), only groups that the invoking user on the receiving side is a member of will be preserved


-l or –links

Copies symlinks as symlinks during the sync


-t

Updates existing files on target by sending only the difference in data


-e

Specifies a protocol name you want to use, “ssh” for example:

-e ssh

–include –exclude

Includes or excludes certain file types from syncing, example:

--include '*.jpg *.png' --exclude '*.gif'

Crontab example for rsync backup of local directory to remote server using ssh

15 03 * * * rsync -ravz --delete -e ssh /local.dir/ remote.user@remote.server:/remote.dir

Rsync over ssh using non standard port on remote host and sshpass to “forward” the password.

rsync -ravz -e 'sshpass -p 'remote.ssh.password' ssh -p remote.port.number' --progress --delete /local.dir/ remote.username@remote.ip.address:/remote.dir

Target computer has to be added to the list of known hosts (you can do this by simple ssh connection then you will be asked for optional addition)

Most of this info is based on: www.linuxtechi.com/rsync-command-examples-linux/

Leave a Comment

Tested on Ubuntu & Pop OS