Centos6系统利用rsync与ssh实现文件同步

Centos6系统利用rsyncssh实现文件同步

Rsyncremote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。Rsync使用所谓的“Rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。

Rsync本来是用于替代rcp的一个工具,目前由rsync.samba.org维护,所以rsync.conf文件的格式类似于samba的主配置文件。Rsync可以通过rshssh使用,也能以daemon模式去运行,在以daemon方式运行时Rsync server会打开一个873端口,等待客户端去连接。连接时,Rsync server会检查口令是否相符,若通过口令查核,则可以开始进行文件传输。第一次连通完成时,会把整份文件传输一次,以后则就只需进行增量备份。

Rsync支持大多数的类Unix系统,无论是LinuxSolaris还是BSD上都经过了良好的测试。此外,它在windows平台下也有相应的版本,如cwRsyncSync2NAS等工具。

 
一、安装与启动rsync服务
1)、下载:

10.100.10.10(服务器端)

tar xvf rsync-3.0.9.tar.gz

tar xvf rsync-patches-3.0.9.tar.gz

 
yum list rsync
Loaded plugins: fastestmirror, presto
Loading mirror speeds from cached hostfile

 * base: mirrors.163.com

 * extras: mirrors.163.com

 * updates: mirrors.163.com

Installed Packages

rsync.x86_64                3.0.6-9.el6                                  @base

yum erase –y rsync   卸载系统自带的rsync
cd rsync-3.0.9
 ./configure --disable-debug --with-rsh=ssh --prefix=/usr/local/rsync

Make && make install

 

ln -sv /usr/local/rsync/bin/rsync /usr/bin/rsync

ln -sv /usr/local/rsync/bin/rsync /usr/sbin/rsync

 
2)为rsync提供配置文件/etc/rsyncd.conf,内容类似如下内容:
Vim /etc/rsyncd.conf
 

# Section 1: Global settings

uid = root
gid = root
use chroot = no

max connections = 3

strict modes = yesi

port = 873

pid file = /var/run/rsync/rsyncd.pid

log file = /var/log/rsync/rsyncd.log

lock file = /var/log/rsync/rsyncd.lock

# Section 2Directory to be synced
[httpdlog]

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 允许来同步数据的主机,多个用空格隔开

hosts deny = *
list = false

auth users = loguser

secrets file = /etc/rsync.passwd

[tomcatlogs]

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 允许来同步数据的主机,多个用空格隔开

hosts deny = *
list = false

auth users = loguser

secrets file = /etc/rsync.passwd

[nginxlog]

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 允许来同步数据的主机,多个用空格隔开

hosts deny = *
list = false

auth users = loguser

secrets file = /etc/rsync.passwd

 
 

vim /etc/rsync.passwd

loguser:loguser123
 

其中冒号前的是用户名,冒号后的是对应用户的密码。此文件不能为其他任意用户可访问,因此可用如下命令修改:

# chmod 600 /etc/rsync.passwd
 
3)、启动rsync服务

mkdir -pv /var/run/rsync/ /var/log/rsync /var/lock/rsync

rsync --daemon –config=/etc/rsyncd.conf netstat -tnlp|grep 873

tcp        0      0 0.0.0.0:873     0.0.0.0:*         LISTEN      26635/rsync        

tcp        0      0 :::873          :::*             LISTEN      26635/rsync 

 
二、配置双机互信(root用户):

1)、开启root用户ssh认证权限

首先由于要运行的rsyncroot用户,有通过ssh做互信的权限。

vim /etc/ssh/sshd_config

PermitRootLogin yes 将其注释去掉

 
2)、在 10.10上:
cd /root
mkdir .ssh chmod 755 .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:

b5:02:63:6f:93:30:a0:f2:a6:e6:e0:a1:05:f3:23:79 root@idc010vm010

The key's randomart image is:

+--[ RSA 2048]----+

|    .            |

|   . .           |

|. .   *   .      |

| o   . * o .     |

|o o     S .      |

| B     . o       |

|=oE              |
|*+..             |
|.o               |
+-----------------+

cat .ssh/id_rsa.pub >> authorized_keys

scp authorized_keys [email protected]:/root/.ssh 要在12服务器mkdir .ssh 之后进行,

输入12机器的root用户密码即可。
 
3)、在10.12 上:
mkdir .ssh
chmod 750 .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 image is:

+--[ RSA 2048]----+

|                 |
|                 |

|            .    |

|           . * . |
|        S . * =o+|
|         o o +oo*|
|        . . o..o.|
|           E =.. |
|            +o+. |
+-----------------+

cat .ssh/id_rsa.pub >> authorized_keys

scp authorized_keys [email protected]:/root/.ssh 输入10机器的root用户密码即可。

 
4)、结果实现:
[root@idc010vm012 ~]# ssh 10.100.10.10

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

[root@idc010vm010 ~]# 
 
三、配置双机互信(普通用户):
1)、10.10
useradd loguser
passwd loguser
 
su loguser
mkdir .ssh chmod 755 .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:

5d:3c:e7:af:8c:5a:60:13:17:a9:87:e8:98:8b:9e:ed loguser@idc010vm010

The key's randomart image is:

+--[ RSA 2048]----+

|            ..   |

|           ...   |

|         ..o= . |

|        ..o+.+   |

|       +S =. . |

|      o .. o   . |

|     . .    .   .|

|    .o.    . o . |

|   .o.E   ... o |

+-----------------+
 

cp /home/loguser/.ssh/id_rsa.pub /home/loguser/authorized_keys

scp /home/loguser/authorized_keys [email protected]:/home/loguser/.ssh/

输入密码

chmod –R 600 .ssh/*

 
2)、10.12上:
useradd loguser
passwd loguser
 
su loguser
mkdir .ssh chmod 755 .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 image is:

+--[ RSA 2048]----+

|       .o==o     |

|       .++=.     |

|        o*. E    |

|       .o * + + |
|        S . * = o|

|           .   o |

|                 |
|                 |
|                 |
+-----------------+
 

cp /home/loguser/.ssh/id_rsa.pub /home/loguser/authorized_keys

scp /home/loguser/authorized_keys [email protected]:/home/loguser/.ssh/

输入密码

chmod –R 600 .ssh/*

 
3)、测试:
[loguser@idc010vm012 ~]$ ssh 10.100.10.10 date

Warning: Permanently added '10.100.10.10' (RSA) to the list of known hosts.

Wed Sep 12 17:11:16 CST 2012
 
4)、拷贝rsync的配置文件:
scp 10.100.10.10:/etc/rsyncd.conf /etc/ 普通用户是没有这个权限的,会报错。

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               

 
创建密码文件:
echo "loguser123" > /etc/rsync.passwf 客户端的rsync密码文件只能存放密码,而不要用户名。
 
5)、使用rsync拷贝文件试试:

[loguser@idc010vm012 .ssh]$ rsync -vzrtopg --progress --stats -e ssh /opt/GeoDB.dat [email protected]:/tmp/cmake

Warning: Permanently added '10.100.10.10' (RSA) to the list of known hosts.

sending incremental file list

GeoDB.dat

     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

-rw-r--r--. 1 loguser loguser 4333568 Sep 1 17:08 cmake 
 

四、Rsync的常用参数描述:

-v, --verbose 详细模式输出
-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
-r, --recursive 对子目录以递归模式处理
-b, --backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用--suffix选项来指定不同的备份文件前缀。

--backup-dir 将备份文件(~filename)存放在在目录下。

-u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件)
-l, --links 保留软链结

-L, --copy-links 想对待常规文件一样处理软链结

--copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链结

--safe-links 忽略指向SRC路径目录树以外的链结

-H, --hard-links 保留硬链结

-p, --perms 保持文件权限-o, --owner 保持文件属主信息
-g, --group 保持文件属组信息
-D, --devices 保持设备文件信息
-t, --times 保持文件时间信息
-n, --dry-run现实哪些文件将被传输

-W, --whole-file 拷贝文件,不进行增量检测

-x, --one-file-system 不要跨越文件系统边界

-B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节

-e, --rsh=COMMAND 指定替代rshshell程序

--rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息

-C, --cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件

--existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件

--delete 删除那些DSTSRC没有的文件

--delete-excluded 同样删除接收端那些被该选项指定排除的文件

--delete-after 传输结束以后再删除

--ignore-errors 即使出现IO错误也进行删除

--max-delete=NUM 最多删除NUM个文件

--partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输
--force 强制删除目录,即使不为空

--numeric-ids 不将数字的用户和组ID匹配为用户名和组名
--timeout=TIME IP
超时时间,单位为秒

-I, --ignore-times 不跳过那些有同样的时间和长度的文件

-z, --compress 对备份的文件在传输时进行压缩处理

--exclude=PATTERN 指定排除不需要传输的文件模式

--include=PATTERN 指定不排除而需要传输的文件模式

--exclude-from=FILE 排除FILE中指定模式的文件

--include-from=FILE 不排除FILE指定模式匹配的文件

--version 打印版本信息
--config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf文件
--port=PORT 指定其他的rsync服务端口
--stats 给出某些文件的传输状态
--progress 在传输时现实传输过程

--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.

drwxr-xr-x        4096 2012/09/12 09:43:30 log
 

[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.

drwxr-xr-x        4096 2012/09/12 09:43:30 .

-rw-------        5311 2012/07/12 16:17:29 anaconda.ifcfg.log

-rw-------       20736 2012/07/12 16:17:29 anaconda.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-------       33926 2012/07/12 16:17:29 anaconda.xlog

-rw-------        4596 2012/07/12 16:17:29 anaconda.yum.log

-rw-r--r--        2546 2012/09/10 18:15:43 boot.log
-rw-------        4224 2012/09/12 16:15:24 btmp
-rw-------       28071 2012/09/12 17:01:01 cron
-rw-------       21953 2012/09/09 03:18:01 cron-20120909
-rw-r--r--       18399 2012/09/10 18:15:20 dmesg
-rw-r--r--       18399 2012/09/06 09:32:09 dmesg.old
-rw-r--r--      129544 2012/07/12 16:17:24 dracut.log
-rw-r--r--      146292 2012/09/12 16:55:45 lastlog

-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同步的模式
1)、增量备份

rsync -av /src_dir/ user@IP_Addres:/dest_dir/

        A-->B 同步本地到远端

rsync -av user@IP_Addres:/src_dir/ /dest_dir/

        B-->A 同步远端到本地

2)、镜像备份
rsync -av --delete /src_dir/ user@IP_Addres:/dest_dir/

        A-->B 同步本地到远端

rsync -av --delete user@IP_Addres:/src_dir/ /dest_dir/

        B-->A 同步远端到本地

在日常的使用中,可以将rsync同步的事件写在crontab 任务里或者先写至脚本里,然后定期执行,减轻了维护人员手动操作的负担。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章