Linux in rsync asynchronous server realizes file synchronization and backup

Source: Internet
Author: User
Tags ssh rsync

Rsync is ideal for quickly synchronizing large, complex directories between two machines, such as the forum's attachment directory. In conjunction with SSH, the security is guaranteed, and you can use SSH public key and cron for automatic timing synchronization.

Description: The two machines are localhost and remotehost respectively, and the users are LocalUser and RemoteUser respectively.

Environment: FreeBSD 4.9 and FreeBSD 6.1

The code is as follows

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

Sign in to end

The code is as follows
$cd. ssh/; Cat Localuser_id_dsa.pub >> Authorized_keys

At this point, set up SSH authentication completed.

Set up rsync

Confirm that both ends of the machine are installed rsync, FreeBSD has ports, installation is very convenient.

Write a foot name for backup.sh, which reads as follows:

The code is as follows
#!/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

The-a option is equivalent to option-rlptgod. In simple terms, this option can be recursive to synchronize almost everything in the past, very useful. Note that-A does not save hardlinks by default, but it can be implemented separately with the-H option.

The-Z option compresses files in transit, which undoubtedly speeds up synchronization.

The-delete option deletes files that should not exist for the recipient, which has been deleted by the sender, which keeps the directory fully synchronized.

Let Cron run this script every 1 o'clock in the morning.

The code is as follows
$crontab-E
0 1 * * */home/localuser/bin/backup.sh

Friendly Tips

Rsync is not automatically start sync function, if we are scheduled to back up a site data we need to use the Timer function, the above example to use the Linux $crontab command to perform the backup data script.

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.