某公司里有一台web服务器,里面的数据很重要,但是始果硬盘坏了,数据就会丢失,现在领导要求你把数据在其他机器上做一个周期性定时备份,要求如下:
每天晚上00点整在web服务器A上打包备份网站程序目录并通过rsync命令推送到服务器B上备份保留(备份思路可以是先在本地按日期打包,然后再推送到备份服务器上)
具体要求如下:
1) web服务器A和备份服务器B的备份目录必须都为/backup
2) web服务器站点目录假定为(/var/www/html)
解答:
操作系统
主机名 | 网卡eth0 | 默认网关 | 用途 |
[email protected] | 192.168.1.111 | 192.168.1.1 | Rsync 节点 |
[email protected] | 192.168.1.121 | 192.168.1.1 | Rsync服务端 |
子网掩码均为255.255.255.0
配置rsync服务端B-Server备份服务器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
[root@B-Server ~]# rpm -aq rsync rsync-3.0.6-4.el5_7.1 [root@B-Server ~]# vi /etc/rsyncd.conf [root@B-Server ~]# cat /etc/rsyncd.conf #Rsync server #created by oldboy 15:01 2009-6-5 ##rsyncd.conf start## uid = root gid = root use chroot = no max connections = 2000 timeout = 600 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log ignore errors read only = false list = false hosts allow = 192.168.1.1/24 hosts deny = 0.0.0.0/32 auth users = rsync_backup secrets file = /etc/rsync.password [backup] comment = www by Mr.Xiong 14:18 2012-1-13 path = /backup/ [root@B-Server ~]# dos2unix /etc/rsyncd.conf dos2unix: converting file /etc/rsyncd.conf to UNIX format ... [root@B-Server ~]# mkdir /backup [root@B-Server ~]# echo "rsync_backup:dingjian">/etc/rsync.password [root@B-Server ~]# chmod 600 /etc/rsync.password [root@B-Server ~]# cat /etc/rsync.password rsync_backup:dingjian [root@B-Server ~]# ll /etc/rsync.password -rw------- 1 root root 22 Apr 23 17:42 /etc/rsync.password |
启动rsync服务
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[root@B-Server ~]# rsync --daemon [root@B-Server ~]# lsof -i tcp:873 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rsync 3173 root 3u IPv4 11363 0t0 TCP *:rsync (LISTEN) [root@B-Server ~]# netstat -lntup|grep 873 tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 3173/rsync tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2873/sendmail |
配置Rsync客户端 A-Server备份服务器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
[root@A-server scripts]# vi /etc/rsync.password [root@A-server scripts]# chmod 600 /etc/rsync.password [root@A-server scripts]# mkdir /var/www/html –p [root@A-server ~]# mkdir /backup/192.168.1.111 -p [root@A-server ~]# mkdir /server/scripts -p [root@A-server ~]# cd /server/scripts [root@A-server scripts]# vi back.sh [root@A-server scripts]# cat back.sh cd /var/www &&\ tar zcf /backup/192.168.1.111/html_$(date +%F).tar.gz ./html &&\ cd /backup/ &&\ rsync -az . rsync_backup@192.168.1.119::bacup --password-file=/etc/rsync.password >&/dev.null find /backup -type f -name "*.gz" -mtime +7|xargs rm –f [root@58server1 scripts]# sh -x back.sh |
把脚本写到定时任务,使其每天晚上00点备份
1 2 3 4 5 |
[root@A-server scripts]# crontab -e #################### 00 00 * * * /bin/sh /server/scripts/back.sh >&/dev/null |