跳至主要内容

利用 rsync 在服务器间同步目录实现多点站群

场景

主服务器: 192.168.1.1

站群服务器A: 192.168.1.2

站群服务器B: 192.168.1.3

要将 主服务器的 /home/wwwroot 目录同步到 站群服务器A /home/wwwroot 下

要将 主服务器的 /home/www 目录同步到 站群服务器B /home/www 下

执行环境

主服务操作系统: centos 7.X

站群服务器AB: Ubuntu 14.x/16.x

配置

我们将使用 rsync 的 daemon 方式来完成任务

登录到 A 服务器,执行

vi /etc/rsyncd.conf

添加

uid = root
gid = root
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

[wwwroot]
path = /home/wwwroot
ignore errors
read only = true
list = true
auth users = backup
secrets file = /etc/rsyncd.pwd

[www]
path = /home/www
ignore errors
read only = true
list = true
auth users = backup
secrets file = /etc/rsyncd.pwd

保存文件 :wq!

执行

vi /etc/rsyncd.pwd

添加

backup:sandyliao

backup为用户和rsyncd.conf里面的用户要一致,sandyliao为密码

保存退出,执行

chmod 600 /etc/rsyncd.pwd 

启动 rsync 服务 执行

rsync --daemon

登录 站群服务器 ,执行

vi /etc/rsyncd.pwd

添加

sandyliao #这为主服务器密码

保存退出,执行

chmod 600 /etc/rsyncd.pwd 

好了,配置已经完成了,现在 主 服务器为服务端 ,站群服务器AB为客户端,现在就可以在 AB 服务器上执行同步操作了

配置说明

uid = root #设置执行rsync的本地用户
gid = root #设置执行rsync的本地组
max connections = 4 #最大同时连接数
pid file = /var/run/rsyncd.pid #指定rsync服务进程的pid file
lock file = /var/run/rsync.lock #指定rsync服务端锁定文件log file = /var/log/rsyncd.log #指定rsync的log输出路径

[wwwroot] #模块设定,可设置多个模块
path = /home/wwwroot #同步文件的真实路径
ignore errors
read only = true #是否只读
list = true
auth users = backup #身份验证用户。这不是系统用户,而是rsync服务自定的
secrets file = /etc/rsyncd.pwd #当前模块的密码文件

开始同步

登录 站群A 服务器,执行

rsync -avzP --password-file=/etc/rsyncd.pwd backup@192.168.1.1::wwwroot  /home/wwwroot

登录 站群B服务器,执行

 

rsync -avzP --password-file=/etc/rsyncd.pwd backup@192.168.1.1::www  /home/www

注意

1.如果 /home/下 wwwroot 目录不存在将会创建,如果已经存在了, 会将 主服务器机器上的 /home/wwwroot 目录下的内容同步到 站群服务器A机器上的 /home/wwwroot/ 下
2.假如 A 机器上有一个文件/home/wwwroot/1.txt , 在第二次执行同步时没有修改,则不会同步
3.假如 B 机器上有一个文件/home/wwwroot/2.txt , A 上没有,同步时不会被删除
4.如果 B 机器上有一个文件/home/wwwroot/1.txt 被修改了,同步时会被 A 上对应的文件覆盖

扩展

如果要同步别的目录,只需要修改 A 服务器上的 /etc/rsyncd.conf 将 logs 部分修改或者复制一份,改成你想同步的目录就可以了,

One more thing

下面是 rsync 的语法说明:
rsync [-avrlptgoD] [-e ssh] [user@host:/dir] [/local/path]
-v :观察模式,可以列出更多的信息;
-q :与 -v 相反,安静模式,输出的信息比较少;
-r :递归复制!可以针对『目录』来处理!很重要!
-u :仅更新 (update),不会覆盖目标的新档案;
-l :复制连结文件的属性,而非连结的目标源文件内容;
-p :复制时,连同属性 (permission) 也保存不变!
-g :保存源文件的拥有群组;
-o :保存源文件的拥有人;
-D :保存源文件的装置属性 (device)
-t :保存源文件的时间参数;
-I :忽略更新时间 (mtime) 的属性,档案比对上会比较快速;
-z :加上压缩的参数!
-e :使用的信道协议,例如使用 ssh 通道,则 -e ssh
-a :相当于 -rlptgoD ,所以这个 -a 是最常用的参数了!

最后为了让他每个小时同步一次我们还需要用到crontab

crontab -e
1 */1 * * * rsync -avzP --password-file=/etc/rsyncd.pwd backup@192.168.1.1::wwwroot  /home/wwwroot

1 */1 * * * rsync -avzP --password-file=/etc/rsyncd.pwd backup@192.168.1.1::www  /home/www

每隔1小时的第一分钟执行一次任务

 

评论

此博客中的热门博文

阿里云通过Nginx中转Linode/digitalocean/AWS的V2ray协议,实现负载均衡

开篇文的废话.原因是Sandy做站服务器太多了,发现利用率不高,皮又痒了又想折腾了.v2ray是利器但是给人的感觉就是只能用一台服务器,官方也说不支持负载均衡,不会A服务器挂机,自动跳转到B服务器.这里就有点折磨人了.每个服务器挂了都要自己去改配置文件. 我们访问国外的服务器,如果没CN2的线路是非常慢的,起码不快!哈哈~现在VPS都便宜起码人手一个人有5台以上吧?这样的话我们就可以开整. 开整条件:国内VPS或者阿里云有CN2线路的机器最好是100MB-200MB的带宽的.保证你爽歪歪.但是Sandy在阿里云HK30MB下也测试了也可以达到30000KB以上,羡慕吧~我们可以开始了! 老规则工具先出: 没被gwf和谐的服务器1台(下文统称:主服务器) 国外服务器2台以上(下文统称:从服务器) nginx 1.9.5版本以上(推荐:lnmp1.4) v2ray croe 第一步: 在 主服务器 安装lnmp1.4,这里就不细说教程了.直接去 官方 看教程 安装完毕以后我们来升级Nginx让他支持stream模块 cd lnmp1.4 vi lnmp.conf 在Nginx_Modules_Options=‘’里面加入--with-stream Nginx_Modules_Options='--with-stream’ 然后执行升级命令 ./upgrade.sh nginx  第二步 修改nginx.conf文件 cd /usr/local/nginx/conf/nginx.conf 在 events { use epoll; worker_connections 51200; multi_accept on; } 下面加入 stream{ server{ listen 0.0.0.0:8080; proxy_pass v2ray_backend; proxy_timeout 10m; proxy_connect_timeout 5000ms; } upstream v2ray_backend{ server ...

利用haproxy来做V2RAY负载均衡

Sandy在之前有说过 阿里云通过Nginx中转Linode/digitalocean/AWS的V2ray协议,实现负载均衡 ,有些学童不想折腾nginx,那么我们可以用haproxy来折腾,不管是haproxy还是Nginx都可以在树莓派和局域网的PC机器折腾! 安装haproxy的教程就移步到-> haproxy安装教程 apt-get install haproxy 安装完毕,我们来修改haproxy的配置 vi /etc/haproxy/haproxy.cfg 里面内容全部删除,加入下面的内容 global ulimit-n 51200 log /dev/log local0 log /dev/log local1 notice chroot /var/lib/haproxy pidfile /var/run/haproxy.pid user haproxy group haproxy daemon defaults log global mode tcp retries 3 option dontlognull option redispatch #上游TCP服务器连接等待时间 timeout connect 5s timeout client 24h timeout server 24h #以下为Web管理页面设置,如不需要可以删除 listen admin_stats bind 0.0.0.0:8888 mode http log 127.0.0.1 local0 err stats refresh 30s stats uri /haproxy stats realm welcome login\ Haproxy stats auth admin:admin stats hide-version stat...

ssh over socks5:通过socks5 proxy来连接ssh服务器[转载]

最近因为不可描述的原因,我在aws soul的云主机访问不了,ssh、80、ss全部都被禁掉了。 80端口在chrome配置SwitchyOmega就可以了,但ssh不太好办,shell上配置http_proxy对ssh没什么用。 其实ssh自己就支持proxy。 注意这跟在ssh端口转发:远程和本地中介绍过使用ssh作为proxy的方法不同,ssh -D是为了用ssh做proxy,而不是通过proxy来连接ssh服务器。 TL;DR ssh -o ProxyCommand='nc -x 192.0.2.0:1080 %h %p' user@awshost 原理解析 $ man ssh_config ProxyCommand Specifies the command to use to connect to the server. The command string extends to the end of the line, and is executed using the user's shell ‘exec’ directive to avoid a lingering shell process. In the command string, any occurrence of ‘%h’ will be substituted by the host name to connect, ‘%p’ by the port, and ‘%r’ by the remote user name. The command can be basically anything, and should read from its standard input and write to its standard output. It should eventually connect an sshd(8) server running on some machine, or execute sshd -i somewhere. Host key management will be done using the HostName of the host being connected (defa...