linux文件热备份

本文介绍如何利用rsync和inotify实现高效、实时的数据备份方案。通过具体步骤展示了如何配置rsync服务并结合inotify监控文件变化,实现自动同步。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、rsync

与传统的cp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。
随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。基于以上原因,rsync+inotify组合出现了!

2、inotify

 Inotify 是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。
在上面章节中,我们讲到,rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题。


正式配置:

基础环境:

       主服务器:192.168.1.222     操作系统:Ubuntu 14.04

       备份服务器:192.168.1.110    操作系统:ubuntu16.04


大致配置流程:

备份服务器:

1.安装rsync同步软件

2.拷贝/etc/rsyncd.conf

3.配置/etc/default/rsync

4.配置/etc/rsyncd.secrects

5.编辑/etc/rsyncd.conf

6.启动rsync


主服务器:

1.安装rsync同步软件和inotify-tools

2.配置/etc/rsyncd.secrects

3.配置/usr/bin/inotify2rsync

4.将脚本添加到开机加载文件中:

5.创建同步目录和赋予权限

备份服务器(192.168.1.110):

1.安装rsync同步软件:

     sudo apt-get install rsync

2.拷贝/etc/rsyncd.conf

守护进程需要调用配置文件来预先加载相关信息参数,文件默认为/etc/rsyncd.conf,但是软件安装后没有这个文件,不过我们可以从/usr/share/doc/rsync/examples/rsyncd.conf复制一份到/etc/rsyncd.conf。

sudo cp /usr/share/doc/rsync/examples/rsyncd.conf /etc/rsyncd.conf

3.配置/etc/default/rsync

  vim /etc/default/rsync

配置

RSYNC_ENABLE=true

RSYNC_CONFIG_FILE='/etc/rsyncd.conf'


4.配置/etc/rsyncd.secrects

在/etc/下建立rsyncd.secrects


vim /etc/rsyncd.secrects
yxw:123456

然后赋予权限:sudo chmod 600 /etc/rsyncd.secrects

保存退出

5.编辑/etc/rsyncd.conf

然后vim /etc/rsyncd.conf


# sample rsyncd.conf configuration file
 
# GLOBAL OPTIONS
 
#motd file=/etc/motd
#开户log日志
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
pid file=/var/run/rsyncd.pid
#syslog facility=daemon
#socket options=
 
# MODULE OPTIONS
 
[ftp]
 
        comment = common
        #指定同步目录 
        path = /var/www/public
        use chroot = yes
#       max connections=10
        lock file = /var/lock/rsyncd
# the default for read only is yes...
        #不只是读,还要写权限
        read only = no
        list = yes
        uid = root
        gid = root
#       exclude =
#       exclude from =
#       include =
#       include from =
        #指定同步校验用户(与主服务中用于同步时用户相同)
        auth users = yxw
        #指定同步校验用户时的密码文件
        secrets file = /etc/rsyncd.secrects
        strict modes = yes
        #指定host
        hosts allow = 192.168.1.222/255.255.255.0
        hosts deny = *
        ignore errors = no
        ignore nonreadable = yes
        transfer logging = no
        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

保存退出

6.启动rsync

我们现在启动rsync服务:

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

好了,执行ps -aux | grep rsync来查看一下软件运行情况:

好 启动成功(注意--config=/etc/rsyncd.conf,启动时必须要有这个)

备份服务器启动完之后,则可进行下一步



主服务器(192.168.1.222):

1.安装rsync同步软件和inotify-tools


首先在服务器上面安装软件:


sudo apt-get install rsync inotify-tools

2.配置/etc/rsyncd.secrects


建立rsync运行所需要的密码文件,并更改运行权限:


sudo echo -ne 123456 > /etc/rsyncd.secrects
chmod 600 /etc/rsyncd.secrects


3.配置/usr/bin/inotify2rsync


在/usr/bin/下面编辑脚本文件,使得inotify-tool检测到文件变化后可以自动运行rsync程序:


sudo touch /usr/bin/inotify2rsync
sudo vim /usr/bin/inotify2rsync


#!/bin/bash
src=/var/www/public
dst=yxw@192.168.1.110::ftp
 
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src | while read files
        do
                /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.secrects $src $dst
                echo "${files} was rsynced" >> /var/log/rsync.log 2>&1
        done
exit 0

src是需要同步的目录,dst是进行备份服务器的认证用户和主机ip,

--password-file=/etc/rsyncd.secrects是主服务器的密码文件

注意ftp对应备份服务器的


sudo chmod 755 /usr/bin/inotify2rsync

4.将脚本添加到开机加载文件中:


将脚本添加到开机加载文件中:


sudo vim /etc/rc.local

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
inotify2rsync
exit 0


保存退出

5.创建同步目录和赋予权限

同样建立同步数据所需要的文件夹,并对文件夹授权:

sudo mkdir /var/www/public

sudo chown -R yxw:123456 /var/www/public

重启服务器,完成配置。(不重启的话在后台运行命令inotify2rsync)


测试:
在主服务器的/var/www/public文件夹中添加文件或者文件夹,看看备份服务器上面的data文件夹是否发生改变

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值