rsync 远程同步 linux 及 windows 主机

简介:

一、前言

1、rsync原理及rsync+inotify触发同步

    可以配置SSH信任,做信任后,同步就不需要密码文件了

    http://nmshuishui.blog.51cto.com/1850554/1387048

2、本篇说明 

    为了保证数据的绝对可靠性:

    首先,所有的服务器的数据都用脚本打包备份在本地一份,如上篇博客

    其次,单独拿出一台pc机做备份机,为rsync客户端,每天定时却拉取各rsync服务器备份好的数据包

    再次,使用rsync同步 linux 及 windows 服务器

3、服务器说明 

    rsync服务器:192.168.200.16,192.168.200.18

    rsync客户端:192.168.1.186

    这三台服务器都是linux,rsync同步windows服务器到linux服务器在第四条

二、部署rsync服务端
1、 禅道服务器(192.168.200.16)
(1)安装超级守护进程

1
#yum –y install xinetd

(2)为rsync服务提供配置文件

1
#vim /etc/rsyncd.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#Global Settings        
uid = nobody
gid = nobody
use chroot = no
max connections = 5
timeout = 600
pid  file  /var/run/rsyncd .pid
lockfile =  /var/run/rsyncd .lock
log  file  /var/log/rsyncd .log
 
#module
[chandao]
path =  /opt/backup/
ignore errors =  yes
read  only = no
write only = no
hosts allow = *
hosts deny = *
list =  yes
uid = root
gid = root
auth  users  = dianyi
secrets  file  /etc/chandao . passwd

(3)配置rsync认证文件/etc/chandao.passwd

1
2
3
[root@chandao ~] #echo “dianyi:dianyi” > /etc/chandao.passwd        
[root@chandao ~] #cat /etc/chandao.passwd
dianyi:dianyi

(4)修改/etc/chandao.passwd的权限为600

1
2
3
[root@chandao ~] #chmod 600 /etc/chandao.passwd
[root@chandao ~] # ll /etc/chandao.passwd
-rw------- 1 root root 14 Jul 29 18:18  /etc/chandao . passwd

(5)配置服务启动

1
2
[root@chandao ~] # chkconfig rsync on
[root@chandao ~] # service xinetd start

(6)检查873端口是否成功监听

1
[root@chandao ~] # ss -antlp | grep 873

(7)检查rsync运行状态

1
[root@chandao ~] # chkconfig --list

 2、禅道数据库服务器(192.168.200.18)
(1)安装超级守护进程

1
#yum –y install xinetd

(2)为rsync服务提供配置文件

1
#vim /etc/rsyncd.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#Global Settings
uid = nobody
gid = nobody
use chroot = no
max connections = 5
timeout = 600
pid  file  /var/run/rsyncd .pid
lockfile =  /var/run/rsyncd .lock
log  file  /var/log/rsyncd .log
 
#module
[db_chandao]
path =  /opt/backup/
ignore errors =  yes
read  only = no
write only = no
hosts allow = *
hosts deny = *
list =  yes
uid = root
gid = root
auth  users  = dianyi
secrets  file  /etc/db_chandao . passwd

(3)配置rsync认证文件/etc/db_chandao.passwd

1
2
3
[root@DBSlave ~] #echo “dianyi:dianyi” > /etc/db_chandao.passwd
[root@DBSlave ~] #cat /etc/db_chandao.passwd
dianyi:dianyi

(4)修改/etc/db_chandao.passwd 的权限为600

1
2
3
[root@DBSlave ~] #chmod 600 /etc/db_chandao.passwd
[root@DBSlave ~] # ll /etc/db_chandao.passwd
-rw------- 1 root root 14 Jul 29 18:18  /etc/db_chandao . passwd

(5)配置服务启动

1
2
[root@DBSlave ~] # chkconfig rsync on
[root@DBSlave ~] # service xinetd start

(6)检查873端口是否成功监听

1
[root@DBSlave ~] # ss -antlp | grep 873

(7)检查rsync运行状态

1
[root@DBSlave ~] # chkconfig --list

三、部署rsync客户端(192.168.1.186)
1、设置rsync客户端的密码文件
(1)禅道的密码文件

1
2
3
[root@backup ~] #echo dianyi > /etc/chandao.passwd
[root@backup ~] # cat /etc/chandao.passwd
dianyi

(2)禅道数据库的密码文件

1
2
3
[root@backup ~] #echo dianyi > /etc/db_chandao.passwd
[root@backup ~] # cat /etc/db_chandao.passwd
dianyi

2、修改密码文件的的权限为600

1
2
3
4
5
[root@backup ~] # chmod 600 /etc/chandao.passwd
[root@backup ~] # chmod 600 /etc/db_chandao.passwd
[root@backup ~] # ll /etc/chandao.passwd /etc/db_chandao.passwd
-rw------- 1 root root 7 Jul 29 17:17  /etc/chandao . passwd
-rw------- 1 root root 7 Jul 29 16:19  /etc/db_chandao . passwd

3、测试rsync客户端是否可以从rsync服务器拉成功

1
[root@backup ~] # rsync -vzrtopg --password-file=/etc/chandao.passwd dianyi@192.168.200.16::chandao/* /tmp

4、rsync同步脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/bash
#func:rsync for chandao
#Date:first write on 2014.7.29
#Version:v1.0
 
ntpdate 192.168.200.16 >  /dev/null  2>&1
date =` date  +%Y-%m-%d`
dest_chandao= /opt/backup/chandao/zentao
dest_db= /opt/backup/chandao/db_chandao
remote_chandao=192.168.200.16
remote_db=192.168.200.18
module_chandao=chandao
module_db=db_chandao
user1=13910892224@139.com
user2=15048088251@139.com
 
main(){
      rsync  -vzrtopg --password- file = /etc/chandao . passwd  dianyi@$remote_chandao::$module_chandao/*$ date * $dest_chandao
      if  [ $? - eq  0 ]; then
          echo  "chandao-*$date* was rsynced"  >>   /opt/log/chandao_rsync .log 2>&1;
      else
          echo  "chandao-*$date* was not rsynced"  >>   /opt/log/chandao_rsync .log 2>&1;
          echo  "Chandao rsync was faild "  | mail -s  "chandao backup"  $user2;
      fi
 
      rsync  -vzrtopg --password- file = /etc/db_chandao . passwd  dianyi@$remote_db::$module_db/*$ date * $dest_db
      if  [ $? - eq  0 ]; then
          echo  "db_chandao_*$date* was rsynced"  >>   /opt/log/chandao_rsync .log 2>&1;
      else
          echo  "db_chandao_*$date* was not rsynced"  >>   /opt/log/chandao_rsync .log 2>&1;
          echo  "db_chandao rsync was faild "  | mail -s  "chandao backup"  $user2;
      fi
}
 
main

四、定时任务

1
2
3
[root@backup ~] # crontab -e
##rsync for chandao
20 09 * * *  /bin/sh  /opt/scripts/rs_chandao .sh

五、同步windows数据到linux备份机上
     主体思想还是和linux保持一致的
          rsync服务器需要配置2个方面:(1)配置文件 (2)密码文件 
          接下来就是rsync客户端配置:     (1)密码文件 (2)权限设为600
1、下载cwrsync server
    安装时,设置的用户和密码均为dianyi,即两台机器使用rsync相互通信时会使用到的用户名和密码
2、在这台windows上配置rsync服务器,远程linux做为rsync的客户端来拉取windows服务器上的文件
(1)修改配置文件
     在C:\Program Files (x86)\ICW\rsyncd.conf 添加模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
use chroot =  false
strict modes =  false
hosts allow = *
log  file  = rsyncd.log
 
# Module definitions
# Remember cygwin naming conventions : c:\work becomes /cygwin/c/work
#
 
[svn]
path =  /cygdrive/e/Repositories     #e代表e盘
ignore errors =  yes
read  only = no
transfer logging =  yes
write only = no
hosts allow = *
hosts deny = *
list =  yes
uid = 0            #这里的udi和gid不再是root或是nobody,而应该是0,如果不填会报会报:@ERROR: invalid uid nobody
gid = 0
auth  users  = dianyi
secrets  file  = etc /svn . passwd

(2)创建密码文件
       在C:\Program Files (x86)\ICW\etc中创建密码文件svn.passwd
       内容为     dianyi:dianyi
(3)启动rsync服务
       运行 --> services.msc --> RsyncServer 启动类型:自动
(4)查看873端口是否监听成功
       运行 --> cmd --> netstat -ano

(5)在防火墙中开放tcp 873端口
3、配置rsync客户端(remote linux:192.168.1.186)
(1)创建密码认证文件

1
2
vim  /etc/svn . passwd
dianyi

(2)设置权限

1
chmod  600  /etc/svn . passwd

(3)测试

1
# rsync -vzrtopg --password-file=/etc/svn.passwd dianyi@192.168.1.64::svn/* /opt/111/

    上面这个命令行中-vzrtopg里的v是verbose,z是压缩,r是recursive,topg都是保持文件原有属性如属主、时间的参数

5、把命令添加到脚本










本文转自 nmshuishui 51CTO博客,原文链接:http://blog.51cto.com/nmshuishui/1532927,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
存储 缓存 固态存储
深度解析linux主机:从应用到硬盘,玩转系统I/O性能优化攻略!
深度解析linux主机:从应用到硬盘,玩转系统I/O性能优化攻略!
|
1月前
|
Kubernetes Linux Windows
第二章 Linux和windows部署helm 客户端
第二章 Linux和windows部署helm 客户端
31 0
|
1月前
|
Linux
Linux 系统时间同步 ​使用 NTP 服务时间同步​
Linux 系统时间同步 ​使用 NTP 服务时间同步​
46 2
|
5天前
|
iOS开发 MacOS Windows
|
2天前
|
NoSQL Linux Redis
Redis的介绍,以及Redis的安装(本机windows版,虚拟机Linux版)和Redis常用命令的介绍
Redis的介绍,以及Redis的安装(本机windows版,虚拟机Linux版)和Redis常用命令的介绍
13 0
|
10天前
|
前端开发 Linux iOS开发
【Flutter前端技术开发专栏】Flutter在桌面应用(Windows/macOS/Linux)的开发实践
【4月更文挑战第30天】Flutter扩展至桌面应用开发,允许开发者用同一代码库构建Windows、macOS和Linux应用,提高效率并保持平台一致性。创建桌面应用需指定目标平台,如`flutter create -t windows my_desktop_app`。开发中注意UI适配、性能优化、系统交互及测试部署。UI适配利用布局组件和`MediaQuery`,性能优化借助`PerformanceLogging`、`Isolate`和`compute`。
【Flutter前端技术开发专栏】Flutter在桌面应用(Windows/macOS/Linux)的开发实践
|
11天前
|
Linux Python Windows
Python虚拟环境virtualenv安装保姆级教程(Windows和linux)
Python虚拟环境virtualenv安装保姆级教程(Windows和linux)
|
17天前
|
网络协议 Linux 程序员
【Linux】虚拟机ipv4地址消失,主机ping不通
【Linux】虚拟机ipv4地址消失,主机ping不通
31 0
|
17天前
|
运维 网络协议 Linux
Linux(28) Linux双网卡配置为连接到Linux主机的PC提供外网访问
Linux(28) Linux双网卡配置为连接到Linux主机的PC提供外网访问
51 1