rsync syntax

参考:rsync



rsync 主要用于文件和目录的备份,会借助差异算法比较源端和目的端的相应文件,只传输更新过的内容,rsync 也支持压缩、加密等多种特性。

基本功能

$ rsync -avz /home/pi pi@192.168.1.111:/home/pi/bak

pi文件夹复制到192.168.1.111中的bak文件夹中,-a表示归档模式,-v表示在stdout打印备份信息和进度,-z表示在网络传输时压缩数据。

-a相当于-rlptgoD-r递归目录,-l保留符号链接,-p保留权限信息,-t保留时间标记,-g保留所属组,-o保留所有者,-D保留设备及特殊文件信息。注意:-a包含了-r,如果这两个选项都没有使用,备份文件夹时会出错(skipping directory)。

关于文件夹路径末尾的/

$ rsync -av ./from ./bak   # ./bak/from/FILES
$ rsync -av ./from/ ./bak  # ./bak/FILES
$ rsync -av ./from ./bak/  # ./bak/FILES

rsync用ssh连接远程主机,应确保ssh连接可用。

可使用crontab -e添加定时备份:

0 */15 * * * rsync -az /home/code pi@192.168.1.111:/home/pi/bak

[TOP]


常用选项

--exclude "*.txt" 不备份.txt文件
--exclude-from ./css 不备份css目录
--delete 在目的端删除源端不存在的文件
-u 不同步目的端修改过的文件
-d 只同步目录信息,不同步文件
--exiting 不在目的端创建新文件
--ma-size='100k' 不传输超过100k的文件
-W 传输所有文件,相当于cp
-i 查看源端和目的端的文件差异,stog分别表示文件大小、时间、所有者、所属组更新


[TOP]


备份服务器

rsync配置文件(/etc/rsync/rsyncd.conf):

uid = www
gid = www
read only = true
transfer logging = true
max connections = 10
slp refresh = 300
hosts allow = 192.168.1.111
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
log format = %h %o %f %l %b

[web]
path = /www/web
comment = Mirror to backup server
read only = true
list = false
auth users = pi
secrets file = /etc/rsync/pass.conf
exclude from = /etc/rsync/exclude.txt

[mysql]
path = /www/mysql
comment = Mirror to MySql backup server
read only = true
list = false
auth users = pi
secrets file = /etc/rsync/pass.conf

配置文件/etc/rsync/pass.conf,保存用户名和验证密码:

pi:PASSWORD

设置该文件权限为600

$ sudo chmod 600 /etc/rsync/pass.conf

配置文件/etc/rsync/exclude.txt,指定不备份的目录:

/www/cache
/www/tmp

启动rsync服务器:

$ rsync --daemon --config=/etc/rsync/rsyncd.conf

建立密码文件:

$ mkdir /etc/rsync
$ touch /etc/rsync/pass.conf
$ cat /etc/rsync/pass.conf
PASSWORD # 不需要用户名,只填密码即可
$ sudo chmod 600 /etc/rsync/pass.conf

启动客户端:

$ rsync -avzP --delete --password-file=/etc/rsync/pass.conf pi@192.168.1.111::web /www/mirror

注意:服务器路径应使用::

[TOP]