Backup of Linux servers rsync

Source: Internet
Author: User
Tags switches rsync

Backup and restore server (Linux) files (in T) and database (MYSQL) due to job requirements. Initially, full backup and restore was achieved through code. The advantage of this is that the entire backup and restore process can be controlled to ensure data security. But the disadvantage is also obvious: low efficiency, long operation time, the server stored in a large amount of files (may reach 6-7t), can not be backed up. As more business requirements are raised and the functionality is improved, the full backup is changed to a two-machine incremental backup. Through the study of on-line materials, the better technology is learned: the synchronization of the database can be synchronized with the synchronization function of MySQL, and the synchronization of files between servers can be realized by using rsync.

MySQL synchronization in the article "MySQL synchronization" is described here, no longer repeat the MySQL synchronization configuration. The following will learn about the use of rsync.

The following original text is located at: http://www.linux.com/feature/117236

Author: Joe ' Zonker ' Brockmeier (July 17, 2007)

Translator: Wang Xu (July 19, 2007)

What are the outstanding advantages of rsync? First, it is designed to transfer only the different parts of the file, not the entire file, to speed up the file transfer. For example, I am writing this article, so I can now use rsync to replicate once, and then transfer again. The second time (third, fourth time ...) ), rsync transmits only the different parts of the file. This will greatly save time, especially when replicating the entire directory during routine backups. The first copy takes a long time, and the next transmission is much less (if you don't drastically modify the contents of the catalog every day).

Another benefit of rsync is that it preserves file ownership and access information and copies symbolic links, in short, it is designed to take control of your files smartly.

To install rsync, you shouldn't have to do anything---general Linux distro should install it by default. If not, you should be able to find and install it in your distro package repository. If you want to copy the data to a remote system, both machines need rsync.

When you use Rsync to copy files to another host, rsync typically works with a remote shell, such as SSH or rsh. In the example below, we will only use SSH for security reasons. Of course, we can also access remote hosts that use rsync daemon, but since SSH is almost ubiquitous now, there is no need for us to use a daemon.

Learn about Rsync

The basic syntax of rsync is very simple---as long as you run rsync [options] source destination, you can copy the file from source to destination.

So, for example, if you want to copy the files from your directory to the USB drive, you can do this: Rsync-a/home/user/dir//media/disk/dir/. It is important to emphasize that "/home/user/dir/" and "/home/usr/dir" are completely different for rsync. If there is no last slash, Rsync will copy the directory entirely, and with the last slash, Rsync will copy only the contents of the directory without creating the directory itself. If you want to copy a directory structure, you should get rid of the trailing slash, such as when mirroring/var/www to other machines or similar situations.

In this example, I added the archive switch (-a), which is actually equivalent to using several rsync switches at the same time. It includes a recursive and duplicate symbolic link switch, a retention group and a master attribute switch, in short, to make rsync suitable for archival copies. Note that the-a does not retain hard links, and you should also add a hard-link switch (-h) if you want.

Another common switch is verbose (-V), which will let rsync report more information. You can also use two or three-V switches----V will give some information,-VV will give more information, and-VVV will give all the information.

If there are no special options, rsync will even copy the hidden files (files starting with.). If you want to not copy hidden files, you should join the switch--exclude= ". * *". You can also use--exclude to prevent the automatic backup files (. bak) of the Vim Interchange file (. SWP) or some other programs from being copied.

Make a local copy

Suppose you have an external USB or 1394 hard drive, and you want to copy the home directory to an external drive. A good way to do this is to save the important data in a top-level directory, and then copy it to the external hard disk to back up the directory with the following command:

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

If you want to ensure that locally deleted files are also deleted on the external drive, you need to use the--deleted switch as follows:

RSYNC-AVH--delete/home/user/dir//media/disk/backup

Use this switch with care; This switch may allow you to delete a batch of innocent files in an unconscious situation. In fact, it might be a good idea to use the--dry-run switch to see what to do without actually synchronizing the files before you use Rsync. If you have already started a rsync transmission and suddenly realize that this command could result in data loss, you should use ctrl-c immediately to abort the execution. Some files may have been lost, but you still have a chance to keep the rest.

Make a remote copy

Want to copy files to a remote host? No problem---all you have to do is join 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/[email protected]:d ir/

If you want to know the speed of file transfer and how much more you need to replicate, you can add the--progress switch:

rsync--progress-avhe ssh--delete/home/user/dir/[email protected]:d ir/

If you do not want to be prompted to enter your password each time you use rsync, make sure you use the SSH public key mechanism instead of the password. To do this, you should use the Ssh-keygen-t DSA and press ENTER directly at the password prompt to generate the public key on this machine. After that, use ssh-copy-id-i. ssh/id_dsa.pub [email protected] to copy the public key to the remote host.

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

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

Where the z-switch compresses the data during transmission, rsync will no longer transfer---if you have a copy of the file that exists locally, and it is no different from transferring the files locally to the remote host.

Encapsulating with a script

Once you have pointed out which directories need to be synchronized and determined the commands to synchronize the files, you can easily encapsulate these commands in a simple script. This is a simple example:

rsync--progress-avze ssh--delete/home/user/bin/[email protected]te.host.com:bin/

rsync--progress-avze ssh--delete/home/user/local/data/[email protected]:local/data/

rsync--progress-avze ssh--delete/home/user/.tomboy/[email protected]:/.tomboy/

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

If you look at Rsync's man page, it might get confused. However, when you have some of the rsync experience, you'll find it's not difficult to set up the Rsync task, which will prepare you for a hard drive failure and data loss that may come a day.

Transferred from: http://blog.chinaunix.net/uid-20101092-id-1977328.html

Backup of Linux servers rsync

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.