使用rsync进行同步备份
rsync
是常用的数据镜像备份工具,它可以:
- 可以镜像保存整个目录树和文件系统。
- 可以很容易做到保持原来文件的权限、时间、软硬链接等等。
- 无须特殊权限即可安装。
本文记录使用rsync
进行系统备份的基本方法。
1、修改目标机(Server端)的rsync
为daemon
模式
编辑/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 | sudo mkdir /etc/rsync |
修改后的文件如下,特别注意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_pub
和ftp_other
,这两个模块同步位置各有不同。
3、创建密码文件:
1 | sudo vi /etc/rsync/rsyncd.secrets |
内容如下: 1
2# 用户名:密码
user:123456
4、创建exclude list
:
分别创建以下两个`list
1 | sudo vi /etc/rsync/exclude_rules_other.conf |
文件内容根据需要,一行一个路径,注意,路径均是针对配置文件中path
的相对路径,绝对路径为$(path)/exclude_path...
。
5、重启rsync
服务:
1 | sudo service rsync restart |
以上服务端配置完成,客户端几乎无配置,只需要配置一个本地的密码文件,设该文件为/etc/rsync/rsync.pwd
,内容为访问用户的密码: 1
123456
!注意:下面这两步操作是必须的,没有足够权限同步就会不成功 1
2sudo chmod 0600 /etc/rsync/rsync.pwd
sudo chown user:usergroup /etc/rsync/rsync.pwd
6、运行
1 | rsync -avlrtopg --progress --delete user@10.3.89.196::ftp_pub/ --password-file=/etc/rsync/rsync.pwd /wd_1t/ftpService/pub/ |
7、使用crontab
自动执行
将第6点中的两个命令保存为一个shell
脚本文件,设文件名为~/rsync_job.sh
,编辑任务列表,并添加合适的周期:
1 | crontab -e |
配置文件如下:
1 | # Edit this file to introduce tasks to be run by cron. |
配置完成编辑后,重新载入cron
服务,使配置生效,命令如下:
1 | sudo service cron restart |
关于时间格式的补充说明:
1 | '*' :表示所有可能的值,例如month字段使用*,在满足其它字段的制约条件后每月都执行该命令操作。 |
参考: