利用dropbox备份ghost blog

Dropbox Developer

  1. 依次点击My Apps → Create App → Dropbox API → App folder
  2. 在生成的 App 里,点击 Generated access token,生成一串 token 字符串,供dropbox_uploader.sh使用。

Dropbox_uploader

// https://github.com/andreafabrizi/Dropbox-Uploader
$ wget https://raw.github.com/andreafabrizi/Dropbox-Uploader/master/dropbox_uploader.sh 
$ chmod a+x dropbox_uploader.sh

// 绑定App
$ ./dropbox_uploader.sh

备份脚本

#!/bin/bash

# MYSQL
MYSQL_USER="root"  
MYSQL_PASS="<root_password>"  
MYSQL_DATABASENAME="<ghost_db_name>"

# Define backup folders
NGINX_CONF_DIR=/etc/nginx/sites-available  # nginx conf  
WEB_DIR=/var/www/ghost/content/data  # web data folder  
WEB_IMAGE=/var/www/ghost/content/images

# define backup folder
DROPBOX_DIR=/$(date +%Y-%m-%d)  # dropbox backup folder  
LOCAL_BAK_DIR=/home/dev/tmp  # tmp folder

# define backup
DBBakName=DB_$(date +%Y%m%d).tar.gz  
NginxConfBakName=NginxConf_$(date +%Y%m%d).tar.gz  
WebBakName=Web_$(date +%Y%m%d).tar.gz  
WebImgBakName=WebImg_$(date +%Y%m%d).tar.gz

# define old data
Old_DROPBOX_DIR=/$(date -d -7day +%Y-%m-%d)  
OldDBBakName=DB_$(date -d -10day +%Y%m%d).tar.gz  
OldNginxConfBakName=NginxConf_$(date -d -10day +%Y%m%d).tar.gz  
OldWebBakName=Web_$(date -d -10day +%Y%m%d).tar.gz  
OldWebImgBakName=WebImg_$(date -d -10day +%Y%m%d).tar.gz


cd $LOCAL_BAK_DIR

# export DB, zip by db
# for db in `mysql -u$MYSQL_USR -p$MYSQL_PWD -B -N -e 'SHOW DATABASES' | xargs`; do
#    (mysqldump -u$MYSQL_USR -p$MYSQL_PWD ${db} | gzip -9 - > ${db}.sql.gz)
# done

mysqldump -u$MYSQL_USER -p$MYSQL_PASS $MYSQL_DATABASENAME > $LOCAL_BAK_DIR/ghostdb.sql


# zip database
tar zcf $LOCAL_BAK_DIR/$DBBakName $LOCAL_BAK_DIR/ghostdb.sql  
rm -rf $LOCAL_BAK_DIR/*.sql

# zip nginx data
cd $NGINX_CONF_DIR  
tar zcf $LOCAL_BAK_DIR/$NginxConfBakName ./*

# zip web data
cd $WEB_DIR  
tar zcf $LOCAL_BAK_DIR/$WebBakName ./*

cd $WEB_IMAGE  
tar zcf $LOCAL_BAK_DIR/$WebImgBakName ./*

# upload
/home/dev/dropbox_uploader.sh upload /home/dev/tmp/$DBBakName $DROPBOX_DIR/$DBBakName
/home/dev/dropbox_uploader.sh upload /home/dev/tmp/$NginxConfBakName $DROPBOX_DIR/$NginxConfBakName
/home/dev/dropbox_uploader.sh upload /home/dev/tmp/$WebBakName $DROPBOX_DIR/$WebBakName
/home/dev/dropbox_uploader.sh upload /home/dev/tmp/$WebImgBakName $DROPBOX_DIR/$WebImgBakName

#Delete old files
rm -rf $LOCAL_BAK_DIR/$OldNginxConfBakName $LOCAL_BAK_DIR/$OldWebBakName $LOCAL_BAK_DIR/$OldWebImgBakName  
/home/dev/dropbox_uploader.sh delete $Old_DROPBOX_DIR/

echo -e "Backup Done!"

Cron Job

查看当前系统时间

date -R

修改时区

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
crontab –e

每天3:30自动备份

30 3 * * * /root/backup.sh
dev@blog:~$ sudo service cron restart

Ref

TENNFY WU » 定时备份VPS数据至Dropbox教程

Show Comments