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.