File synchronization replication under Linux
1. Mode of adoption
Create a temporary storage directory on a single machine, store files, from this temporary directory to multiple
Server to transfer files. For the temporary directory after the transfer of files, completely deleted.
/root/tempplay: Temporary directory
/root/play: Play file storage directory
and assume the IP address as shown in the figure.
|----------------------------|
| Machine A (192.168.0.1) |
|----------------------------|
|/root/tempplay |
|----------------------------|
|/root/play |
|----------------------------|
|-----------------------------|
| machine B1 (192.168.0.2) |
|-----------------------------|
|/root/play |
|-----------------------------|
The directory structure of the machine b2,b3 and B1 is consistent.
The machine A in the figure is our data source machine, the/root/tempplay directory on the source machine is
Our temp directory, we need to transfer files from/root/tempplay to the/root/play directory of all machines. /
2. Steps
For ease of operation, all the root users are tested on Red Linux.
2.1 Temporary Directory Establishment
On the A
Mkdir/root/tempplay
2.2 Setting up the play directory
On all the machines
Mkdir/root/play
2.3 Get the certification, generate the key
On the A
CD ~/.ssh
#产生公钥文件 (id_dsa.pub) and private key file (ID_DSA), type DSA, length 1024 bits
#注意询问passphrase的时候直接回车
SSH-KEYGEN-T dsa-b 1024
#将公钥 (id_dsa.pub) copied to the remote b1,b2,b3 three servers, named Id_dsa.pub.client
2.4 Confirmation Certificate
Perform the following action on the B1,B2,B3
CD ~/.ssh
#将Client的公钥放入Server的信任列表
Cat Id_dsa.pub.client >> Authorized_keys
#更新权限, it's important.
chmod $HOME/.ssh
chmod $HOME/.ssh/authorized_keys
chmod g-w $HOME $HOME/.ssh $HOME/.ssh/authorized_keys
2.5 Write the Copy file script file synchfile.sh, the contents are as follows:
#bin/bash
# copy suffix name is mp4 file
Scp-p–q/root/tempplay/*.mp4 Root/play/
Scp-p–q/root/tempplay/*.mp4 Root@192.168.0.2:/root/play/
Scp-p–q/root/tempplay/*.mp4 Root@192.168.0.3:/root/play/
Scp-p–q/root/tempplay/*.mp4 Root@192.168.0.4:/root/play/
# Delete temporary directory files after end
Rm–f–r/root/tempplay/*.mp4
2.6 Script Deployment
On a, copy files synchfile.sh to/root
chmod a+x/root/synchfile.sh
2.7 Regular execution
On A,
#生成定时执行文件/root/mycron,
Crontab-l >/root/mycron
Edit the file/root/mycron and add the following (for 15 minute execution)
*/15 * * * */root/synchfile.sh
#crontab command to configure timed execution synchfile.sh
Crontab/root/mycron–u Root
#重新引导
/etc/rc.d/init.d/crond restart
2.8 drawbacks
The above deployment does not guarantee you to copy files to the/root/tempplay directory while the foot
This is also done by copying files from the directory to the/root/play directory, which will conflict, causing the file to be incomplete. So when the copy script is executing, do not delegate to the/root/tempplay files that need to be replicated to other machines.
You can use the Ps–e–f |grep SCP command to see if the copy script is running.