Centos6系统利用rsync与ssh实现文件同步
Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。Rsync使用所谓的“Rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。
Rsync本来是用于替代rcp的一个工具,目前由rsync.samba.org维护,所以rsync.conf文件的格式类似于samba的主配置文件。Rsync可以通过rsh或ssh使用,也能以daemon模式去运行,在以daemon方式运行时Rsync server会打开一个873端口,等待客户端去连接。连接时,Rsync server会检查口令是否相符,若通过口令查核,则可以开始进行文件传输。第一次连通完成时,会把整份文件传输一次,以后则就只需进行增量备份。
Rsync支持大多数的类Unix系统,无论是Linux、Solaris还是BSD上都经过了良好的测试。此外,它在windows平台下也有相应的版本,如cwRsync和Sync2NAS等工具。
10.100.10.10(服务器端)
tar xvf rsync-patches-3.0.9.tar.gz
* base: mirrors.163.com
* extras: mirrors.163.com
* updates: mirrors.163.com
rsync.x86_64 3.0.6-9.el6 @base
Make && make install
ln -sv /usr/local/rsync/bin/rsync /usr/bin/rsync
ln -sv /usr/local/rsync/bin/rsync /usr/sbin/rsync
# Section 1: Global settings
max connections = 3
strict modes = yesi
pid file = /var/run/rsync/rsyncd.pid
log file = /var/log/rsync/rsyncd.log
lock file = /var/log/rsync/rsyncd.lock
path = /var/log/httpd/
ignore errors = yes
hosts allow = x.x.x.0/255.255.255.0 y.y.0.0/255.255.0.0 允许来同步数据的主机,多个用空格隔开
auth users = loguser
secrets file = /etc/rsync.passwd
path = /appsdata/apps/tomcat/logs
ignore errors = yes
hosts allow = x.x.x.0/255.255.255.0 y.y.0.0/255.255.0.0 允许来同步数据的主机,多个用空格隔开
auth users = loguser
secrets file = /etc/rsync.passwd
path = /appsdata/apps/nginx/logs
ignore errors = yes
hosts allow = x.x.x.0/255.255.255.0 y.y.0.0/255.255.0.0 允许来同步数据的主机,多个用空格隔开
auth users = loguser
secrets file = /etc/rsync.passwd
vim /etc/rsync.passwd
其中冒号前的是用户名,冒号后的是对应用户的密码。此文件不能为其他任意用户可访问,因此可用如下命令修改:
mkdir -pv /var/run/rsync/ /var/log/rsync /var/lock/rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 26635/rsync
tcp 0 0 :::873 :::* LISTEN 26635/rsync
1)、开启root用户ssh认证权限
vim /etc/ssh/sshd_config
PermitRootLogin yes 将其注释去掉
ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:
b5:02:63:6f:93:30:a0:f2:a6:e6:e0:a1:05:f3:23:79 root@idc010vm010
The key's randomart p_w_picpath is:
+--[ RSA 2048]----+
| . |
| . . |
|. . * . |
| o . * o . |
| B . o |
cat .ssh/id_rsa.pub >> authorized_keys
scp authorized_keys root@10.100.10.12:/root/.ssh 要在12服务器mkdir .ssh 之后进行,
ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:
5b:fd:ab:b1:bd:10:5a:8d:2d:f1:77:78:4b:1f:8b:2b root@idc010vm012
The key's randomart p_w_picpath is:
+--[ RSA 2048]----+
| . |
cat .ssh/id_rsa.pub >> authorized_keys
scp authorized_keys root@10.100.10.10:/root/.ssh 输入10机器的root用户密码即可。
Warning: Permanently added '10.100.10.10' (RSA) to the list of known hosts.
Last login: Wed Sep 12 15:17:03 2012 from idc010vm012.idc.lecast
ssh-keygen –t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/loguser/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/loguser/.ssh/id_rsa.
Your public key has been saved in /home/loguser/.ssh/id_rsa.pub.
The key fingerprint is:
5d:3c:e7:af:8c:5a:60:13:17:a9:87:e8:98:8b:9e:ed loguser@idc010vm010
The key's randomart p_w_picpath is:
| .. |
| ... |
| ..o+.+ |
| o .. o . |
| . . . .|
| .o. . o . |
| .o.E ... o |
cp /home/loguser/.ssh/id_rsa.pub /home/loguser/authorized_keys
scp /home/loguser/authorized_keys loguser@10.100.10.12:/home/loguser/.ssh/
chmod –R 600 .ssh/*
ssh-keygen –t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/loguser/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/loguser/.ssh/id_rsa.
Your public key has been saved in /home/loguser/.ssh/id_rsa.pub.
The key fingerprint is:
c4:95:5a:5a:89:93:34:45:e3:9f:d2:f4:79:86:89:4c loguser@idc010vm012
The key's randomart p_w_picpath is:
+--[ RSA 2048]----+
| .o==o |
| .++=. |
| o*. E |
| . o |
cp /home/loguser/.ssh/id_rsa.pub /home/loguser/authorized_keys
scp /home/loguser/authorized_keys loguser@10.100.10.12:/home/loguser/.ssh/
chmod –R 600 .ssh/*
Warning: Permanently added '10.100.10.10' (RSA) to the list of known hosts.
scp 10.100.10.10:/etc/rsyncd.conf ./ 放在家目录下,随后用root用户cp至/etc下。
Warning: Permanently added '10.100.10.10' (RSA) to the list of known hosts.
rsyncd.conf 100% 905 0.9KB/s 00:00
[loguser@idc010vm012 .ssh]$ rsync -vzrtopg --progress --stats -e ssh /opt/GeoDB.dat loguser@10.100.10.10:/tmp/cmake
Warning: Permanently added '10.100.10.10' (RSA) to the list of known hosts.
sending incremental file list
4333568 100% 11.52MB/s 0:00:00 (xfer#1, to-check=0/1)
Number of files: 1
Number of files transferred: 1
Total file size: 4333568 bytes
Total transferred file size: 4333568 bytes
Literal data: 4333568 bytes
Matched data: 0 bytes
File list size: 27
File list generation time: 0.001 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 554522
Total bytes received: 31
sent 554522 bytes received 31 bytes 369702.00 bytes/sec
total size is 4333568 speedup is 7.81
[root@idc010vm010 tmp]# ll cmake
四、Rsync的常用参数描述:
--backup-dir 将备份文件(如~filename)存放在在目录下。
-L, --copy-links 想对待常规文件一样处理软链结
--copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链结
-H, --hard-links 保留硬链结
-W, --whole-file 拷贝文件,不进行增量检测
-x, --one-file-system 不要跨越文件系统边界
-B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节
-e, --rsh=COMMAND 指定替代rsh的shell程序
--rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息
-C, --cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件
--delete 删除那些DST中SRC没有的文件
--delete-excluded 同样删除接收端那些被该选项指定排除的文件
--ignore-errors 即使出现IO错误也进行删除
--max-delete=NUM 最多删除NUM个文件
--numeric-ids 不将数字的用户和组ID匹配为用户名和组名
--timeout=TIME IP超时时间,单位为秒
-I, --ignore-times 不跳过那些有同样的时间和长度的文件
--exclude=PATTERN 指定排除不需要传输的文件模式
--include=PATTERN 指定不排除而需要传输的文件模式
--exclude-from=FILE 排除FILE中指定模式的文件
--include-from=FILE 不排除FILE指定模式匹配的文件
--log-format=formAT 指定日志文件格式
--password-file=FILE 从FILE中得到密码
--bwlimit=KBPS 限制I/O带宽,KBytes per second
另外要注意最后的/ 有没有是不一样的:
如:
[loguser@idc010vm010 ~]$ rsync --list-only -e ssh 10.100.10.12:/var/log
Warning: Permanently added '10.100.10.12' (RSA) to the list of known hosts.
[loguser@idc010vm010 ~]$ rsync --list-only -e ssh 10.100.10.12:/var/log/
Warning: Permanently added '10.100.10.12' (RSA) to the list of known hosts.
-rw------- 5311 2012/07/12 16:17:29 anaconda.ifcfg.log
-rw------- 37319 2012/07/12 16:17:29 anaconda.program.log
-rw------- 299403 2012/07/12 16:17:29 anaconda.storage.log
-rw------- 41263 2012/07/12 16:17:29 anaconda.syslog
-rw------- 4596 2012/07/12 16:17:29 anaconda.yum.log
-rw-r--r-- 3142485 2012/09/11 16:43:10 localhost_access_log.2012-07-23.txt
-rw-r--r-- 3142485 2012/09/11 16:06:36 localhost_access_log.2012-09-11.txt
rsync -av /src_dir/ user@IP_Addres:/dest_dir/
A-->B 同步本地到远端
rsync -av user@IP_Addres:/src_dir/ /dest_dir/
B-->A 同步远端到本地
A-->B 同步本地到远端
B-->A 同步远端到本地
在日常的使用中,可以将rsync同步的事件写在crontab 任务里或者先写至脚本里,然后定期执行,减轻了维护人员手动操作的负担。
转载于:https://blog.51cto.com/chenjiuling/1008136