某公司里有一台Web服务器,里面的数据很重要,但是如果硬盘坏了,数据就会丢失,现在领导要求你把数据做备份,这样Web服务器数据丢失可以进行恢复。要求如下: 每天晚上00点整在Web服务器A上打包备份系统配置文件、网站程序目录及访问日志并通过rsync命令推送到服务器B上备份保留(备份思路可以是先在本地按日期打包,然后再推到备份服务器B上)。 问题一、实现把Web服务器A数据备份到服务器B。 具体要求如下: 1)Web服务器A和备份服务器B的备份目录必须都为/backup。 2)系统配置文件包括但不限于: a.定时任务服务的配置文件(/var/spool/cron/root)。 b.开机自启动的配置文件(/etc/rc.local) c.日常脚本的目录(/server/scripts) d.防火墙iptables的配置文件(/etc/sysconfig/iptables)。 e.自己思考下还有什么需要备份呢? 3)Web服务器站点目录假定为(/var/html/www)。 4)Web服务器A访问日志路径假定为(/app/logs) 5)Web服务器保留打包后的7天的备份数据即可(本地留存不能多于7天,因为太多硬盘会满),备份服务器B上要保留6个月的数据副本。 6)备份服务器B上要按照备份数据服务器的IP为目录保存,打包的文件按照时间名字保存。 特别提示:本题在工作中是网站生产环境全网备份项目方案的一个小型模拟,很有意义。 解答: 特别说明: 1)工作中领导很可能不会告诉你如何去做,只会提需求,例如:小崔,WEB服务器很重要,请你把数据在别的服务器备份一份(定期的备份)。 2)逻辑架构图更不可能是领导给你画,而是你理解了领导的意思,然后自己想出备份的方案,最后,在实施前你做的一个图纸而已。 |
Rsync-server服务端:
[[email protected] ~]# rpm -qa rsync 查看是否有安装包 rsync-3.0.6-12.el6.x86_64 [[email protected] ~]# touch /etc/rsyncd.conf 创建配置文件 [[email protected] ~]# vim /etc/rsyncd.conf 编辑配置文件 #created by yvonne 16:30 2015-6-24 ##rsyncd.conf start## uid = rsync 指定用户uid gid = rsync 指定用户组id use chroot = no 用户不能切入 max connections = 2000 指定最大连接时2000 timeout = 600 指定超时时间 pid file = /var/run/rsyncd.pid 指定pid文件路径 lock file = /var/run/rsync.lock 指定lock文件路径 log file = /var/log/rsyncd.log 指定日志文件路径 ignore errors 指定报错提示: read only = false 不允许只读 list = false 不允许列表查看 hosts allow = 192.168.10.0/24 允许的网段 hosts deny = 0.0.0.0/32 拒绝的网段/ip(此处不拒绝) auth users = rsync_backup 指定验证用户 secrets file = /etc/rsync.password 指定密码文件名 ##################################### [backup] comment = backup server by yvonne 16:30 2015-6-24 path = /backup 指定备份路径 "/etc/rsyncd.conf" 22L, 502C written [[email protected] ~]# [[email protected] ~]# mkdir /backup 创建备份文件/backup [[email protected] ~]# useradd rsync 创建rsync 用户 [[email protected] ~]# chown -R rsync /backup/ 更改/backup属主为rsync [[email protected] ~]# ll /backup/ total 0 [[email protected] ~]# ls -ld /backup/ drwxr-xr-x. 2 rsync root 4096 Jun 22 19:11 /backup/ [[email protected] ~]# echo "rsync_backup:oldboy">/etc/rsync.password 创建密码文件 [[email protected] ~]# cat /etc/rsync.password rsync_backup:oldboy [[email protected] ~]# chmod 600 /etc/rsync.password 更改密码文件权限 [[email protected] ~]# ll /etc/rsync.password -rw-------. 1 root root 20 Jun 22 19:12 /etc/rsync.password [[email protected] ~]# rsync –daemon 启动rsync 服务 [[email protected] ~]# vim /etc/rc.local #!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don‘t # want to do the full Sys V style init stuff. touch /var/lock/subsys/local /etc/init.d/iptables stop rsync –daemon 加入开机自启动 ~ ~ "/etc/rc.local" 9L, 261C written [[email protected] ~]# ll /backup/ 验证,备份成功 total 8 -rw-r--r--. 1 rsync rsync 4514 Jun 22 20:19 192.168.10.105_var_html_www_2015-06-22.tar.gz -rw-r--r--. 1 rsync rsync 0 Jun 22 19:15 test [[email protected] ~]# find /backup/ -type f -mtime +180 -name ".*tar" |xargs rm -rf 保留180天内 [[email protected] ~]# mkdir -p /server/scripts [[email protected] ~]# vim /server/scripts/reserve.sh 写成脚本 #print for reserve backup file in 6 mounth by yvonne 19:44 2015-6-24 find /backup/ -type f -mtime +180 -name ".*tar" |xargs rm -rf ~ ~ "/server/scripts/reserve.sh" [New] 4L, 135C written [[email protected] ~]# [[email protected] ~]# /bin/sh /server/scripts/reserve.sh 执行脚本 [[email protected] ~]# crontab –e 编辑定时任务 no crontab for root - using an empty one #print for reserve backup file in 6 mounth by yvonne 19:44 2015-6-24 00 00 1 */6 * /bin/sh /server/scripts/reserve.sh >/dev/null 2&>1 ~ "/tmp/crontab.8J1TLx" 3L, 136C written crontab: installing new crontab [[email protected] ~]# |
Rsync-client客户端
[[email protected] ~]# rpm -qa rsync 查看是否有安装包 rsync-3.0.6-12.el6.x86_64 [[email protected] ~]# echo "oldboy">/etc/rsync.password 创建密码文件 [[email protected] ~]# chmod 600 /etc/rsync.password 修改密码文件的权限为600 [[email protected] ~]# cat /etc/rsync.password oldboy [[email protected] ~]# ll /etc/rsync.password -rw-------. 1 root root 7 Jun 22 19:22 /etc/rsync.password [[email protected] ~]# mkdir /backup 创建备份目录 [[email protected] ~]# cd /backup/ [[email protected] backup]# touch test [[email protected] backup]# cd [[email protected] ~]# rsync /backup/* [email protected]::backup --password-file=/etc/rsync.password 推送测试 [[email protected] ~]# mkdir -p /var/html/www 创建站点目录 [[email protected] ~]# cd /var/html/www/ [[email protected] www]# ll total 0 [[email protected] www]# mkdir var_spool_cron 创建各个需要备份文件的压缩目录 [[email protected] www]# mkdir etc_rc.local [[email protected] www]# mkdir server_scripts [[email protected] www]# mkdir etc_sysconfig_iptables [[email protected] www]# mkdir -p /app/logs 创建访问日志路径 [[email protected] www]# ll total 16 drwxr-xr-x. 2 root root 4096 Jun 22 19:27 etc_rc.local drwxr-xr-x. 2 root root 4096 Jun 22 19:28 etc_sysconfig_iptables drwxr-xr-x. 2 root root 4096 Jun 22 19:27 server_scripts drwxr-xr-x. 2 root root 4096 Jun 22 19:27 var_spool_cron [[email protected] www]# cd /etc/ [[email protected] etc]# tar -zcvf /backup/`/sbin/ifconfig eth4|awk -F ‘[: ]+‘ ‘NR==2 {print $4}‘`_etc_rclocal_`date +%F`.tar.gz rc.local 打包rc.local rc.local 此处省略部分参考“打包rc.local”,先执行命令,然后写入脚本 [[email protected] etc]# mkdir -p /server/scripts [[email protected] ~]# vim /server/scripts/tar.sh 将需要打包的文件放入脚本 #print for tar important files to /var/html/www by yvonne 18:44 2015-6-24. cd /server/ &&\ tar -zcf /var/html/www/server_scripts/`/sbin/ifconfig eth4|awk -F ‘[: ]+‘ ‘NR==2 {print $4}‘`_server_scripts_`date +%F`.ta r.gz scripts
cd /etc/ &&\ tar -zcf /var/html/www/etc_rc.local/`/sbin/ifconfig eth4|awk -F ‘[: ]+‘ ‘NR==2 {print $4}‘`_var_spool_cron_`date +%F`.tar. gz rc.local
cd /etc/init.d/ &&\ tar -zcf /var/html/www/etc_init.d_iptables/`/sbin/ifconfig eth4|awk -F ‘[: ]+‘ ‘NR==2 {print $4}‘`_etc_iptables_`date +%F` .tar.gz iptables
cd /var/spool/ &&\ tar -zcf /var/html/www/var_spool_cron/`/sbin/ifconfig eth4|awk -F ‘[: ]+‘ ‘NR==2 {print $4}‘`_var_spool_cron_`date +%F`.ta r.gz cron ~ ~ ~ ~ ~ "/server/scripts/tar.sh" [New] 12L, 612C written [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# /bin/sh /server/scripts/tar.sh 执行脚本 [[email protected] ~]# ls /var/html/www/etc_rc.local/ 192.168.10.105_var_spool_cron_2015-06-22.tar.gz [[email protected] ~]# ls /var/html/www/etc_init.d_iptables/ 192.168.10.105_etc_iptables_2015-06-22.tar.gz [[email protected] ~]# ls /var/html/www/var_spool_cron/ 192.168.10.105_var_spool_cron_2015-06-22.tar.gz [[email protected] ~]# ls /var/html/www/server_scripts/ 192.168.10.105_server_scripts_2015-06-22.tar.gz [[email protected] www]# cd /var/html/ [[email protected] html]# tar -zcvf /backup/`/sbin/ifconfig eth4|awk -F ‘[: ]+‘ ‘NR==2 {print $4}‘`_var_html_www_`date +%F`.tar.gz www/ www/ www/var_spool_cron/ www/var_spool_cron/192.168.10.105_var_spool_cron_2015-06-22.tar.gz www/etc_init.d_iptables/ www/etc_init.d_iptables/192.168.10.105_etc_init.d_iptables_2015-06-22.tar.gz www/etc_init.d_iptables/192.168.10.105_etc_iptables_2015-06-22.tar.gz www/server_scripts/ www/server_scripts/192.168.10.105_server_scripts_2015-06-22.tar.gz www/etc_rc.local/ www/etc_rc.local/192.168.10.105_var_spool_cron_2015-06-22.tar.gz [[email protected] html]# tar -zcf /backup/`/sbin/ifconfig eth4|awk -F ‘[: ]+‘ ‘NR==2 {print $4}‘`_var_html_www_`date +%F`.tar.gz www/ [[email protected] html]# vim /server/scripts/tar.sh cd /server/ &&\ tar -zcf /var/html/www/server_scripts/`/sbin/ifconfig eth4|awk -F ‘[: ]+‘ ‘NR==2 {print $4}‘`_server_scripts_`date +%F`.ta r.gz scripts cd /etc/ &&\ tar -zcf /var/html/www/etc_rc.local/`/sbin/ifconfig eth4|awk -F ‘[: ]+‘ ‘NR==2 {print $4}‘`_var_spool_cron_`date +%F`.tar. gz rc.local cd /etc/init.d/ &&\ tar -zcf /var/html/www/etc_init.d_iptables/`/sbin/ifconfig eth4|awk -F ‘[: ]+‘ ‘NR==2 {print $4}‘`_etc_init.d_iptables_`da te +%F`.tar.gz iptables cd /var/spool/ &&\ tar -zcf /var/html/www/var_spool_cron/`/sbin/ifconfig eth4|awk -F ‘[: ]+‘ ‘NR==2 {print $4}‘`_var_spool_cron_`date +%F`.ta r.gz cron cd /var/html/ &&\ tar -zcf /backup/`/sbin/ifconfig eth4|awk -F ‘[: ]+‘ ‘NR==2 {print $4}‘`_var_html_www_`date +%F`.tar.gz www/ ~ ~ "/server/scripts/tar.sh" 15L, 748C written [[email protected] html]# [[email protected] html]# /bin/sh /server/scripts/tar.sh [[email protected] html]# ll /backup/ total 8 -rw-r--r--. 1 root root 4514 Jun 22 20:23 192.168.10.105_var_html_www_2015-06-22.tar.gz [[email protected] html]# rsync /backup/* [email protected]::backup --password-file=/etc/rsync.password 将/backup推送到备份服务器 [[email protected] html]# [[email protected] html]# vim /server/scripts/backup.sh 写成脚本 #print for backup to backup-server by yvonne 19:29 2015-6-24. rsync /backup/* [email protected]::backup --password-file=/etc/rsync.password ~ ~ "/server/scripts/backup.sh" [New] 3L, 152C written [[email protected] html]# /bin/sh /server/scripts/backup.sh 执行脚本 [[email protected] html]# /bin/sh /server/scripts/tar.sh [[email protected] html]# crontab –e 编辑定时任务 no crontab for root - using an empty one #print for backup important files by yvonne 19:28 2015-6-24. 00 22 * * * /bin/sh /server/scripts/tar.sh >/dev/null 2>&1 00 01 * * * /bin/sh /server/scripts/backup.sh >/dev/null 2>&1 "/tmp/crontab.Fe9LeT" 3L, 182C written [[email protected] ~]# crontab -l #print for backup important files by yvonne 19:28 2015-6-24. 00 22 * * * /bin/sh /server/scripts/tar.sh >/dev/null 2>&1 00 01 * * * /bin/sh /server/scripts/backup.sh >/dev/null 2>&1 [[email protected] ~]# mkdir -p /app/logs [[email protected] ~]# crontab -e
#print for backup important files by yvonne 19:28 2015-6-24. 00 22 * * * /bin/sh /server/scripts/tar.sh >/app/logs 2>&1 00 01 * * * /bin/sh /server/scripts/backup.sh >/app/logs 2>&1 ~ "/tmp/crontab.f8JwpT" 3L, 182C written [[email protected] ~]# [[email protected] ~]# find /backup/ -type f -mtime +7 -name ".*tar" |xargs rm -rf 保留7天内的 [[email protected] ~]# vim /server/scripts/reserve.sh #print for reserve backup file in 7 days by yvonne 19:44 2015-6-24. find /backup/ -type f -mtime +7 -name ".*tar" |xargs rm -rf ~ ~ "/server/scripts/reserve.sh" [New] 2L, 129C written [[email protected] ~]# [[email protected] ~]# /bin/sh /server/scripts/reserve.sh 执行脚本 [[email protected] ~]# crontab –e 写入定时任务 #print for backup important files by yvonne 19:28 2015-6-24. 00 22 * * * /bin/sh /server/scripts/tar.sh >/app/logs 2>&1 00 01 * * * /bin/sh /server/scripts/backup.sh >/app/logs 2>&1 #print for reserve backup file in 7 days by yvonne 19:44 2015-6-24. 00 10 * * 0 /bin/sh /server/scripts/reserve.sh >/app/logs 2>&1 ~ ~ "/tmp/crontab.kZUxZs" 6L, 314C written crontab: installing new crontab [[email protected] ~]# |