使用rsync进行同步备份

  rsync是常用的数据镜像备份工具,它可以:

  1. 可以镜像保存整个目录树和文件系统。
  2. 可以很容易做到保持原来文件的权限、时间、软硬链接等等。
  3. 无须特殊权限即可安装。

  本文记录使用rsync进行系统备份的基本方法。

1、修改目标机(Server端)的rsyncdaemon模式

  编辑/etc/default/rsync文件,将RSYNC_ENABLE=false改为true,然后重新启动rsync

1
sudo vi /etc/default/rsync

  修改为daemon模式

1
2
3
4
5
6
7
# start rsync in daemon mode from init.d script?
# only allowed values are "true", "false", and "inetd"
# Use "inetd" if you want to start the rsyncd from inetd,
# all this does is prevent the init.d script from printing a message
# about not starting rsyncd (you still need to modify inetd's config yourself).
# RSYNC_ENABLE=false
RSYNC_ENABLE=true

2、复制一份rsyncd.conf文件并修改

1
2
3
4
5
sudo mkdir /etc/rsync
sudo cp /usr/share/doc/rsync/examples/rsyncd.conf /etc/rsync
cd /etc
# 这里注意,做了一个软连接
sudo ln -s rsync/rsyncd.conf rsyncd.conf

  修改后的文件如下,特别注意auth users字段,指允许的用户

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# sample rsyncd.conf configuration file

# GLOBAL OPTIONS

#motd file=/etc/motd
log file=/var/log/rsyncd
# for pid file, do not use /var/run/rsync.pid if
# you are going to run rsync out of the init.d script.
# The init.d script does its own pid file handling,
# so omit the "pid file" line completely in that case.
pid file=/var/run/rsyncd.pid
syslog facility=daemon
#socket options=

# MODULE OPTIONS

[ftp_pub]

comment = public archive
path = /wd_4t_sda/ftpService/pub/
use chroot = yes
# max connections=10
lock file = /var/lock/rsyncd
# the default for read only is yes...
read only = yes
list = yes
uid = nobody
gid = nogroup
# exclude =
exclude from = /etc/rsync/exclude_rules_pub.conf
# include =
# include from =
auth users = user
secrets file = /etc/rsync/rsyncd.secrets
strict modes = yes
hosts allow = 10.3.89.98
hosts deny = *
# ignore errors = no
ignore errors = yes
ignore nonreadable = yes
# transfer logging = no
transfer logging = yes
log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes.
timeout = 600
refuse options = checksum dry-run
dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz *.7z

[ftp_other]
comment = public archive
path = /wd_4t_sda/ftpService/
use chroot = yes
# max connections=10
lock file = /var/lock/rsyncd
# the default for read only is yes...
read only = yes
list = yes
uid = nobody
gid = nogroup
# exclude =
exclude from = /etc/rsync/exclude_rules_other.conf
# include =
# include from =
auth users = user
secrets file = /etc/rsync/rsyncd.secrets
strict modes = yes
hosts allow = 10.3.89.98
hosts deny = *
# ignore errors = no
ignore errors = yes
ignore nonreadable = yes
# transfer logging = no
transfer logging = yes
log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes.
timeout = 600
refuse options = checksum dry-run
dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz *.7z

  上述配置文件中,配置了2个模块,ftp_pubftp_other,这两个模块同步位置各有不同。

3、创建密码文件:

1
sudo vi /etc/rsync/rsyncd.secrets

内容如下:

1
2
# 用户名:密码
user:123456

4、创建exclude list:

  分别创建以下两个`list

1
2
sudo vi /etc/rsync/exclude_rules_other.conf
sudo vi /etc/rsync/exclude_rules_pub.conf

  文件内容根据需要,一行一个路径,注意,路径均是针对配置文件中path的相对路径,绝对路径为$(path)/exclude_path...

5、重启rsync服务:

1
sudo service rsync restart

  以上服务端配置完成,客户端几乎无配置,只需要配置一个本地的密码文件,设该文件为/etc/rsync/rsync.pwd,内容为访问用户的密码:

1
123456

  !注意:下面这两步操作是必须的,没有足够权限同步就会不成功

1
2
sudo chmod 0600 /etc/rsync/rsync.pwd
sudo chown user:usergroup /etc/rsync/rsync.pwd

6、运行

1
2
rsync -avlrtopg --progress --delete user@10.3.89.196::ftp_pub/ --password-file=/etc/rsync/rsync.pwd /wd_1t/ftpService/pub/ 
rsync -avlrtopg --progress --delete user@10.3.89.196::ftp_other/ --password-file=/etc/rsync/rsync.pwd /wd_10t_sdc/ftpService/others/

7、使用crontab自动执行

  将第6点中的两个命令保存为一个shell脚本文件,设文件名为~/rsync_job.sh,编辑任务列表,并添加合适的周期:

1
crontab -e

  配置文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
# 每周一、周四和周六,凌晨2点执行脚本
0 2 * * 1,4,6 bash /home/user/rsync_job.sh

  配置完成编辑后,重新载入cron 服务,使配置生效,命令如下:

1
sudo service cron restart

  关于时间格式的补充说明:

1
2
3
4
'*' :表示所有可能的值,例如month字段使用*,在满足其它字段的制约条件后每月都执行该命令操作。
',' :用于指定多个单独值,例如,“1,2,5,7,8,9”,
'-' :用于连接两个整数,表示整数范围,例如“2-6”表示“2,3,4,5,6”
'/' :指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。如果'*'与之一起使用,表示每个xx,例如 ' */10',表示每隔10(分钟/小时/等)执行一次。

参考:

  1. https://blog.csdn.net/yimingsilence/article/details/52153732