某公司里有一台web服务器,里面的数据很重要,但是如果硬盘坏了,数据会丢失,现在领导要求你把数据在其他机器上做一个周期性定时备份。要求如下:
每天晚上00点整在web服务器A上打包备份网站程序目录并通过rsync命令推送到服务器B上备份保留(备份思路可以是现在本地按日期打包,然后再利用rsnc推到备份服务器上)
具体要求如下:
1)web服务器A和备份服务器B的备份目录必须都为/backup
2)web服务器站点目录假定为(/var/www/html)
3)web服务器本地仅保留7天内的备份
4)备份服务器上检查备份结果是否正常,并将每天的备份结果发给管理员邮箱
5)备份服务器上每周六的数据都保留,其他备份仅保留180天备份
思路:
1.配置客户端和服务端
2.写脚本
3.测试
一、Rsync服务器的安装
1安装准备
1.1查看rsync的版本号
[root@backup ~]# rsync --version
rsync version 3.0.6 protocol version 30
Copyright (C) 1996-2009 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:
64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
append, ACLs, xattrs, iconv, symtimes
rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you
are welcome to redistribute it under certain conditions. See the GNU
General Public Licence for details.
1.2 查看服务器的内核,版本信息
[root@backup ~]# cat /etc/redhat-release
CentOS release 6.5 (Final)
[root@backup ~]# uname -r
2.6.32-431.el6.x86_64
[root@backup ~]# uname -m
x86_64
2.通过daemon实现数据同步
2.1/etc/rsyncd.conf是rsync的默认配置文件,该配置文件不存在,需要编辑内容
[root@backup ~]# cat /etc/rsyncd.conf
#rsync_config_____________________________start
#created by wang 15:01 20018-6-5
##rsyncd.conf start##
uid = rsync ##进程对应的用户,是虚拟用户。远端的命令使用rsync访问共享目录
gid = rsync ##进程对应的用户组。
use chroot = no ##安全相关
max connections = 200 ##最大连接数
timeout = 300 ##超时时间
pid file = /var/run/rsyncd.pid ##进程对应的进程号文件
lock file = /var/run/rsyncd.lock ##锁文件
log file = /var/log/rsyncd.log ##日志文件
[backup] ###模块名称
path = /backup ###服务器提供访问的目录
ignore errors ##忽略错误
read only = false ##可写
list = false ##不能列表
hosts allow = 172.16.1.0/24 ##允许的ip地址
##hosts deny = 0.0.0.0/32
auth users = rsync_backup ##虚拟用户
secrets file = /etc/rsync.password ###虚拟密码
#rsync_config________________________end
其中rsync用户默认是不存在的,需要创建用户
[root@backup ~]# useradd rsync -s /sbin/nologin -M
[root@backup ~]# cat /etc/passwd|grep rsync
rsync:x:501:501::/home/rsync:/sbin/nologin
[root@backup~]# id rsync
uid=501(rsync) gid=501(rsync) 组=501(rsync)
![](https://i-blog.csdnimg.cn/blog_migrate/7f9b04a4ad2e08dbc280ff6ff6f61695.png)
为什么用虚拟用户?
应答:文件和进程都要满足属主的要求,文件和进程的存在一定是需要用户的,也是为了安全问题。
创建/backup目录,并且属主和属组都属于rsync
[root@ backup ~]# mkdir /backup/ -p
[root@ backup ~]# chown -R rsync.rsync /backup/
[root@backup ~]# ls -ld /backup/
drwxr-xr-x 2 rsync rsync 4096 12月 9 2016 /backup/
![](https://i-blog.csdnimg.cn/blog_migrate/94dd2bc0c67b5221028fdf3b476ff84a.png)
创建配置文件/etc/rsync.password,默认不存在这个配置文件
[root@ backup ~]# cat /etc/rsync.password
rsync_backup:123456
[root@ backup ~]#chmod 600 /etc/rsync.password
[root@ backup ~]# ls -l /etc/rsync.password
-rw-------. 1 root root 20 11月 29 01:14 /etc/rsync.password
启动服务:
[root@ backup ~]# rsync --daemon[root@ backup ~]#ps -ef|grep rsync|grep -v grep ##查看进程有没有启动
root 1075 1 0 14:41 ? 00:00:00 /usr/bin/rsync --daemon
加入开机自启动
[root@ backup~]# tail -1 /etc/rc.local
/usr/bin/rsync --daemon
![](https://i-blog.csdnimg.cn/blog_migrate/9573ce1a08b2b31a7ebe24df99d12d6b.png)
二、Rsync客户端的安装
编辑配置文件/etc/rsync.passwd,该配置文件默认不存在
[root@nfs01 ~]# vim /etc/rsync.passwd
[root@nfs01 ~]# cat /etc/rsync.password
123456
[root@nfs01 ~]#chmod 600 /etc/rsync.passwd
[root@nfs01 ~]# ls -l /etc/rsync.password
创建backup目录
[root@nfs01 ~]# mkdir -p /backup
[root@nfs01 ~]# cd /backup/
[root@nfs01 backup]# touch {1..5}.txt
[root@nfs01 backup]# ls
1.txt 2.txt 3.txt 4.txt 5.txt
![](https://i-blog.csdnimg.cn/blog_migrate/32ca55a2690da8bac843249c3907fc77.png)
客户端推送:
方法1:
[root@nfs01 backup]# rsync -avz /backup/ rsync_backup@172.16.1.41::backup/ --password-file=/etc/rsync.password
方法2:
[root@nfs01backup]#rsync-avz /backup/ rsync://rsync_backup@172.16.1.41/backup/ --password-file=/etc/rsync.password
注:当传输过程中没有出现@ERROR字样,一般都传输成功
服务器端查看:
[root@backup ~]# cd /backup/
[root@backup backup]# ls
1.txt 2.txt 3.txt 4.txt 5.txt
三、编写脚本
1.客户端脚本
[root@nfs01 backup]# cat /server/scripts/bak.sh
IP=$(ifconfig eth1|awk -F "[ :]+" 'NR==2 {print $4}')
Path=/backup
if [ $(date +%w) -eq 0 ]
then
Time="week_$(date +%F_%w -d "-1day")"
else
Time=$(date +%F -d "-1day")
fi
mkdir $Path/$IP/ -p
cd / &&\
tar zcfh $Path/$IP/backup_$Time.tar.gz var/spool/cron/root etc/rc.local etc/sysconfig/iptables var/www/html app/logs &&\
md5sum $Path/$IP/backup_$Time.tar.gz >$Path/$IP/flag_$Time.log &&\
rsync -az $Path/ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password &&\
find $Path -type f -mtime +7 \( -name "*.tar.gz" -o -name "*.log" \) |xargs rm -f
2.服务器端脚本
[root@backup backup]# cat /server/scripts/check_and_del.sh
Path=/backup
if [ $(date +%w) -eq 0 ]
then
Time="week_$(date +%F_%w -d "-1day")"
else
Time=$(date +%F -d "-1day")
fi
LANG=en
find $Path -type f -name "*${Time}*.log"|xargs md5sum -c >>$Path/${Time}_result.log 2>&1
mail -s "$Time bak result" 1539975717@qq.com <$Path/${Time}_result.log
find $Path -type f -mtime +180 ! -name "*week*_6*"|xargs rm -f
3.设置mail的参数,不然发送邮件就不能实现。
1)查看是否安装mailx,没有安装yum install mailx -y
[root@backup backup]# rpm -qa mailx
mailx-12.4-7.el6.x86_64
2)配置vi /etc/mail.rc
[root@backup backup]# vi /etc/mail.rc 在末尾添加以下参数
set from=******@qq.com
set smtp=smtps://smtp.qq.com:465
set smtp-auth-user=******@qq.com
set smtp-auth-password=********* (不是密码,是POP3/SMTP服务的授权码)
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/
四、测试
客户端执行脚本:/server/scripts/bak.sh
[root@nfs01 backup]# sh /server/scripts/bak.sh
服务器端查看:
[root@backup backup]# ls
172.16.1.31
[root@backup backup]# cd 172.16.1.31/
[root@backup 172.16.1.31]# ls
backup_2018-06-06.tar.gz flag_2018-06-06.log
服务器端执行脚本:/server/scripts/check_and_del.sh
[root@backup ~]# sh /server/scripts/check_and_del.sh
[root@backup ~]# Error in certificate: Peer's certificate issuer is not recognized.
测试成功
五、其中遇见的报错:
1.rsync: failed to set times on "." (in backup): Permission denied (13)
客户端报错信息:
[root@nfs01 backup]# touch backup.txt
[root@nfs01 backup]# rsync -avz /backup/ rsync_backup@172.16.1.41::backup/ --password-file=/etc/rsync.password
sending incremental file list
./
rsync: failed to set times on "." (in backup): Permission denied (13)
backup.txt
rsync: mkstemp ".backup.txt.ytujAS" (in backup) failed: Permission denied (13)
sent 80 bytes received 30 bytes 220.00 bytes/sec
total size is 0 speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]
服务端rsync日志信息:
2018/06/07 08:56:09 [1171] connect from nfs01 (172.16.1.31)
2018/06/07 08:56:09 [1171] rsync to backup/ from rsync_backup@nfs01 (172.16.1.31)
2018/06/07 08:56:09 [1171] receiving file list
2018/06/07 08:56:09 [1171] rsync: failed to set times on "." (in backup): Permission denied (13)
2018/06/07 08:56:09 [1171] rsync: mkstemp ".backup.txt.ytujAS" (in backup) failed: Permission denied (13)
2018/06/07 08:56:09 [1171] sent 255 bytes received 137 bytes total size 0
一开始是以为是文件目录和用户权限问题,但是做了相关调试了之后还是一样,排除文件和用户权限问题。然后再仔细对比了一下配置文件,没错啊。然后想到是不是SELinux的问题
[root@backup backup]# getenforce
Enforcing
[root@backup backup]# setenforce 0
[root@backup backup]# getenforce
Permissive
2.开机起不来:
centos系统重启报错:checking filesystems failedchecking filesystems/dev/mapper/vg_0-root
看到提示
/dev/sda1:Superblock last mount time in the future
想起来为了试验取某个特定时间,将系统时间调整到了16年1月6日,而当前时间为2018年6月6日
再次进入命令行,通过时间命令设置成最新时间,重启问题解决
date -s "20180606"