Use rsync for expert backup

Source: Internet
Author: User
Use rsync for expert-level backup-Linux Enterprise Application-Linux server application information. The following is a detailed description. In the past two months, I have been exhausted. In the past two months, my computer crashed. Without rsync, I would be in a lot of trouble. Fortunately, I often use this tool, and my data (in fact most of the data) has been copied to other places for use at any time. It may take a while to get familiar with rsync, but once you get familiar with rsync, you can use a very short script to take control of most backup tasks.

What are the highlights of rsync? First, it is designed to transfer only different parts of a file, rather than the entire file, to accelerate file transmission. For example, if I am writing this article, I can use rsync to copy it once and then transmit it again. Second (third, fourth ......) When the Parent and Child files are stored, rsync only transmits different parts of the files. This greatly saves time, especially when copying the entire directory during routine backup. The first copy will take a long time, and the next transmission will be much less (if you do not significantly modify the contents of the Directory every day ).

Another advantage of rsync is that it can retain the ownership and access permission information of files and copy symbolic links. In short, rsync is designed to control your files smartly.

To install rsync, you do not need to do anything. Generally, Linux distro should install rsync by default. If not, you should be able to find and install it in your distro Software Package repository. If you want to copy data to a remote system, rsync is required for both machines.

When you use rsync to copy files to another host, rsync usually uses a remote shell, such as ssh or rsh. In the following example, we will only use ssh for security considerations. Of course, we can also access remote hosts that use rsync daemon. However, since ssh is almost ubiquitous now, we do not have to make effort to use a daemon.

Rsync
The basic syntax of rsync is very simple-you only need to run rsync [options] source destination to copy the file from source to destination.

So, for example, if you want to copy the files in your directory to a USB hard disk, you can do this: rsync-a/home/user/dir // media/disk/dir /. For rsync, "/home/user/dir/" and "/home/usr/dir" are completely different. Without the last slash, rsync copies the entire directory. With the last slash, rsync only copies the contents of the directory without creating the directory itself. If you want to copy a directory structure, you should remove the ending slash, such as when the image/var/www is sent to another machine or similar situations.

In this example, I added the archive switch (-a), which is equivalent to using several rsync switches at the same time. It includes the recursive and copy symbol link switch, the retention group and the owner attribute switch. In short, rsync applies to archive copies. Note that-a does not retain the hard link. If you need it, you should also add the hard link switch (-H ).

Another common switch is verbose (-v), which enables rsync to report more information. You can also use two or three-v switches ----v to provide some information,-vv will give more information, and-vvv will give all the information.

Without special options, rsync copies hidden files (Files starting with.) together. If you do not want to copy the hidden file, you should add the switch -- exclude = ".*/". You can also use -- exclude to prevent copying vim swap files (. swp) or automatic backup files (. bak) of some other programs ).

Local copy
Suppose you have an external USB or 1394 hard drive, and you want to copy the home directory to the external drive. A good method is to store important data in a top-level directory, and then copy it to the backup directory of the external hard disk. The command is as follows:

Rsync-avh/home/usr/dir // media/disk/backup/

If you want to ensure that the locally deleted files are also deleted from the external hard disk, you need to use the -- deleted switch, as shown below:

Rsync-avh -- delete/home/user/dir // media/disk/backup

Use this switch with caution. This switch may allow you to delete a batch of innocent files unconsciously. In fact, before you use rsync, it may be a good idea to use the -- dry-run switch to see what operations you want to perform without actually Synchronizing files. If you have started an rsync transfer and suddenly realize that this command may cause data loss, you should immediately use Ctrl-C to stop the execution. Some files may have been lost, but you still have a chance to save the rest.

Remote copy
Want to copy the file to the remote host? No problem-all you need to do is add the host and user information. For example, if you want to copy the same directory to a remote host, you should use:

Rsync-avhe ssh -- delete/home/user/dir/user@remote.host.com: dir/

If you want to know the speed of file transmission and how many copies are required, you can add the -- progress switch:

Rsync -- progress-avhe ssh -- delete/home/user/dir/user@remote.host.com: dir/

If you do not want to be prompted to enter the password every time you use rsync, make sure that you use the SSH Public Key instead of the password. To do this, you should use ssh-keygen-t dsa and press enter in the password prompt to generate a public key on the local machine. Then, copy the public key to the remote host using the ssh-copy-id-I. ssh/id_dsa.pub user@remote.host.com.

If you want to retrieve some previous backup files from the remote host, you can use the following command:

Rsync-avze ssh remote.host.com:/home/user/dir // local/path/

The z switch compresses data during transmission. If the copied file already exists locally, rsync will not transfer any more-this is no different from transferring files locally to a remote host.

Encapsulate with a script
Once you point out which directories need to be synchronized and determine the commands for Synchronizing files, you can easily encapsulate these commands in a simple script. This is a simple example:

Rsync -- progress-avze ssh -- delete/home/user/bin/user@remote.host.com: bin/rsync -- progress-avze ssh -- delete/home/user/local/data/user@remote.host.com: local/data/rsync -- progress-avze ssh -- delete/home/user /. tomboy/user@remote.host.com :/. tomboy/

If you are running rsync in interactive mode, you can use the -- progress switch. This switch is not required if it is not an interactive mode.

If you look at the man page of rsync, it may be confused. However, when you have experience with rsync, you will find it not difficult to set up rsync tasks. It will prepare for hard disk faults and data losses that may occur one day.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.