rsync is a very useful tool when it comes to backing up multiple files from remote or local targets. Below are a few nice examples you can use.
To backup the whole contents of your /home directory to let’s say a usb disk, use the following.
rsync -av /home /media/usb_disk
Note that this will create a directory to the path /media/usb_disk/home
If you want everything under /home directory to be copied directly to /media/usb_disk (without the “home” directory under the disk) you can use this,
rsync -av /home/ /media/usb_disk
This is useful when you’re planning to move your /home to a new mount point. So the trailing / means “copy the contents only, not the directory itself”.
In these commands, the -a argument is very important, it means archive mode and will preserve everything recursively including symbolic links, devices, attributes, permissions, ownerships etc.
To copy some content to a remote machine with ssh access,
rsync -av -e ssh /home some_user@192.168.2.5:/backups
To make the transfer with compression on, use the -z argument.
rsync -avz -e ssh /home some_user@192.168.2.5:/backups
You can copy files from remote server to your local, and use the progress argument to see the status of the transfer,
rsync -avz --progress -e ssh some_user@192.168.2.5:/backups /path/to/local/directory
You can delete the files in the target that are not present in the source,
rysnc -avz --delete -e ssh some_user@192.168.2.5:/backups /path/to/local/directory
Or you can sync only current files, and NOT copy new ones,
rysnc -avz --existing -e ssh some_user@192.168.2.5:/backups /path/to/local/directory
You can tell rsync to only include some files and excluse some others,
rsync -avz --include '*.conf' --exclude '*' --existing -e ssh some_user@192.168.2.5:/backups /path/to/local/directory
The above example will only update .conf files accross the backup server and local one, without writing new files.
You can limit the file transfer size so that you can transfer big files,
rsync -avz --max-size='5M' -e ssh some_user@192.168.2.5:/backups /path/to/local/directory
The best part of rsync is that in only transfers the part of the files that have been altered. To achieve this it uses md5checksums which can cost you some CPU time. If you don’t have bandwidth problems but want to offload CPU usage while syncing your files, you can tell rsync to copy the WHOLE file and skip the md5 checking.
rsync -avzW -e ssh some_user@192.168.2.5:/backups /path/to/local/directory
Sometimes the path of rsync on the remote server might be different, in these cases we should specify it. Below is a very universal format of the rsync command.
rsync -arzgopv some_user@192.168.2.5:/backups /path/to/local/directory --rsync-path=/usr/bin/rsync