1、备份规则:
在生产环境中有若干服务器需要定时将服务器中应用程序,以及数据库等进行备份。要求在本地服务器中保存近一周的备份,备份服务器中保存最近一月的备份文件。
以服务器A为例:在服务器A上有应用程序业务及(/ucap/UServer_V5.5.1_5002),有数据库业务(mongodb和mysql)需要将以上三个业务进行备份,且在A服务器上创建backup-dir将备份文件存放在目录下,每日凌晨进行备份,并将备份文件上传至备份服务器(NFS),在平时备份服务器与服务器A不会有联系,即只是在上传备份文件,服务器A自动挂载备份文件共享目录,备份完成卸载目录。本地服务器保存近一周文件,备份服务器保存近30日备份文件。
2、具体实现如下步骤:
1 #delet backup when is sunday 周日删除上周文件。
2 WEEK=$(date +%w)
3 BACKUP_ROOT=/backup
4 BACKUP_DIR=/backup/backup_full_`date +%F`
5 if [ -e $BACKUP_ROOT ]
6 then
7 cd $BACKUP_ROOT #如果/backup目录存在,进入目录。
8 if [ $WEEK -eq 0 ]
9 then
10 rm -rf $BACKUP_ROOT/* #如果进入是星期天(0表示星期天)执行删除/backup下所有的备份文件。
11 fi
12 if [ $? -eq 0 ] #判断是否删除成功
13 then
14 action "old-backup is remove." /bin/true
15 fi
16
17 fi
18 ####mkdir backup-dir #创建本地备份目录。如果目录存在则输出目录已经存
19 #在,否则表示不存在目录则创建包含时间的目录文件目录
20 if [ -e $BACKUP_DIR ]
21 then
22 action "$BACKUP_DIR had been created." /bin/true
23 else
24 #含当前时间的目录文件实际可以用$BACKUP_DIR
25 mkdir -p /backup/backup_full_`date +%F`
26 action "$BACKUP_DIR is creating." /bin/true
27 fi
本地创建备份目录及周日删除
注意:周日删除必须在创建当天目录之前。因为一旦先创建目录在删除文件由于使用的是rm -rf $BACKUP_ROOT/*意思是将本地备份目录下的所有文件删除,所有新创建的目录也会被删除。因此应该执行先删除再创建。
1 ###tar backup /ucap/UServer_V5.5.1_5002
2 CMSP=/ucap
3 cd $CMSP
4 FILE_UCM=UServer_V5.5.1_5002
5 #使用tar -g快照功能进行备份将快照snapshot.cms也存放在备份目录下,然后将备份文件按照ip地址以及日期进行打包存放在备份目录下的当日文件目录下。
6 tar -g $BACKUP_ROOT/snapshot.cms -cvf $BACKUP_DIR/${FILE_UCM}_ip_1.25_backup.$(date +%F).tar $FILE_UCM >/dev/null 2>&1
7
8 if [ $? = 0 ] #判断是否执行成功。
9 then
10 action "$FILE_UCM is backup." /bin/true
11 else
12 action "$FILE_UCM is no been backuped." /bin/true
13 fi
14 ###tar backup /ucap/mongodb
15 CMSP=/ucap
16 cd $CMSP
17 FILE_UCM=mongodb
18 #备份规则同上。
19 tar -g $BACKUP_ROOT/snapshot.cms -cvf $BACKUP_DIR/${FILE_UCM}_ip_1.25_backup.$(date +%F).tar $FILE_UCM >/dev/null 2>&1
20
21 if [ $? = 0 ]
22 then
23 action "$FILE_UCM is backup." /bin/true
24 else
25 action "$FILE_UCM is no been backuped." /bin/true
26 fi
备份文件及mongodb
1 #parameter defined
2 BAKDATE=`date +%F`
3 MYSQL_PATH=/usr/local/mysql/bin
4 MYUSER=root
5 MYPASS="Ucap7890&*()"
6 #DBNAME=`mysql -uroot -p'Ucap7890&*()' -e "show databases;"|grep cpmdb`
7 DATA_FILE=${BACKUP_DIR}/mysql_backup_${BAKDATE}.sql.gz
8 [ ! -d ${BACKUP_DIR} ] && mkdir -p ${BACKUP_DIR}
9 #command defined
10 #定义mysqldump命令备份cpmdb库并进行行锁
11 MYSQL_DUMP="${MYSQL_PATH}/mysqldump -u$MYUSER -p$MYPASS -S /tmp/mysql.sock -F -B cpmdb --single-transaction -e"
12 #backup command#执行命令并用GZI打包至备份目录当前日期下的文件中。
13 ${MYSQL_DUMP} | gzip > $DATA_FILE
Mysql备份
1 #####copye backup_tar to backup_server
2 BACKUP_SERVER=23.202.1.23:/backup/Search #NFS备份服务器
3 LOCAL_DIR=/backup1 #本地挂载点
4 RPC_PID=`/etc/init.d/rpcbind status|wc -l` #判断RPC
5 MOUNT_STATUS=`df -h | grep backup1 | wc -l` #判断是否已经挂载
6 BACKUP_FILEDIR=backup_full_`date +%F`
7 SERVER_IP=`ifconfig| grep Bcast|awk '{print $2}'` #本机IP
8 if [ ! -e ${LOCAL_DIR} ] #判断挂载点目录是否存在
9 then
10 mkdir ${LOCAL_DIR}
11 fi
12
13 if [ ${RPC_PID} -eq 0 ] #判断RPC-BIND是否启动没有启动执行启动
14 then
15 /etc/init.d/rpcbind start
16 fi
17 if [ $MOUNT_STATUS -eq 0 ] #判断NFS是否挂载没有挂载执行挂载
18 then
19 mount -t nfs ${BACKUP_SERVER} ${LOCAL_DIR}
20 cd ${LOCAL_DIR} #进入挂载目录
21 find ${LOCAL_DIR} -mtime +30 -exec rm{} \; #删除30天以前文件
22 if [ `find ${LOCAL_DIR} -mtime +30 -exec ls{} \;|wc -l` -eq 0 ] #如果没有了30天前的文件将本地备份目录下的备份文件备份至备份服务器。
23 then
24 cd $BACKUP_ROOT
25 cp -r ${BACKUP_FILEDIR} ${LOCAL_DIR}/
26 fi
27 if [ $? -eq 0 ] #判断执行是否成功,成功后卸载挂载目录。
28 then
29 umount -l ${LOCAL_DIR}
30 fi
31 else #表示已经挂载了NFS备份服务器执行删除备份卸载。
32 cd ${LOCAL_DIR}
33 find ${LOCAL_DIR} -mtime +30 -exec rm{} \;
34 cd $BACKUP_ROOT
35 cp -r ${BACKUP_FILEDIR} ${LOCAL_DIR}/
36 umount -l ${LOCAL_DIR}
37 exit 1
38 fi
本地备份存放至本分服务器并删除30日前的备份文件
3、整个脚本,由于本分脚本中含有数据库密码所有在最好做判断只有root用户才能执行脚本。
1 #!/bin/bash
2 ##########################################
3 # this script is created by xuxuedong. #
4 # e_mail:365***746@qq.com #
5 # qqinfo:365***746 #
6 # This is backup the fiel for the server. #
7 # version:1.1 #
8 ##########################################
9 . /etc/init.d/functions
10 #set env
11 export PATH=$PATH:/bin:/sbin:/usr/sbin
12 export LANG="zh_CN.GB18030"
13
14 # Require root to run this script. 判断是否是root用户执行。
15 if [[ "$(whoami)" != "root" ]]; then
16 echo "Please run this script as root." >&2
17 exit 1
18 fi
19
20 # Source function library.
21 #. /etc/init.d/functions
22 WEEK=$(date +%w)
23 BACKUP_ROOT=/backup
24 BACKUP_DIR=/backup/backup_full_`date +%F`
25 if [ -e $BACKUP_ROOT ]
26 then
27 cd $BACKUP_ROOT
28 if [ $WEEK -eq 0 ]
29 then
30 rm -rf $BACKUP_ROOT/*
31 fi
32 if [ $? -eq 0 ]
33 then
34 action "old-backup is remove." /bin/true
35 fi
36 else
37 echo "Time is not, cannot be deleted." /bin/false
38
39 fi
40 ####mkdir backup-dir
41 if [ -e $BACKUP_DIR ]
42 then
43 action "$BACKUP_DIR had been created." /bin/true
44 else
45 mkdir -p /backup/backup_full_`date +%F`
46 action "$BACKUP_DIR is creating." /bin/true
47 fi
48 ###tar backup /ucap/UServer_V5.5.1_5002
49 CMSP=/ucap
50 cd $CMSP
51 FILE_UCM=UServer_V5.5.1_5002
52 tar -g $BACKUP_ROOT/snapshot.cms -cvf $BACKUP_DIR/${FILE_UCM}_ip_1.25_backup.$(date +%F).tar $FILE_UCM >/dev/null 2>&1
53
54 if [ $? = 0 ]
55 then
56 action "$FILE_UCM is backup." /bin/true
57 else
58 action "$FILE_UCM is no been backuped." /bin/true
59 fi
60 ###tar backup /ucap/mongodb
61 CMSP=/ucap
62 cd $CMSP
63 FILE_UCM=mongodb
64 tar -g $BACKUP_ROOT/snapshot.cms -cvf $BACKUP_DIR/${FILE_UCM}_ip_1.25_backup.$(date +%F).tar $FILE_UCM >/dev/null 2>&1
65
66 if [ $? = 0 ]
67 then
68 action "$FILE_UCM is backup." /bin/true
69 else
70 action "$FILE_UCM is no been backuped." /bin/true
71 fi
72 #parameter defined
73 BAKDATE=`date +%F`
74 MYSQL_PATH=/usr/local/mysql/bin
75 MYUSER=root
76 MYPASS="Ucap7890&*()"
77 #DBNAME=`mysql -uroot -p'Ucap7890&*()' -e "show databases;"|grep cpmdb`
78 DATA_FILE=${BACKUP_DIR}/mysql_backup_${BAKDATE}.sql.gz
79 [ ! -d ${BACKUP_DIR} ] && mkdir -p ${BACKUP_DIR}
80 #command defined
81 MYSQL_DUMP="${MYSQL_PATH}/mysqldump -u$MYUSER -p$MYPASS -S /tmp/mysql.sock -F -B cpmdb --single-transaction -e"
82 #backup command
83 ${MYSQL_DUMP} | gzip > $DATA_FILE
84
85 #####copye backup_tar to backup_server
86 BACKUP_SERVER=23.202.1.23:/backup/Search
87 LOCAL_DIR=/backup1
88 RPC_PID=`/etc/init.d/rpcbind status|wc -l`
89 MOUNT_STATUS=`df -h | grep backup1 | wc -l`
90 BACKUP_FILEDIR=backup_full_`date +%F`
91 SERVER_IP=`ifconfig| grep Bcast|awk '{print $2}'`
92 if [ ! -e ${LOCAL_DIR} ]
93 then
94 mkdir ${LOCAL_DIR}
95 fi
96
97 if [ ${RPC_PID} -eq 0 ]
98 then
99 /etc/init.d/rpcbind start
100 fi
101 if [ $MOUNT_STATUS -eq 0 ]
102 then
103 mount -t nfs ${BACKUP_SERVER} ${LOCAL_DIR}
104 cd ${LOCAL_DIR}
105 find ${LOCAL_DIR} -mtime +30 -exec rm{} \;
106 if [ `find ${LOCAL_DIR} -mtime +30 -exec ls{} \;|wc -l` -eq 0 ]
107 then
108 cd $BACKUP_ROOT
109 cp -r ${BACKUP_FILEDIR} ${LOCAL_DIR}/
110 fi
111 if [ $? -eq 0 ]
112 then
113 umount -l ${LOCAL_DIR}
114 fi
115 else
116 cd ${LOCAL_DIR}
117 find ${LOCAL_DIR} -mtime +30 -exec rm{} \;
118 cd $BACKUP_ROOT
119 cp -r ${BACKUP_FILEDIR} ${LOCAL_DIR}/
120 umount -l ${LOCAL_DIR}
121 exit 1
122 fi