数据备份在任何时候都要记住,这一点我一直没有意识。

有一次公司的服务器宕机了,官网无法访问,公司老大通过备份数据恢复保障官网的正常运行。

以下是阿里云服务器备份脚本,仅供参考:

#!/bin/bash
# gophper.sh
#----------------------------------
#实现服务器的备份
TIME=`date +'%Y%m%d'`
BAKDIR=/opt/backdata
UPLOADIR=/opt/uploadir
FTPDIR=/opt/var/www/html/ftp
#1.备份mysql gophper
/usr/bin/mysqldump -h127.0.0.1 -uroot -p111111 gophper >${BAKDIR}/gophper.sql
if [ "$?" != "0" ];then
        echo "mysqldump gophper failed"
        exit 1;
fi

#2.备份svn
SVNPATH=/opt/svn/resource
BAKSVNDIR=${BAKDIR}/${TIME}
if [ ! -f "${BAKSVNDIR}" ];then
    mkdir -p ${BAKSVNDIR}
fi
# 全备份
/usr/bin/svnadmin hotcopy ${SVNPATH} ${BAKSVNDIR}
# 如果能打印版本号说明备份正确
/usr/bin/svnlook youngest ${BAKSVNDIR}
if [ "$?" != "0" ];then
        echo "svn backup failed"
        exit 1;
fi
cd ${BAKDIR}
# 压缩备份
tar cvzf svn.tar.gz ${TIME}/
rm -rf ${BAKSVNDIR}
#3.备份etc 和 opt 目录除 uploadir
cd ${UPLOADIR}
tar czvf gophper_${TIME}.tar.gz /opt /etc --exclude=${UPLOADIR} --exclude=${FTPDIR}
#4.qrsync 上传
/opt/bin/qrsync-v2 /opt/bin/qrsync.json 
#删除成功,删除上传文件
rm ${UPLOADIR}/gophper_${TIME}.tar.gz
if [ "$?" != "0" ];then
    echo "rm ${UPLOADIR}/gophper_${TIME}.tar.gz failed"
    exit 1;
fi
echo "gophper backup success"