Backing up your files is one of the more important and commonly performed tasks of a system administrator. There are many tools and methods you can use for performing backups, and today we will be looking at working with rsync. Rsync, as its name implies, is a tool designed to synchronize files between directories. These may be local directories or remote directories, allowing rsync to copy files directly between one server and another. Due to its design to synchronize files, it does more than simply copy the contents of a directory to another. It will save time and bandwidth by only duplicating the differences between the source directory and the destination, including deleting files from the destination that don’t exist in the source.
What is Rsync?
Rsync is one of those tools that come as standard with most Linux distributions. If it isn’t installed on your system, you can use the following commands to install it on Debian or Ubuntu systems:
sudo apt-get update
sudo apt-get install rsync
For CentOS and Red Hat systems, this command can be used:
sudo yum install rsync
One of Linux’s strengths is that a lot of useful tools are included as standard. One of those tools is rsync. On the surface rsync is a tool for copying files from one directory to another, under the hood it benefits from some clever design. First, it only copies files that have changed since the last backup.
Once installed, rsync uses the following command structure:
rsync <options> <source> <destination>
Commonly Used Options
There is a wide variety of options available for rsync, and the man pages are well worth consulting for a guide to them all. Let’s look at a handful of commonly used options::
* -r recursive, synchronizes the contents of subdirectories within the directory being synchronized.
* -l links, copy symlinks as symlinks rather than creating them as a directory and transferring the files within
* -p perms, transfers the permissions settings of the files
* -t preserve modification times
* -o preserve user ownership
* -g preserve group ownership
* -a archive, this applies all the previous settings with a single option, which will probably be your most used one.
* -E preserves file execution settings
* -z compress data during file transfer to save bandwidth
* –delete delete files from the destination that do not exist in the source
* –progress displays the progress of the transfer on screen
* -e Allows the use of an external shell with the transfer, such as ssh
When making backups, normally the -a flag is used to preserve the user information as well as the permissions, and the -z flag is used to reduce bandwidth use when sending the data between servers. If you are manually syncing, the –progress flag is nice to watch but pointless for automated transfers. If you are keeping a pure copy of the original files, then the –delete flag would be used. However, if you want to recover accidentally deleted files, then it would be logical to omit this flag when making backups.
Simple Copy Operations
So let’s look at some simple copy operations using rsync:
rsync -a /var/www/mysite /backups/websites
This first command would synchronize the “/var/www/mysite” directory into the “/backups/websites” directory, creating “mysite” directory there if it doesn’t already exist.
rsync -az /var/www/mysite email@example.com:/backups/websites
This steps things up a notch, and synchronizes the “/var/www/mysite” directory to another server in the “/backups/websites” directory. It’s worth noting that that when we are connecting to a remote server we separate the server information from the path with a colon (:). The server information is passed in the form of “firstname.lastname@example.org“, if you don’t have a domain name pointed at the server then the domain name can be replaced with the server’s IP. The username is also an optional field and can be omitted along with the at symbol (@) if you are connecting to the server with the same username as the current user on the server running the command. When connecting to a remote server you will be prompted for the remote user’s password or key information to connect.
rsync -az email@example.com:/var/www/mysite /backups/websites
This example is much the same as the previous one, but shows the source of the file transfer can be the remote site. So in this case, the files are synchronized from the “/var/www/mysite” directory on the web server to the “/backups/websites” directory on the local machine.
This article was brought to you by VPS.net, for dedicated server hosting, cloud servers and 24/7 support visit our site here vps.net