念念不忘
必有回响🎉

自动备份网站到DropBox

尽管开通了Vultr的backup服务,但是依旧有种“总有刁民想害朕”的想法,于是为了双保险将网站定时备份到网盘,国内服务器的可选阿里云OSS、七牛等,国外的话还是感觉DropBox靠谱点,我自己是DropBox的忠实用户,于是就备份到DropBox吧。

1.将网站与数据库打包

要备份到DropBox,首先需要将网站源码、数据库等都打包。打包脚本来自张戈博客,仅对其加了一个-h参数,用来指定数据库的IP地址。在/data/script文件夹中新建backup.sh,将如下填入其中:

#!/bin/bash
#Author:ZhangGe
#Des:Backup database and webfile.
#Date:2014-8-28
TODAY=$(date +"%Y-%m-%d")

if [ -z $1 ];then
        echo Needed Usage arguments. Please Use --help to get more infomation.
        exit 1
fi

if [ "$1" == "db" ];then
        domain=$2
        dbname=$3
        mysqluser=$4
        mysqlpd=$5
        back_path=$6
        mysql_host=$7
        cd $back_path
        mysqldump -h$mysql_host -u$mysqluser -p$mysqlpd $dbname>$back_path/$domain\_db_$TODAY\.sql
        tar cvzf $back_path/$domain\_db_$TODAY\.tar.gz $domain\_db_$TODAY\.sql --remove-files

elif [ "$1" == "file" ];then
        domain=$2
        site_path=$3
        back_path=$4
        tar cvzf $back_path/$domain\_file_$TODAY\.tar.gz $site_path

elif [ "$1" == "--help" ];then
        clear
        echo =====================================Help infomation=========================================
        echo 1. Use For Backup database:
        echo The \$1 must be \[db\]
        echo \$2: \[domain\]
        echo \$3: \[dbname\]
        echo \$4: \[mysqluser\]
        echo \$5: \[mysqlpassword\]
        echo \$6: \[back_path\]
        echo \$7: \[mysql_hosst\]
        echo
        echo For example:./backup.sh db zhangge.net zhangge_db zhangge 123456 /home/wwwbackup/zhangge.net
        echo
        echo 2. Use For Backup webfile:
        echo The \$1 must be [\file\]:                                                                                                                                                     
        echo \$2: \[domain\]                                                                                                                                                               
        echo \$3: \[site_path\]                                                                                                                                                            
        echo \$4: \[back_path\]                                                                                                                                                            
        echo                                                                                                                                                                               
        echo For example:./backup.sh file zhangge.net /home/wwwroot/zhangge.net /home/wwwbackup/zhangge.net                                                                                
        echo =====================================End of Hlep==============================================                                                                                
        exit 0                                                                                                                                                                             
else                                                                                                                                                                                       
        echo "Error!Please Usage --help to get help infomation!"                                                                                                                           
        exit 2                                                                                                                                                                             
fi

测试脚本:

bash backup.sh file blog.liyang.pro /var/www/blog.liyang.pro /data/backup/
bash backup.sh db blog.liyang.pro dbname dbuser dbpassword /data/backup/ 10.99.0.2

然后在/data/backup中查看文件

ls /data/backup/
blog.liyang.pro_db_2018-10-14.tar.gz  blog.liyang.pro_file_2018-10-14.tar.gz

2.创建DropBox App

登录DropBox后,点击https://www.dropbox.com/developers/apps,点击Create App。

点击Create App,此时App就已经创建成功,并且接入Setting菜单。找到Oauth 2,点击Generate,生成Token。

记下这个Token。

3.配置DropBox脚本

下载DropBox脚本:

cd /data/script/
wget https://raw.github.com/andreafabrizi/Dropbox-Uploader/master/dropbox_uploader.sh 
chmod +x dropbox_uploader.sh 
sh dropbox_uploader.sh

执行脚本,输入刚才的Token

bash dropbox_uploader.sh

新增上传备份文件到DropBox的脚本

vim /data/script/auto_upload_dropbox.sh

填入如下内容:

#!/bin/bash 
#
# Dropbox App 应用根目下自动创建日期文件夹
DROPBOX_DIR=/$(date +%Y-%m-%d) 
# 网站备份文件目录
BACKUP_DIR=/data/backup 
# 定义要上传的文件名
CURRENT_FILE=www.liyang.pro_file_$(date +%Y-%m-%d).tar.gz
CURRENT_DB=www.liyang.pro_db_$(date +%Y-%m-%d).tar.gz
# 定义要删除3天以上文件名
THREE_DAY_FILE=www.liyang.pro_file_$(date -d -3day +"%Y-%m-%d").tar.gz
THREE_DAY_DB=www.liyang.pro_db_$(date -d -3day +"%Y-%m-%d").tar.gz
# 配置DROPBOX中要删除的目录(默认清理Dropbox中30天的备份) 
DEL_DROPBOX_DIR=/$(date -d -30day +%Y-%m-%d)
# 清理本地过时的备份(默认清理3天,也可以用find命令清理) 
echo -ne "Delete local data of 3 days old..." 
rm -rf $BACKUP_DIR/$THREE_DAY_FILE $BACKUP_DIR/$THREE_DAY_DB
echo -e "Done" 
# 开始上传到 Dropbox 
echo -e "Start uploading..." 
bash /data/script/dropbox_uploader.sh upload  $BACKUP_DIR/$CURRENT_FILE $DROPBOX_DIR/
bash /data/script/dropbox_uploader.sh upload  $BACKUP_DIR/$CURRENT_DB $DROPBOX_DIR/
# 清理 Dropbox 里 30 天前的旧数据 
RESULT=`bash /data/script/dropbox_uploader.sh search $DEL_DROPBOX_DIR`
HIT="[F]"
IDX=`expr index "${RESULT}" ${HIT}`
if [ $IDX -eq 0 ];then
  echo "Dropbox $DEL_DROPBOX_DIR is not found."
else
  bash /data/script/dropbox_uploader.sh delete $DEL_DROPBOX_DIR
  echo "DELETE Dropbox $DEL_DROPBOX_DIR DONE ."
fi
echo -e "All done." 

4.配置Crontab

crontab -e

新增如下内容:

00 01 * * * bash /data/script/backup.sh db blog.liyang.pro dbname dbuser dbpassword /data/backup/ 10.99.0.2 >/dev/null 2>&1
30 01 * * * bash /data/script/backup.sh file blog.liyang.pro /var/www/blog.liyang.pro /data/backup/ >/dev/null 2>&1
00 03 * * * bash /data/script/auto_upload_dropbox.sh >/dev/null 2>&1

此时即可实现自动备份到DropBox。

赞(1) 打赏
未经允许不得转载:Charles's Blog » 自动备份网站到DropBox

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏