Rsync

安装

操作系统为 Ubuntu 环境。

# 一般 Ubutnu 发行版系统会默认安装,没有的话可以通过 apt 安装
sudo apt-get update && sudo apt-get install rsync


备份

本地备份

以压缩的形态进行本机目录备份,用法上很接近 cp

rsync -azvh /home/path/folder1/ /home/path/folder2

远程备份

通过网络进行不同机器间的备份,可以满足内网的备份管理。

# 下面的 --dry-run 相当于模拟备份,真实执行的时候需要去掉这个参数
rsync --dry-run --delete -azvh -e ssh /home/path/folder1/ remoteuser@remotehost.remotedomain:/home/path/folder2

查看更多常用参数的使用。

--dry-run This tells rsync to not actually do anything. It will just write a log of what it would do to the screen. Once you've made sure everything will work as you expect, you have to remove this option, and run the command again to perform the actual backup.
 
--delete deletes files that don't exist on the system being backed up.(Optional)
 
-a preserves the date and times, and permissions of the files (same as -rlptgoD).
 
With this option rsync will:
Descend recursively into all directories (-r),
 
copy symlinks as symlinks (-l),
 
preserve file permissions (-p),
 
preserve modification times (-t),
 
preserve groups (-g),
 
preserve file ownership (-o), and
 
preserve devices as devices (-D).
 
-z compresses the data
 
-vv increases the verbosity of the reporting process
 
-e specifies remote shell to use


Rsync Daemon

Rsync 可以作为后台提供服务,你所要做的仅仅是完成一份备份计划表就可以随意的在各种主机上发起备份请求。

这样做伴随有一定的风险,官方解释的内容如下。

The rsync daemon is an alternative to SSH for remote backups. Although more difficult to configure, it does provide some benefits. For example, using SSH to make a remote backup of an entire system requires that the SSH daemon allow root login, which is considered a security risk. Using the rsync daemon allows for root login via SSH to be disabled.

为了使用这项服务,首先修改 Rsync 的配置文件。

/etc/default/rsync
RSYNC_ENABLE=inetd

为了安全性考量,需要安装 xinetd 管理 rsyncd 的进程。

xinetd是一个运行于类Unix操作系统的开放源代码的超级服务器(Super-server)守护进程。它的功能是管理网络相关的服务。由于其较高的安全性,xinetd开始逐渐取代inetd。
$ sudo apt-get -y install xinetd
/etc/xinetd.d/rsync
service rsync
{
    disable = no
    socket_type = stream
    wait = no
    user = root
    server = /usr/bin/rsync
    server_args = --daemon
    log_on_failure += USERID
    flags = IPv6
}

这样 rsyncd 会被 xinetd 接管进程,随后需要获取更多的权限。

下面的 share 就是你为要备份的数据做的计划,注意 user 要写成你这台机器自己的用户名。

/etc/rsyncd.conf
max connections = 2
log file = /var/log/rsync.log
timeout = 300
 
[share]
comment = Public Share
path = /home/share
read only = no
list = yes
uid = nobody
gid = nogroup
auth users = user
secrets file = /etc/rsyncd.secrets

再创建一个存储密码的文件,与上面的配置 /etc/rsyncd.secrets 和用户 user 当然是对应的,密码需要自行填写。

/etc/rsyncd.secrets
user:password

更改该存储密码文件的权限,防止其他用户随意读取。

$ sudo chmod 600 /etc/rsyncd.secrets

最后重启一下 xinetd 服务就完成了。

sudo /etc/init.d/xinetd restart

可以稍微在本地测试配置的正确性。

$ sudo rsync user@hostname::share
  Password: 
  drwxr-xr-x        4096 2006/12/13 09:41:59 .
  drwxr-xr-x        4096 2006/11/23 18:00:03 folders

这样就非常方便的使用到了配置文件中计划的方案了。


FIXME

Jovi Meng 2017/09/05 07:11