rsync:是类unix系统下的数据镜像和快速增量备份的工具(remote sync).远程同步支持本地复制,或
者与其他SSH、rsync主机同步,但不能做到实时同步.
Inotify:是监控文件系统操作,比如读取,写入和创建.Inotify反应灵敏,用法非常简单,并且比cron任务
的繁忙轮询高效得多.
Unison:是一款跨windows/linux/MAC OS平台的文件同步工具,不仅支持本地对本地同步,也支持通过
SSH、RSH和Socket等网络协议进行同步.更棒的是Unison支持双向同步操作,你既可以从A同步到
B,也可以从B同步到A,这些都不需要额外的设定.
环境规划:
IP 主机名
192.168.1.247 tong1
192.168.1.248 tong2
一.利用rsync+ssh工具对文件同步(用计划任务定时同步)
步骤:
(1)在文件服务端和文件镜像端都要ssh互信
(2)在文件服务端安装rsync软件
(3)在文件镜像端也要安装rsync软件
1.下载安装rsync软件和ssh互相信任
[root@tong1 ~]# hostname
tong1
[root@tong1 ~]# cat /etc/hosts
192.168.1.247 tong1
192.168.1.248 tong2
[root@tong1 ~]# wget https://download.samba.org/pub/rsync/src/rsync-3.1.1.tar.gz
[root@tong1 ~]# tar xvf rsync-3.1.1.tar.gz
[root@tong1 ~]# cd rsync-3.1.1
[root@tong1 rsync-3.1.1]# ./configure --prefix=/usr/local/rsync-3.1.1
[root@tong1 rsync-3.1.1]# make && make install
[root@tong1 rsync-3.1.1]# echo $?
0
[root@tong1 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
79:cb:ca:73:40:9e:5c:2b:46:7f:67:b1:b2:35:8a:91 root@tong1
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| |
| o.. . |
| =S+.o o |
| BoE.o * |
| . oo+ B . |
| ...o o |
| oo |
+-----------------+
[root@tong1 ~]# scp /root/.ssh/id_rsa.pub tong2:/root/.ssh/authorized
root@tong2's password:
id_rsa.pub 100% 392 0.4KB/s 00:00
[root@tong1 ~]#
tong2节点:
[root@tong2 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
79:cb:ca:73:40:9e:5c:2b:46:7f:67:b1:b2:35:8a:91 root@tong2
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| |
| o.. . |
| =S+.o o |
| BoE.o * |
| . oo+ B . |
| ...o o |
| oo |
+-----------------+
[root@tong2 ~]# cd .ssh/
[root@tong2 .ssh]# cat id_rsa.pub >> authorized_keys
[root@tong2 .ssh]# scp authorized_keys 192.168.1.247:/root/.ssh/
[root@tong2 .ssh]# ssh tong1 date
Wed May 6 11:28:19 CST 2015
[root@tong2 .ssh]# ssh tong2 date
Wed May 6 11:31:24 CST 2015
[root@tong2 .ssh]#
2.编辑配置文件和启动服务
tong1节点:
[root@tong1 .ssh]# cd /etc/
[root@tong1 etc]# vim rsyncd.conf
uid=root --rsync守护进程的用户
gid=root
use chroot=no --不能跳到上级目录
max_connect=4 --允许4个客户端连接
strict modes=yes --脚本模式
port=873 --监听端口
[backup]
path=/home/rsync --服务根目录
comment=This is a test --描述信息
ignore errors --忽略IO错误
read only=yes --只读
list=no --不能列出文件
auth users=hening --认证用户名
secrets file=/etc/rsync.pas --密钥文件
hosts allow=192.168.1.248 --允许哪个IP访问
pid file=/var/run/rsync.pid --PID文件
lock file=/var/run/rsync.lock
log file=/var/log/rsync.log --日志文件
[root@tong1 etc]# vim rsync.pas --密钥文件内容
hening:111111 --用户名和密码(这个用户名不是系统用户名,是rsync客户端登陆时用的)
[root@tong1 etc]# chmod 600 /etc/rsync.pas
[root@tong1 etc]# chown root:root /etc/rsync.pas
[root@tong1 etc]# vim rsync.motd --登陆后的提示信息
Welcome to use the rsync services!
[root@tong1 etc]# mkdir /home/rsync/{1,2} -pv
[root@tong1 etc]# touch /home/rsync/1.txt
[root@tong1 etc]# /usr/local/rsync-3.1.1/bin/rsync --daemon
[root@tong1 etc]# netstat -antup |grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 3352/rsync
tcp 0 0 :::873 :::* LISTEN 3352/rsync
[root@tong1 etc]# vim /etc/rc.local
/usr/local/rsync-3.1.1/bin/rsync --daemon
[root@tong1 etc]#
3.配置rsync客户端
tong2节点:
[root@tong2 ~]# mkdir /home/rsync
[root@tong2 ~]# vim /etc/rsync.pas
111111 --只写密码就可以了
[root@tong2 ~]# chmod 600 /etc/rsync.pas
[root@tong2 ~]# chown root:root /etc/rsync.pas
[root@tong2 rsync]# /usr/local/rsync-3.1.1/bin/rsync -vzrtopg --progress --delete hening@192.168.1.247::backup /home/rsync --password-file=/etc/rsync.pas --测试同步文件
receiving incremental file list
./
1/
2/
1.txt
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=2/4)
sent 48 bytes received 155 bytes 135.33 bytes/sec
total size is 0 speedup is 0.00
[root@tong2 rsync]# ll /home/rsync/
total 8
drwxr-xr-x. 2 root root 4096 May 6 11:58 1
-rw-r--r--. 1 root root 0 May 6 13:08 1.txt
drwxr-xr-x. 2 root root 4096 May 6 11:58 2
[root@tong2 ~]# vim /usr/local/rsync-3.1.1/bin/rsync.sh --将命令保存为脚本同步文件
#!/bin/bash
/usr/local/rsync-3.1.1/bin/rsync -vzrtopg --progress --delete hening@192.168.1.247::backup /home/rsync --password-file=/etc/rsync.pas
[root@tong2 ~]# crontab -e
*/1 * * * * /usr/local/rsync-3.1.1/bin/rsync.sh >/dev/null 2>&1 --每一分钟同步一次
[root@tong2 ~]#
rsync命令参数详解:
-v 表示显示verbose详细显示
-z 表示压缩文件
-r 表示递归
-t 保持原文件创建的时间
-o 保持原文件的属主
-g 保持原文件属组
-p 保持原文件的参数
-a 存档模式
--progress 详细信息进度情况
--delete 服务器端删除文件时,客户端也删除
--password-file 密码文件
hening 在服务端指定的用户,不是系统用户(auth users=hening)
192.168.1.247 服务端的IP地址
::backup 服务端配置文件的模块([backup])
0 0