We have already seen how to create large files, now let’s see how to backup files using rsync. But before we see rsync command examples, let’s see what exactly is rsync and its benefits.

What is rsync?
Rsync is a Linux tool that helps in syncing a file from server A
to server B
and vice versa. Rsync is a powerful tool with numerous options and we will see some examples below. The best part about rsync is that it only syncs the changes. This will saves a lot of time and resources.
1. Rsync syntax
Here is the syntax which needs to be used. The basic you need to remember is rsync origin destination
$ rsync [option] [email protected]:[file/directory origin] [destination]
The real command will look something like this.
$ rsync -avzh /home/user/backups/ [email protected]:/home/backup/
So above command will copy the contents from the backup folder to the remote destination.
if you don’t put trailing / in the /home/user/backups it will copy the whole folder instead of just the contents in it.
Further to explain the above command
-a stands for copy files recursively and preserve ownership of files when files are copied
-v stands for runs the verbose version of the command; all of the processes that are run will be written out for the user to read
-z stands for compress the data synced
– h stands for produce easily readable output, not just numbers
2. Show Progress
If you are doing large transfers and want to see progress, you can use the --progress
option.
$ rsync -avzh --progress /home/user/backups/ [email protected]:/home/backup/
3. Exclude Files/folders from syncing.
Create a file called exclude.txt
and add all the files and folders you want to exclude.
example exclude.txt
test/file.txt justgeek/files/privacy justgeek/chess
$ rsync -avzh --exclude-from 'exclude.txt' sync/ 192.168.0.151:/home/
4. Copying data from remote servers
As you can copy data from your server to a remote server, you can also copy data from a remote server to your local machine.
$ rsync -avzh [email protected]:/root/rpmpkgs /tmp/myrpms
5. Delete files
So when you sync the files using rsync, it stays forever. Even if you delete files from the source folder and then again run rsync, it won’t delete files from the destination. However, if you want both folders to be exactly the same with no extra file on the destination you can use --delete
option.
The below command will delete the file from the destination folder if you delete it from the source.
rsync -avzh --delete [email protected]:/var/lib/rpm/ /root/rpm
6. Removing source files
If you have a server, where you want to remove the files after they are backed, then you can use the option --remove-source-files
The below command will remove files from the source folder after they are rsync’d
$ rsync --remove-source-files -zvh backup.tar.gz [email protected]:/tmp/bak
7. Dry run
If you want to see what your command is actually doing, without making any real changes then you use --dry-run
$ rsync --dry-run --remove-source-files -zvh bak.gz [email protected]:/tmp/bak
I hope you have liked the post on rsync command examples, do not forget to share it with your colleagues.