定期备份数据(centos)

备份相关问题:
1 需要在被备份数据的机器A上启动rsync的服务端,即启用相关进程。也可以根据需求在机器B上启动相关进程。故需要根据实际情况确定好服务器和客户端。
2 在备份的机器B上可以通过crontab来定时备份由A指定的数据,也可以通过inotify来实现。
3 A上配置文件/etc/rsyncd.conf 内容如下:
#运行RSYNC守护进程的用户 
uid = root
#运行RSYNC守护进程的组 
gid = root
use chroot = no
#不使用chroot 
max connections = 4
 # 最大连接数为4 
strict modes =yes
 #是否检查口令文件的权限 
port = 873
#默认端口873 
pid file = /var/run/rsyncd.pid 
lock file = /var/run/rsync.lock  
log file = /var/log/rsyncd.log 
 
[database]
#这里是认证的模块名,在client端需要指定 
path = /data/
 #需要做镜像的目录,不可缺少! 
comment = This is dev mysql database backup
#这个模块的注释信息  
ignore errors
 #可以忽略一些无关的IO错误 
read only = no
#只读 
list = no
#不允许列文件 
auth users = backup
 #认证的用户名,如果没有这行则表明是匿名,此用户与系统无关 
secrets file = /etc/rsync.password
 #密码和用户名对比表,密码文件自己生成 
hosts allow = 192.168.3.24,192.168.3.25
#允许主机 
#hosts deny = *
#禁止主机
uid = root
gid = root
[test]
 #这里是认证的模块名,在client端需要指定 
path = /home/jaiying/
 #需要做镜像的目录,不可缺少! 
comment = This is dev mysql database backup
 #这个模块的注释信息  
ignore errors
 #可以忽略一些无关的IO错误 
read only = no
# 只读 
write only = no
list = no
 #不允许列文件 
auth users = test
 #认证的用户名,如果没有这行则表明是匿名,此用户与系统无关 
secrets file = /etc/rsync.password
 #密码和用户名对比表,密码文件自己生成 
hosts allow = *
#hosts allow = 192.168.3.27,192.168.3.24#允许主机 
#hosts deny = *
#禁止主机
uid = root
gid = root
服务器端的相关命令
echo "backup:654321" >/etc/rsync.password  //写入密码到密码文件
chmod 600 /etc/rsync.password            //设置密码文件权限为600读写
cat /etc/rsync.password      //查看密码是否成功写入密码文件
ll /etc/rsync.password       //查看密码文件权限是否为600
rsync --daemon          //启动rsync
echo "/usr/bin/rsync --daemon" >>/etc/rc.local  //将文件写入自启动文件中
cat /etc/rc.local|grep daemon //查看是否成功写入
ps -ef|grep rsync  //显示现行终端机下的所有程序并过滤出与rsync有关的
pkill rsync    //结束rsync
rsync --daemon    //启动rsync
4 在B上执行如下的命令
echo "654321" >/etc/rsync.password    //将密码654321写入密码文件中
chmod 600 /etc/rsync.password   //设置密码文件权限为600
cat /etc/rsync.password        //查看密码是否成功写入密码文件
ll /etc/rsync.password       //查看密码文件权限是否成功写入600权限
rsync -vzrtopg --delete --progress --password-file=/etc/rsync.password [email protected]::test /data/dev_database
5 通过inotify来实现,inotify监控节点文件的变化,如果文件有变动,那么就启动rsync,将文件实时同步到服务节点上。
服务节点启动rsync服务进程,相关配置文件和命令如下:
vi /etc/rsyncd.conf
#运行RSYNC守护进程的用户 
uid = root
#运行RSYNC守护进程的组 
gid = root
use chroot = no
#不使用chroot 
max connections = 4
 # 最大连接数为4 
strict modes =yes
 #是否检查口令文件的权限 
port = 873
#默认端口873 
pid file = /var/run/rsyncd.pid 
lock file = /var/run/rsync.lock  
log file = /var/log/rsyncd.log 
 
[database]
#这里是认证的模块名,在client端需要指定 
path = /data/dev_database
 #需要做镜像的目录,不可缺少! 
comment = This is dev mysql database backup
#这个模块的注释信息  
ignore errors
 #可以忽略一些无关的IO错误 
read only = no
#只读 
list = no
#不允许列文件 
auth users = backup
 #认证的用户名,如果没有这行则表明是匿名,此用户与系统无关 
secrets file = /etc/rsync.password
 #密码和用户名对比表,密码文件自己生成 
hosts allow = 192.168.7.8
#允许主机 
hosts deny = *
#禁止主机
uid = root
gid = root
[test]
 #这里是认证的模块名,在client端需要指定 
path = /home/jiaying/
 #需要做镜像的目录,不可缺少! 
comment = This is dev mysql database backup
 #这个模块的注释信息  
ignore errors
 #可以忽略一些无关的IO错误 
read only = no
# 只读 
write only = no
list = no
 #不允许列文件 
auth users = test
 #认证的用户名,如果没有这行则表明是匿名,此用户与系统无关 
secrets file = /etc/rsync.password
 #密码和用户名对比表,密码文件自己生成 
hosts allow = *
#hosts allow = 192.168.7.8#允许主机 
#hosts deny = 0.0.0.0/0 #禁止主机
uid = root
gid = root
先进行测试:
rsync -vzrtopg --delete --progress --password-file=/etc/rsync.password /home/jaiying [email protected]::test
如果没有crontab,进行安装 yum install cronie.x86_64
crontab -e进行编辑,进行定时备份
50 11 * * * rsync -vzrtopg --delete --progress --password-file=/etc/rsync.password /data [email protected]::database
在要出发的机器上安装inotify
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
#tar zxvf inotify-tools-3.14.tar.gz
#cd inotify-tools-3.14
#./configure
#make
#make install
#ll /usr/local/bin/inotifywa*
-rwxr-xr-x 1 root root 37264 04-14 13:42 /usr/local/bin/inotifywait
-rwxr-xr-x 1 root root 35438 04-14 13:42 /usr/local/bin/inotifywatch
编写脚本
#脚本1(同步/home/jaiying 目录)
vi rsync.sh
#!/bin/bash
src=/home/jaiying
dst1=test
/usr/local/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/rsync.password $src [email protected]::$dst1
                echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
         done

猜你喜欢

转载自blog.csdn.net/wzlsunice88/article/details/79623486
今日推荐