全网备份案例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了全网备份案例相关的知识,希望对你有一定的参考价值。

第1章 案例及要求

1.1 案例
某公司里有一台web服务器,里面的数据很重要,但是如果硬盘坏了,数据就会丢失,现在领导要求你把数据在其他机器上做一个周期性定时备份
1.2 案例要求
1) 每天晚上00点整在web服务器A上打包备份重要目录并通过rsync命令推送到服务器B上备份
2) 需要备份的文件目录有(原则上,只要是运维人员写入或更改的数据,都需要备份)
/var/spool/cron/root /etc/rc.local /etc/sysconfig/iptables /var/www/html /app/logs
3) 为了规范化,每台web服务器进行本地备份时都备份到/backup/目录下
4) 每台web服务器进行本地备份时,都备份到/backup/下以本机IP地址命名的目录中
5) 打包的文件名中需要包含执行当天的日期
6) 为了方便知道每次备份是否成功,我们需要做如下操作
在每台web服务器上检查备份是否成功
在存储备份数据的服务器上检查备份数据是否成功,并发送邮件至管理员邮箱
7) 由于备份服务器空间有限,需要删除超过180天的备份数据,但每周六的备份数据需要永久保留

第2章 解答过程

2.1 配置rsync备份服务器,并在客户端实现推送
2.1.1 rsync备份服务器的配置过程
? 【查看安装环境】
[[email protected] ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)
[[email protected] ~]# uname -r
2.6.32-696.el6.x86_64
? 【查看是否有rsync的安装包】
[[email protected] ~]# rpm -qa rsync
rsync-3.0.6-12.el6.x86_64
? 【添加rsync用户,用来管理本地目录】
[[email protected] ~]# useradd rsync -s /sbin/nologin -M
[[email protected] ~]# id rsync
uid=501(rsync) gid=501(rsync) groups=501(rsync)
注意:该用户是虚拟用户,不需要进行登录
? 【生成配置文件/etc/rsyncd.conf】
[[email protected] ~]# vim /etc/rsyncd.conf
################################写入的内容如下#################################
#rsync_config____start
#created by oldboy
##rsyncd.conf start##
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
#log file = /rsync/rsyncd.log
[backup]
path = /backup/
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
#rsync_config_____end
[[email protected] ~]# cat /etc/rsyncd.conf #查看
注意:Centos6默认是不存在该文件的,但是Centos7是存在该文件的
? 【启动并检查是否启动成功】
[[email protected] ~]# rsync –daemon
[[email protected] ~]# ps -ef |grep rsync|grep -v grep
? 【建立共享目录并赋予权限】
[[email protected] ~]# mkdir /backup/
[[email protected] ~]# chown -R rsync.rsync /backup/
[[email protected] ~]# ls -ld /backup/
注意:如果目录不存在,会报错,详细错误见下面的错误总结
? 【创建密码文件并编辑】
[[email protected] ~]# vim /etc/rsync.password
[[email protected] ~]# cat /etc/rsync.password
rsync_backup:123456
注意:密码文件的格式为: 用户:密码
? 【更改密码文件的权限并检查】
[[email protected] ~]# chmod 600 /etc/rsync.password
[[email protected] ~]# ls -l /etc/rsync.password
-rw------- 1 root root 20 Jan 16 20:21 /etc/rsync.password
注意:密码文件的权限是600
? 【查看rsync服务的端口】
[[email protected] ~]# lsof -i :873
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsync 1201 root 4u IPv4 10536 0t0 TCP :rsync (LISTEN)
rsync 1201 root 5u IPv6 10537 0t0 TCP
:rsync (LISTEN)
[[email protected] ~]# netstat -tunlp|grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0: LISTEN 1201/rsync
tcp 0 0 :::873 :::
LISTEN 1201/rsync
? 【将rsync服务加入开机自启动】
[[email protected] ~]# echo "/usr/bin/rsync --daemon" >>/etc/rc.local
[[email protected] ~]# tail -1 /etc/rc.local
/usr/bin/rsync --daemon
2.1.2 客户端的配置
? 【建立密码文件,只需在文件中写入密码即可】
[[email protected] ~]# vim /etc/rsync.password
[[email protected] ~]# cat /etc/rsync.password
123456
? 【给密码文件设置权限】
[[email protected] ~]# chmod 600 /etc/rsync.password
[[email protected] ~]# ls -l /etc/rsync.password
-rw------- 1 root root 7 Mar 8 10:56 /etc/rsync.password
注意:密码文件的权限是600
【建立打包目录】
[[email protected] ~]# mkdir /backup/
? 【在客户端建立测试文件并测试】
[[email protected] ~]# cd /backup/
[[email protected] backup]# touch {01..10}
[[email protected] backup]# ls
stu01 stu02 stu03 stu04 stu05 stu06 stu07 stu08 stu09 stu10
[[email protected] backup]# rsync -avz /backup/ rsync[email protected]::backup/ --password-
file=/etc/rsync.password
2.2 在Web服务器上,实现打包、推送、删除、定时任务推送
? 【脚本】
#!/bin/sh
IP=$(ifconfig eth1|awk -F "[ :]+" ‘NR==2 {print$4}‘)
Path=/backup/$IP
if [ $(date +%w) -eq 0 ]
then
Time=week
$(date +%F-%w -d "-1day")
else
Time=$(date +%F -d "-1day")
fi
mkdir -p $Path
cd / &&\
tar zcf $Path/back$Time.tar.gz var/spool/cron/root etc/sysconfig/iptables etc/rc.local var/www/html/ /app/logs/ &&\
md5sum $Path/back
$Time.tar.gz >>$Path/flag_$Time.log &&\
rsync -az /backup/ [email protected]::backup/ --password-file=/etc/rsync.password &&\
find /backup/ -type f -mtime +7 ( -name ".tar.gz" -o -name ".log" )|xargs rm -f

2.3 在备份服务器上实现发送邮件并删除180前的数据,并发送邮件
【脚本】
#!/bin/sh
if [ $(date +%w) -eq 0 ]
then
Time="week_$(date +%F-%w -d "-1day")"
else
Time=$(date +%F -d "-1day")
fi
find /backup/ -type f -name "${Time}.log"|xargs md5sum -c >>/backup/${Time}_result.log
mail -s "$Time bak result " [email protected]</backup/${Time}_result.log
find /backup/ -type f -mtime +180 ! -name "_week-6" |xargs rm -f
2.3.1 Web服务器上的定时任务
[[email protected] ~]# crontab -l
#backup
00 00
/bin/sh /server/scripts/backup.sh >/dev/null 2>&1
2.3.2 备份服务器上的定时任务
[[email protected] ~]# crontab -l
#del
00 00 * /bin/sh /server/scripts/del.sh >/dev/null 2>&1
2.3.3 邮件发送的配置
编辑/etc/mail.rc文件然后在文件的最后一行加入下面的内容
set [email protected]
set smtp=pop.163.com
set smtp-auth-user=xxxxxxx
set smtp-auth-password=xxxxxxx
set smtp-auth=login
上面的各行的解释如下:
from: 当别人收到邮件时显示的发件人。
smtp: smtp服务器地址
smtp-auth: 指定SMTP的认证方式。默认是LOGIN方式
smtp-auth-user: 指定用户名
smtp-auth-password: 指定密码(163是授权码)

以上是关于全网备份案例的主要内容,如果未能解决你的问题,请参考以下文章

全网备份案例

全网备份案例

全网备份案例

项目-第六次全网备份上机考试

全网备份项目解决方案实例

公司实现全网备份项目:(rsync+定时任务)