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