Linux下web服务器备份恢复脚本

inux下的Web服务器备份恢复脚本,经测试centos和freebsd下都可以用只不过要修改脚本当中的变量TAR为系统的tar命令路径,脚本如下


?
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/sh  
cat << EOF  
     +----------------------------------------------------------------+  
     |              === 服务器备份、恢复脚本 ===                  |  
     |------------------http: //www.xmo.cc-------------------|  
     +-----------------------clairelume-----------------------------+  
EOF  
WEB_DIR=/data/www  
BACK_DIR=/data/www/webback  
TIME=` date +%Y%m%d`  
TAR= "/usr/bin/tar" 
TAR_BACK= "czf" 
TAR_RECOVERY= "xvzf" 
COLOR_CYAN= "\033[36;49;1m" 
COLOR_GREEN= "\033[32;49;1m" 
COLOR_RED= "\033[31;49;1m" 
COLOR_BACK= "\033[39;49;0m" 
echo -e "$COLOR_CYAN备份环境信息:$COLOR_BACK" 
echo "+--------------------------------------------------------------+" 
echo -e "$COLOR_CYAN 1. 程序目录:$WEB_DIR$COLOR_BACK" 
echo -e "$COLOR_CYAN 2. 备份目录:$BACK_DIR$COLOR_BACK" 
echo -e "$COLOR_CYAN 3. 备份时间:$TIME$COLOR_BACK" 
echo -e "$COLOR_CYAN 4. 备份文件格式:web_back_`date +%Y%m%d`.tar.gz$COLOR_BACK" 
echo -e "$COLOR_RED 注:shell中的目录的参数,修改为您服务器的对应目录$COLOR_BACK" 
echo "+--------------------------------------------------------------+" 
if [ ! -d $BACK_DIR ];then 
     mkdir -p $BACK_DIR 
fi  
while true; do 
     echo -e "$COLOR_CYAN请您选择操作(1=备份,2=恢复,3|q|Q=退出,*=重新选择)[1,2,3]:$COLOR_BACK" 
     read a  
     case "$a" in 
         1)  
             echo -e "$COLOR_CYAN当前服务器的站点程序:$COLOR_BACK" 
             echo "+------------------------------------------------+" 
             ls -l $WEB_DIR |grep '^d' |awk '{print $NF}'  
             echo "+------------------------------------------------+" 
             echo -e "$COLOR_CYAN选择需要备份的站点程序:$COLOR_BACK" 
             read b  
             if [ -d $WEB_DIR / $b ];then 
                 cd $WEB_DIR 
                 $TAR $TAR_BACK $BACK_DIR / "$b" _back_` date +%Y%m%d`.tar.gz $b / > /dev/null 2>&1  
                 echo -e "$COLOR_GREEN$TIME $b备份成功!请继续选择其他操作$COLOR_BACK" 
                 echo "$TIME $b备份成功!$TIME" >> / var /log/web_back.log  
             else   
                 echo -e "$COLOR_RED需要备份站点不存在,请重新输入:$COLOR_BACK" 
                 read d  
                 if [ -d $WEB_DIR / $d ];then 
                     cd $WEB_DIR 
                     $TAR $TAR_BACK $BACK_DIR / "$d" _back_` date +%Y%m%d`.tar.gz $d / > /dev/null 2>&1  
                     echo -e "$COLOR_GREEN$TIME $d备份成功!请继续选择其他操作$COLOR_BACK" 
                     echo "$TIME $d备份成功!$TIME" >> / var /log/web_back.log  
                 else   
                     echo -e "$COLOR_RED备份站点名输入出错两次,退出当前备份选择!$COLOR_BACK" 
                     echo "$TIME 备份站点名输入出错两次,退出当前备份!" >> / var /log/web_back.log             
                 fi  
             fi  
             continue  
             ;;  
         2)  
             echo -e "$COLOR_CYAN当前服务器的备份文件:$COLOR_BACK" 
             echo "+------------------------------------------------+" 
             ls -l $BACK_DIR |grep "tar.gz" |awk '{print $NF}'  
             echo "+------------------------------------------------+" 
             echo -e "$COLOR_CYAN选择需要恢复的恢复文件:$COLOR_BACK" 
             read c  
             if [ -f $BACK_DIR / $c ];then 
                 cd $WEB_DIR 
                 $TAR $TAR_RECOVERY $BACK_DIR / $c -C $WEB_DIR / > /dev/null 2>&1  
                 echo -e "$COLOR_GREEN$TIME $c恢复成功,请继续选择其他操作!$COLOR_BACK" 
                 echo "$TIME $c恢复成功!" >> / var /log/web_back.log  
             else 
                 echo -e "$COLOR_RED需要恢复站点不存在,请重新输入:$COLOR_BACK" 
                 read f  
                 if [ -f $BACK_DIR / $f ];then 
                     cd $WEB_DIR 
                     $TAR $TAR_RECOVERY $BACK_DIR / $f -C $WEB_DIR / > /dev/null 2>&1  
                     echo -e "$COLOR_GREEN$TIME $f恢复成功,请继续选择其他操作!$COLOR_BACK" 
                     echo "$TIME $f恢复成功!" >> / var /log/web_back.log  
                 else 
                     echo -e "$COLOR_RED恢复文件名输入出错两次,退出当前恢复选择!$COLOR_BACK" 
                     echo "$TIME 恢复文件名输入出错两次,退出当前恢复选择!" >> / var /log/web_back.log  
                 fi  
             fi  
             continue  
             ;;  
         3|Q|q)  
             echo -e "$COLOR_GREEN退出成功!$COLOR_BACK" 
             exit  
             ;;  
         *)  
             echo -e "$COLOR_RED输入错误,请重新输入!$COLOR_BACK" 
             continue  
             ;;  
     esac  
     exit 0  
done


转载请注明来源: Linux下web服务器备份恢复脚本
http://www.php1.cn/Content/Linux_Xia_web_FuWuQiBeiFenHuiFuJiaoBen.html

你可能感兴趣的:(运维)