余苏明的幻想乡

部署rsync远程备份

写在前面

年终了!要回去过年,担心各个设备出问题,要一个备份机器将其他设备上的程序同步做灾备。

实施过程

其中52为备份客户端,51为服务端

打通2.52与2.51之间的ssh免密码登陆(root用户)

  1. 备份服务器52上使用命令ssh-keygen -t rsa -P '' -f /root/.ssh/id_rsa一次生成

    或者运行ssh-keygen -t rsa**然后敲击三次回车生成密钥对

  2. 如果在2.51服务端上的/root/.ssh/目录下没有authorized_keys文件,则生成:touch authorized_keys
  3. 修改权限chmod 600 authorized_keys
  4. 将客户端上生成的id_rsa.pub追加到服务端authorized_keys文件中

    1. 服务端51上使用scp命令将远程服务器上的id_rsa.pub下载到本地服务器
      scp -p root@192.168.2.52:/root/.ssh/id_rsa.pub /root/.ssh/id_rsa.pub_192.168.2.52

    2. 追加到authorized_keys文件中

      cat /root/.ssh/id_rsa.pub_192.168.2.52 >>authorized_keys

  5. 修改ssh的配置文件/etc/ssh/sshd_config将下列三行注释去掉

    1
    2
    3
    4
    5
    6
    7
    8
    #RSAAuthentication yes
    #PubkeyAuthentication yes
    #AuthorizedKeysFile .ssh/authorized_keys
    ##################修改后#############################
    RSAAuthentication yes
    PubkeyAuthentication yes
    AuthorizedKeysFile .ssh/authorized_keys
  6. 重启sshd服务:service sshd restart

  7. 到此已经打通2.51和2.52之间的ssh
  8. 保险起见,将每个服务器上的id_rsa.pub_*文件删除rm -f /root/.ssh/id_rsa.pub_*

使用rsync同步两台服务器程序

修改51上的/etc/rsyncd.conf文件,没有则创建。权限644

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example:
# uid = nobody
# gid = nobody
uid = root
gid = root
# use chroot = yes
use chroot = no
# max connections = 4
max connections = 4
# pid file = /var/run/rsyncd.pid
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
timeout = 600
# ignore nonreadable = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
[exp_1]
path = /exp
comment = bakup exp_1_zipingtai #此行为备注行,可随意更改
ignore errors = yes
read only = yes
list = no
hosts allow = 192.168.2.52 #限定允许访问的ip
hosts deny = 0.0.0.0/0 #拒绝所有其他ip使用rsync同步
exclude = log/* #过滤文件及目录为相对path的路径
auth users = root
secrets file = /etc/backserver.pas
[tomcat]
path = /usr/tomcat
comment = bakup tomcat
ignore errors = yes
read only = yes
list = no
hosts allow = 192.168.2.52
hosts deny = 0.0.0.0/0
exclude = log/* logs/* webapps/ROOT/* work/*
auth users = root
secrets file = /etc/backserver.pas
# 要同步其他程序可自行按上行格式同步

生成认证密码文件/etc/backserver.pas

执行下列命令

1
2
3
4
# root密码为123456;可以与登陆密码不一致
echo root:123456 > /etc/backserver.pas
# 修改文件权限,否则服务起不来
chmod 400 /etc/backserver.pas

启动服务端

1
2
3
4
5
rsync --daemon
# 观察是否启动成功,下列三种命令皆可
lsof -i :873
netstat -an|grep 873
ps -ef|grep rsync

客户端配置

  1. 创建密码文件
1
2
3
echo 123456 > /root/backserver.pas #与服务端密码一致
# 更改密码文件权限,否则报错
chmod 600 /root/backserver.pas
  1. 创建对应文件夹
    mkdir -pv /bak/exp_1
  2. 执行下面命令
1
rsync -vzrtopg --bwlimit=1000 --delete --progress root@192.168.2.51::exp_1 /bak/exp_1/ --password-file=/root/backserver.pas

--bwlimit参数是限定同步速度为1000kB/s
--delete参数是同步时发现客户端有而服务端没有的文件则删除

优化

  1. 可以将exclude参数写入服务端配置文件中,例如

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    exclude = bin/ history/ #多个目录使用空格隔开
    # 如果过滤文件较多,可以使用参数exclude from
    # 注意:
    ##########################
    # 过滤目录不是按绝对值目录匹配的
    # 例如有两个目录bin/和foo/bin/
    # 则该参数会将两个bin目录都过滤掉
    # 如果要过滤foo/bin/则需要用foo/bin/
    # exclude = foo/bin/
    ###########################
    exclude from = /home/exclude.list
    #exclude.list每行为一个参数,参数为相对路径
  2. 编写autobakup.sh备份脚本

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    #!/bin/bash
    #auther: summingbest@gmail.com
    MONTH=`date +"%Y%m"`
    DAY=`date +"%d"`
    HOUR=`date +"%H"`
    MIN=`date +"%M"`
    tarname=/bak/exp/exp_1_${MONTH}${DAY}${HOUR}.tar.gz
    [ ! -d /bak/exp ] && mkdir -p /bak/exp;
    [ ! -d /bak/exp/exp_1 ] && mkdir -p /bak/exp/exp_1;
    [ ! -d /bak/exp/tomcat ] && mkdir -p /bak/exp/tomcat;
    #删除7天前的备份文件
    find /bak/exp -name "exp_1_*.tar.gz" -mtime +7 -type f|xargs rm -f
    rsync -vzrtopg --bwlimit=1000 --delete --progress root@192.168.2.51::exp_1 /bak/exp/exp_1/ --password-file=/root/backserver.pas
    rsync -vzrtopg --bwlimit=1000 --delete --progress root@192.168.2.51::tomcat /bak/exp/tomcat/ --password-file=/root/backserver.pas
    tar -zcvf ${tarname} /bak/exp/exp_1/ /bak/exp/tomcat/
  3. 可以在客户端 将脚本放入指定的/bak/bin目录,并使用命令加入crontab,定时执行

1
echo '20 9 * * * /bin/sh /bak/bin/autobakup.sh' >> /var/spool/cron/root

在其他服务器上配置

只需要更改一些对应的参数即可

参考文献

  1. ssh/scp免密码登陆
  2. rsync简单配置
  3. rsync百度文库详细参数
  4. rsync常见错误