Linux uses rsync to synchronize and back up files between servers

Source: Internet
Author: User
Rsync is suitable for fast synchronization of large and complex directories between two machines, such as the attachment directories of the Forum. With ssh, the security is ensured and can be used

Rsync is suitable for fast synchronization of large and complex directories between two machines, such as the attachment directories of the Forum. With ssh, the security is ensured, and automatic timing synchronization can be performed using ssh public key and cron.

Note: The two machines are localhost and remotehost, and the users are localuser and remoteuser respectively.
Environment: FreeBSD 4.9 and FreeBSD 6.1

Set ssh public key authentication
$ Ssh-keygen-t dsa-B 2048
Generate the required key
$ Scp/home/localuser/. ssh/id_dsa.pub remoteuser @ remotehost:/home/remoteuser/. ssh/localuser_id_dsa.pub
Copy the public key to remotehost
$ Ssh remoteuser @ remotehost

Log on to the terminal
$ Cd. ssh/; cat localuser_id_dsa.pub> authorized_keys

So far, ssh authentication has been set up.

Set rsync
Make sure that rsync is installed on both machines. freeBSD has ports, which is very convenient to install.
Write a script named backup. sh with the following content:
#! /Bin/sh
RSYNC =/usr/local/bin/rsync
SSH =/usr/bin/ssh
KEY =/home/localuser/. ssh/id_rsa
RUSER = remoteuser
RHOST = remotehost
RPATH =/remote/dir
LPATH =/this/dir
$ RSYNC-az-delte-e "$ SSH-I $ KEY" $ RUSER @ $ RHOST: $ RPATH $ LPATH

-Option a is equivalent to option-rlptgoD. To put it simply, this option is useful for recursion and synchronizing almost everything. Note that-a does not save hardlinks by default, but can be implemented by using the-H option separately.
-The z option compresses files during transmission, which undoubtedly speeds up synchronization.
-The delete option will delete some files that should not exist by the receiver. This file has been deleted by the sender, which will keep the directory completely synchronized.

Let cron run this script at every day
$ Crontab-e
0 1 ***/home/localuser/bin/backup. sh

Related Article

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.