我的定时备份网站脚本

发布日期:

dropbox upload脚本:

curl "https://raw.githubusercontent.com/andreafabrizi/Dropbox-Uploader/master/dropbox_uploader.sh" -o dropbox_uploader.sh
chmod +x dropbox_uploader.sh
./dropbox_uploader.sh

创建dropbox token:

https://www.dropbox.com/developers/apps

#!/bin/bash
 
# MYSQL数据库帐号密码
MYSQL_USR="root"  # 数据库帐号
MYSQL_PWD="passwd"  # 数据库密码
 
# 定义需要备份的目录
Apache_CONF_DIR=/etc/apache2  # Apache配置目录
WEB_DIR=/var/www/zrdn  # 网站数据存放目录

# 定义备份存放目录
DROPBOX_DIR=/$(date +%Y-%m-%d)  # Dropbox上的备份目录
LOCAL_BAK_DIR=/root/backup  # 本地备份文件存放目录
 
# 定义备份文件名称
DBBakName=DB_$(date +%Y%m%d).tar.gz
ApacheConfBakName=ApacheConf_$(date +%Y%m%d).tar.gz
WebBakName=Web_$(date +%Y%m%d).tar.gz

# 定义旧数据名称
Old_DROPBOX_DIR=/$(date -d -1day +%Y-%m-%d)
OldDBBakName=DB_$(date -d -30day +%Y%m%d).tar.gz
OldApacheConfBakName=ApacheConf_$(date -d -30day +%Y%m%d).tar.gz
OldWebBakName=Web_$(date -d -3day +%Y%m%d).tar.gz
 
cd $LOCAL_BAK_DIR
 
#使用命令导出SQL数据库,并且按数据库分个压缩
for db in `mysql -u$MYSQL_USR -p$MYSQL_PWD -B -N -e 'SHOW DATABASES' | xargs`; do
	(mysqldump -u$MYSQL_USR -p$MYSQL_PWD --single-transaction ${db} | gzip -9 - > ${db}.sql.gz)
done

#压缩数据库文件合并为一个压缩文件
tar zcf $LOCAL_BAK_DIR/$DBBakName $LOCAL_BAK_DIR/*.sql.gz
rm -rf $LOCAL_BAK_DIR/*.sql.gz
 
#压缩Apache配置数据
cd $Apache_CONF_DIR
tar zcf $LOCAL_BAK_DIR/$ApacheConfBakName ./*
 
#压缩网站数据
cd $WEB_DIR
tar zcf $LOCAL_BAK_DIR/$WebBakName ./*
 
cd /root
#开始上传
./dropbox_uploader.sh upload $LOCAL_BAK_DIR/$DBBakName $DROPBOX_DIR/$DBBakName
./dropbox_uploader.sh upload $LOCAL_BAK_DIR/$ApacheConfBakName $DROPBOX_DIR/$ApacheConfBakName
./dropbox_uploader.sh upload $LOCAL_BAK_DIR/$WebBakName $DROPBOX_DIR/$WebBakName
 
#删除旧数据
rm -rf $LOCAL_BAK_DIR/$OldDBBakName $LOCAL_BAK_DIR/$OldApacheConfBakName $LOCAL_BAK_DIR/$OldWebBakName
./dropbox_uploader.sh delete $Old_DROPBOX_DIR
 
echo -e "Backup Done!"