用Rsync实现windows下同步linux服务器的数据

一:环境

  一、服务端:Red Hat Enterprise Linux Server release 6.4 (Santiago)
  二、客户端:windows7旗舰版64位
  三、同步对象:测试数据
  四、Rsync介绍:rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync能够远程同步,支持本地复制,或者与其余SSH、rsync主机同步。是一款很是理想的数据同步工具
  Rsync的好处:支持增量备份,对于不少大的数据文件来讲用rsync是一个很理想的工具,既能节省磁盘空间还能提升备份相率linux

  五、注意事项:此种同步方式仅限rsync的daemon模式vim

二:无差别同步示意图

三:服务端配置文件

  一、 rsync配置文件:/etc/rsyncd.conf

uid = rsync # rsync用户,要是0的话表明全部用户
gid = rsync # rsync用户组
use chroot = no # 安全考虑,出现BUG能够把危险的数据定位到一个chroot目录下去,避免恶意攻击
max connections = 200 # 最大链接数
timeout = 300 # 超时时常
pid file = /var/run/rsyncd.pid # 服务端进程号
lock file = /var/run/rsync.lock # 服务端锁文件
log file = /var/log/rsyncd.log # 服务端后台日志
[test] # 模块名
path = /tmp/filesystem/ # 须要同步的目录内容
ignore errors # 忽略错误
read only = false # 取消只读
list = false # 客户端列出服务端的内容
hosts allow = 192.168.3.0/24  # 同步的主机地址
hosts deny = 0.0.0.0/32 # 须要阻止哪些地址登录
auth users = test_rsync # rsync用户名
secrets file = /etc/rsync.password 

  二、rsync密码文件:/etc/rsync.password

[root@test ~]# cat /etc/rsync.password 
test_rsync:test1234  #前面是rsync的虚拟用户,后者是rsync的密码 
[root@test ~]# ll /etc/rsync.password
-rw------- 1 root root 17 Nov 26 14:46 /etc/rsync.password  # 修改密码文件权限为600

  三、修改服务端两个配置文件权限(/etc/rsyncd.conf、rsync.password)

[root@test ~]# chmod 600 /etc/rsyncd.conf
[root@test ~]# chmod 600 /etc/rsync.password

  四、建立rsync用户

[root@test ~]# useradd rsync -s /sbin/nologin

  五、启动服务器端rsync进程

[root@test ~]# rsync --daemon

  六、检查进程状态以及进程端口(默认873)以下:说明服务端一切正常

[root@test ~]# netstat -lnutp | grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 20509/rsync 
tcp 0 0 :::873 :::* LISTEN 20509/rsync 
[root@test ~]# ps -ef | grep rsync
root 20509 1 0 16:20 ? 00:00:00 rsync --daemon
root 22508 10881 0 17:12 pts/1 00:00:00 grep rsync

三:客户端准备


  下载客户端软件:cwRsync_4.1.0_Installer.exewindows

  连接:https://pan.baidu.com/s/1rzDbW1O5mVoW3F-sKQodUg
  提取码:lwg0 安全

四:安装rsync客户端

五:客户端配置

  一、rsync家目录内容

  二、配置客户端rsync

    在bin目录下加入密码文件:rsync.password,注意此密码要和服务端的/etc/rsync.password里面的密码一致,服务端密码为:test1234,因此客户端密码文件也写成这个服务器

  三、修改客户端密码文件的属主:(不修改属主默认会提示错误)

  四、同步服务端的数据到客户端指定位置

    在客户端rsync目录的bin目录下执行:
    rsync.exe -avz --password-file=rsync.password test_rsync@192.168.3.14::test /cygdrive/f/testdir >> syslog.logtcp

  五、参数解读

    -avz参数说明:
    -a:等于下图全部内容:工具

    -v:可视化
    -z:传输时进行压缩提升传输速率
    --password-file:指定密码文件
    test_rsync:服务端/etc/rsyncd.conf里面的rsync的虚拟用户
    ::test:这个是服务端/etc/rsyncd.conf里面的模块名,客户端会根据服务端的模块名去找服务端须要共同步数据的目录
    /cygdrive/f/testdir:告诉rsync个人数据是同步到客户端f盘的testdir目录下测试

  六、linux和linux系统之间同步客户端配置更简单★★★★

    只须要在客户端上加入以下内容便可:ui

[root@rsync_client ~]# cat /etc/rsync.password 
test1234

六:无差别备份实验

  一、服务端数据

[root@rsync_server zwy]# ls
1 10 2 3 4 5 6 7 8 9

  二、客户端数据

[root@rsync_client data]# pwd
/root/data
[root@rsync_client data]# ls # 客户端无数据

  三、第一次同步

[root@rsync_client data]# rsync -avz --password-file=/etc/rsync.password test_rsync@192.168.3.14::test ~/data/
receiving incremental file list
./
1
10
2
3
4
5
6
7
8
9
sent 248 bytes received 539 bytes 1574.00 bytes/sec
total size is 0 speedup is 0.00

  四、检查服务端数据是否同步正常

[root@rsync_client data]# ls
1 10 2 3 4 5 6 7 8 9

  五、无差别同步,删除服务器端的1-5的数据,而后客户端拉取数据看看效果

[root@rsync_server zwy]# ls
1 10 2 3 4 5 6 7 8 9
[root@rsync_server zwy]# rm -f {1..5}
[root@rsync_server zwy]# ls
10 6 7 8 9

  六、客户端用无差别备份的方式拉数据看看效果

[root@rsync_client data]# rsync -avz --delete --password-file=/etc/rsync.password test_rsync@192.168.3.14::test ~/data/
receiving incremental file list
deleting 5
deleting 4
deleting 3
deleting 2
deleting 1
./
10
6
7
8
9
sent 153 bytes received 333 bytes 972.00 bytes/sec
total size is 0 speedup is 0.00
[root@rsync_client data]# ls # 再看客户端数据发现1-5的数据已经被删除了
10 6 7 8 9

  七、无差别备份要了解拉数据和推数据的流程,不然风险很大!!!!!

七:附rsync自启动脚本

 1 [root@test ~]# cat /etc/init.d/tfrsync 
 2 #!/bin/sh
 3 #
 4 #
 5 # rsync start/stop/status/restart
 6 #
 7 # chkconfig: 35 57 10
 8 # description: rsync backup oa data 
 9 
10 . /etc/init.d/functions
11 
12 PID="/var/run/rsyncd.pid"
13 LOCK="/var/run/rsync.lock"
14 COUNT=`ps -ef | grep rsync | grep -vE "grep|vim|cat|more|sh" | wc -l`
15 STATUS_TRUE="action "$0" /bin/true"
16 STATUS_FALSE="action "$0" /bin/false"
17 
18 case "$1" in
19 
20   start)
21 
22         if [ $COUNT -eq 1 ] 
23           then
24             echo -ne "rsync is started " && $STATUS_FALSE
25             exit 1
26         else
27             /usr/bin/rsync --daemon && $STATUS_TRUE || $STATUS_FALSE
28 
29         fi
30 
31         ;;
32 
33   stop)
34 
35         cat $PID | xargs kill -9 2>/dev/null && $STATUS_TRUE && rm -f $PID $LOCK || $STATUS_FALSE
36 
37         ;;
38 
39   status)
40 
41         [ $COUNT -eq 1 ] && echo "rsync is running." || echo "rsync is stoped."
42 
43         ;;
44 
45   restart)
46 
47         if [ -f $PID ]
48           then
49             cat $PID | xargs kill -9 && $STATUS_TRUE && rm -rf $PID $LOCK && \
50             /usr/bin/rsync --daemon && $STATUS_TRUE || $STATUS_FALSE
51         else
52             /usr/bin/rsync --daemon && $STATUS_TRUE || $STATUS_FALSE
53         fi
54 
55         ;;
56 
57   *)
58         echo "Usage:$0 {start|stop|status|restart}"
59         ;;
60 esac

脚本写的很差,还请你们多多包涵,谢谢!spa

相关文章
相关标签/搜索