利用rsync同步备份文件

来源:互联网 发布:caffe softmax层 编辑:程序博客网 时间:2024/05/16 23:50

一、环境准备

1、服务器详细信息如下,有两台主机,A和B

主机 主机名 ip A linux-node1 192.168.17.23 B linux-node2 192.168.17.24

2、为详细的拓扑图架构
逻辑架构图
ps:这里ip好像写错了,右边应该是192.168.17.24
自己画的一个小的逻辑图

物理架构图
3、将两台主机上的hosts解析设置为一样
主机A

[root@linux-node1 ~]# tail -2 /etc/hosts192.168.17.23  linux-node1.com192.168.17.24  linux-node2.com

主机B

[root@linux-node2 ~]# tail -2 /etc/hosts192.168.17.23  linux-node1.com192.168.17.24  linux-node2.com

二、部署rsync服务器端

1、配置rsyncd.conf

#情动发现报错,原来是需要配置rsyncd.conf[root@linux-node1 ~]# rsync --daemonFailed to parse config file: /etc/rsyncd.conf#配置rsyncd.conf[root@linux-node1 ~]# cat /etc/rsyncd.conf    ###this is rsyncd.conf ###by amsilenceuid = rsyncgid = rsyncuse chroot = nomax connections =2000timeout = 600pid file = /var/run/rsyncd.pidlock file = /var/run/rsync.locklog file = /var/log/rsyncd.logignore errorsread only = falselist = falsehosts allow = 192.168.17.0/24hosts deny = 0.0.0.0/32auth users = rsync_backupsecrets file = /etc/rsync.password#################################[backup]path = /backup

2、启动rsync服务
错误1:

[root@linux-node1 ~]# rsync --deamonrsync: --deamon: unknown optionrsync error: syntax or usage error (code 1) at main.c(1422) [client=3.0.6]

解决办法:忘记创建/etc/rsync.password文件

[root@linux-node1 ~]# echo “rsync_backup:123456”  >/etc/rsync.password 

启动rsync成功

[root@linux-node1 ~]# cat /etc/rsync.password rsync_backup:123456[root@linux-node1 ~]# chmod 600 /etc/rsync.password [root@linux-node1 ~]# rsync --daemon[root@linux-node1 ~]# pgrep rsync1267[root@linux-node1 ~]# netstat -lntup|grep rsynctcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      1267/rsynctcp        0      0 :::873                      :::*                        LISTEN      1267/rsync    

3、创建rsync需要使用对的真实用户

[root@linux-node1 ~]# useradd -s /sbin/nologin -M rsync[root@linux-node1 ~]# id rsyncuid=501(rsync) gid=501(rsync) groups=501(rsync)

4、创建rsync推送需要的目录(需要按照)

[root@linux-node1 ~]# mkdir /backup[root@linux-node1 ~]# chown -R rsync.rsync /backup/

5、使用真实用户来测试rsync服务
注意:如果ssh端口修改了,就需要用这种加上端口,rsync -参数 文件 -e ‘ssh -p 端口号’ 用户名@ip:要推送到那里

[root@linux-node1 ~]# rsync -avz install.log '-e ssh -p 4086' silence@linux-node1.com:/tmpThe authenticity of host '[linux-node1.com]:4086 ([192.168.17.23]:4086)' can't be established.RSA key fingerprint is a3:55:28:26:ec:e6:9c:95:bc:06:3c:fd:fc:1b:b3:23.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '[linux-node1.com]:4086,[192.168.17.23]:4086' (RSA) to the list of known hosts.silence@linux-node1.com's password: sending incremental file listinstall.logsent 5398 bytes  received 31 bytes  638.71 bytes/sectotal size is 21764  speedup is 4.01[root@linux-node1 ~]# ll /tmp/total 24-rw-r--r-- 1 silence silence 21764 Jan  4 08:41 install.log

测试rsync使用成功

三、部署rsync客户端

1、测试rsync虚拟用户
注意:在使用虚拟用户的时候不需用ssh的端口

[root@linux-node2 ~]# rsync -avz install.log rsync_backup@linux-node1.com::backup                 Password: sending incremental file listinstall.logsent 5394 bytes  received 27 bytes  2168.40 bytes/sectotal size is 21764  speedup is 4.01

2、创建自动应答密码文件

[root@linux-node2 ~]# cat /etc/rsync.password 123456[root@linux-node2 ~]# chmod 600 /etc/rsync.password#下面内容为测试 [root@linux-node2 ~]# rsync -avz install.log rsync_backup@linux-node1.com::backup --password-file=/etc/rsync.password sending incremental file listsent 32 bytes  received 8 bytes  26.67 bytes/sectotal size is 21764  speedup is 544.10
0 0