Rsync examples in Linux - how to take backups Introduction
In this tutorial we will see some of the most common and advance rsync examples step by step. Rsync command is used to take backups, copy files and directories remotely or locally in Linux and UNIX. It is the best tool to synchronize your files and directories from one computer to another or from one server to another. It is very fast and secure. With it you can use ssh, which encrypts your data for secure transfer.
In Windows you need to install Windows rsync client and server to perform backup between Linux and Windows.
There are number of options available which you can use with rsync to increase its efficiency. Some of the most common rsync options are as follows.
z = Compression mode v = Verbose mode r = Recursive mode h = Human readable mode a = Archive mode
To use rsync you have to install rsync in your Linux machine. First install rsync tool in your system,
For installation in Debian based system and Red hat based system use the rsync commands given below.
Debian based system
# apt-get install rsync
Red hat based system
# yum install rsync
This below rsync examples is the general rsynx syntax that you should follow when using the command.
# rsync options <source> <destination>
# rsync -zv /opt/zimbra firstname.lastname@example.org:/home/backup/
The option '-z' is used to compress files before transfer and decompresses it when the transfer is done.
The transfer time is reduced if you use rsync compression method while transferring over the internet. If you are using a very slow connection, rsync compression is advisable, otherwise it will slow things down.
# rsync -z /home/directory/ email@example.com:/root/directory2/
Rsync examples to copy files or directory locally
Sync your file or in other word copy your files to other directory. Use options '-z', '-v', '-h' to compress, see verbosely, and easily readable format respectively.
# rsync -zvh file1.txt /Directory1/
Rsync from one server to another
You can sync or copy files and directory from local computer to a remote server by the following ways. This is called rsync push because you are pushing files from local to remote.
# rsync -zvh file1 firstname.lastname@example.org:/Directory2/
Rsync from remote to local
Sometimes you may have to sync a file from your remote server to your local machine. In other words using rsync to pull files from remote to a local computer See the rsync examples given below for synchronizing files from remote server to your local machine.
# rsync -zvh email@example.com:/Directory1/file1 /Directory2/Application/
Rsync over SSH
Rsync over ssh is always a better idea when you are transferring files over the network, Use rsync command with ssh to encrypt and transfer your files securely.
# rsync -zvh ssh firstname.lastname@example.org:/Directory1/file1 /Directory2/
Learn more about passwordless rsync over ssh login.
Rsync exclude & rsync include
Rsync exclude and rsync include options can be used to transfer only selected files. For example If there are many files whose names are starting from 'S' and you want to include them all but at the same time you want the rest of the files to be excluded while taking the backup, you need to use include and exclude with rsync command. Some rsync exclude example is here.
# rsync -zvrh ssh --include 'S*' --exclude '*' /Directory1/ email@example.com:/Home/
Rsync max size
Set the max file size you want to transfer by using rsync max size combination. See the example.
# rsync -zvrh ssh --max-size='5M' /Directory1/ firstname.lastname@example.org:/Dir/
When transferring data you would like to see how much data got transferred and how much is still left, use the option 'progress' to track the data transfer progress.
# rsync -zvrh ssh --progress /Directory1/ email@example.com:/
Rsync delete source
Use rsync to delete files not in source. Means, files which exists at the target but not at the source, you may want to delete those files from the target. The option 'delete' is used for that.
# rsync -zvrh --delete firstname.lastname@example.org:/root/Directory2/ /Directory1/
Rsync delete after copy
You can use rsync to delete source file after copy. The below example will clarify how to automatically delete the source file when the transfer is over with the 'remove-source-files' option.
# rsync -zvrh --remove-source-files /Directory1/ email@example.com:/Directory2/ sending incremental file list file1 file2 sent 121 bytes received 50 bytes 342.00 bytes/sec
Rsync preserve timestamp
While taking backups you would like to preserve the file permissions, groups and ownership, timestamps, its symbolic links etc. use the option '-a' with rsync to preserve timestamp. See the example below.
# rsync -azvrh file1 firstname.lastname@example.org:/root/Directory2/
Rsync specify port
It is possible to specify different ssh port while using rsync command. You need to use option '-e' to specify port See the below rsync example.
# rsync -azv -e 'ssh -p 2000' file1 email@example.com:/root/Directory2/
Rsync command for update
Rsync can overwrite files at the destination by using the option -u or --update . You can update the older files with a newer one by using this option.
# rsync -av --update /home/Directory1 firstname.lastname@example.org:/root/Directory2/
Rsync dry run
Rsync dry run is a very good option beginner who doesn't know much about this command and want to test what would be the output of the command before actually runs it on the terminal. Using the 'dry-run' option lets you analyze the output before you try to run the command. When you are confident about the output of the command, you can remove the dry-run to actually run the command.
# rsync -azvrh --dry-run file1 email@example.com:/root/Directory2/
Rsync examples of scheduling backup
Always make a schedule backup for your important files. You can use rsync with crontab for schedule backups. The below script will illustrate how to make rsync script to take schedule backup of files. The below script will work as follows.
Backup everyday at 6:00 PM.
Backup every saturday at 7:00 PM.
Full backup on 1st of every month 8:00 AM
# crontab e
00 18 * * * rsync –av --delete /home/directory /home/everyday 00 19 * * 6 rsync –av --delete /home/everyday /home/everyweek 00 8 1 * * tar -cvf /home/everymonth_$(date +%Y%m%d).tar /home/everyday/
Image credit: kenjoey of flickr
This is it with the tutorial Rsync examples in Linux - how to take backups.