|
本帖最后由 vpsbar 于 2014-4-24 21:48 编辑
转载自vpsbar.im.
博客原文 http://blog.vpsbar.im/?p=322
Dropbox,一个提供同步本地文件的网络存储在线应用,它提供了丰富的API,我们可以通过API将自己VPS备份的数据立刻同步到DropBox中,而DropBox提供的免费空间最够大部分站长的备份空间需求。我们利用DropBox这样的第三方脚本,来实现同步与删除。
配置自己的Dropbox,创建一个app
下载DropBox脚本,到自己的VPS上
[ol]# wget https://raw.github.com/andreafabrizi/Dropbox-Uploader/master/dropbox_uploader.sh# chmod +x dropbox_uploader.sh# ./dropbox_uploader.sh[/ol]复制代码
运行该脚本,根据以下提示进行DropBox的设置
[ol]This is the first time you run this script.1) Open the following URL in your Browser, and log in using your account: https://www2.dropbox.com/developers/apps在浏览器中打开,https://www2.dropbox.com/developers/apps2) Click on "Create App", then select "Dropbox API app"点击"Create App"按钮,然后选择"Dropbox API app"3) Select "Files and datastores"选择"Files and datastores"选项4) Now go on with the configuration, choosing the app permissions and access restrictions to your DropBox folder现在去进行配置,选择这个app对你整个Dropbox文件夹的访问权限5) Enter the "App Name" that you prefer (e.g. MyUploader113012919723169)输入一个你喜欢的App名称Now, click on the "Create app" button.When your new App is successfully created, please type theApp Key, App Secret and the Permission type shown in the confirmation page:# App key: xxxxxxxxxx# App secret: xxxxxxxxx# Permission type, App folder or Full Dropbox [a/f]: a> App key is xxxxxxxxxx, App secret is xxxxxxxxxx and Access level is App Folder, it's ok? [y/n]y> Token request... OKPlease open the following URL in your Browser, and allow Dropbox Uploaderto access your DropBox folder:--> https://www2.dropbox.com/1/oauth/authorize?oauth_token=iP0TqrmjjKcAUfwWPress enter when done...> Access Token request... OKSetup completed![/ol]复制代码
到此为止,关于Dropbox的设置就结束了,接下来按照自己的情况编写备份同步脚本。
脚本下载地址 http://vpsbar.qiniudn.com/backup.sh
同学们可以修改好以后在上传,或者直接在vps里面下载方法如下
[ol]wget http://vpsbar.qiniudn.com/backup.sh[/ol]复制代码
然后修改
[ol]vim backup.sh[/ol]复制代码
下面是修改的信息
[ol]#!/bin/bash## when:2013/10/15# who:http://blog.coocla.orgTODAY=`date -I` # 获取当前日期BACKUP_LOG=/data/backup/backup_${TODAY}.log # 备份日志Expire=`date -d -7day +"%Y-%m-%d"` # 获取7天前的日期MYSQL_USER="root" # Mysql用户MYSQL_PASS="rootpass" # Mysql密码MYSQL_DB=('blog' 'yunxiaojia') # 要备份的数据库名BACK_DIR=/data/backup # 备份存放的目录Dropbox=/${TODAY} # Dropbox上创建的app存放目录,这里的根(/)是指app的根目录WEB_DATA=/data/www/wwwroot # 网页文件目录#Create Today BackupDirectoryif [ ! -d $BACK_DIR/$TODAY ];then mkdir $BACK_DIR/$TODAYfi#Backup Mysql DBecho "###############################################################" > $BACKUP_LOGecho "Backup Mysql DB." >> $BACKUP_LOGecho "Start Time : `date +%F" "%H:%M:%S`" >> $BACKUP_LOGfor db in ${MYSQL_DB[@]};do /usr/bin/mysqldump -u$MYSQL_USER -p$MYSQL_PASS --skip-opt --add-drop-table --create-options -q -e --set-charset --routines --single-transaction --master-data=2 $db > ${TODAY}_${db}_full_back.sql --log-error=$BACKUP_LOGdonetar zcf ${TODAY}_db_full_back.tar.gz *.sqlrm -f *.sqlmv ${TODAY}_db_full_back.tar.gz ${BACK_DIR}/${TODAY}/echo "Stop Time : `date +%F" "%H:%M:%S`" >> $BACKUP_LOG#Backup Website Dataecho "###############################################################" >> $BACKUP_LOGecho "Backup Website Data." >> $BACKUP_LOGecho "Start Time : `date +%F" "%H:%M:%S`" >> $BACKUP_LOGcd $WEB_DATAtar zcf ${TODAY}_web_full_back.tar.gz ./* && cd -mv ${WEB_DATA}/${TODAY}_web_full_back.tar.gz ${BACK_DIR}/${TODAY}/echo "Stop Time : `date +%F" "%H:%M:%S`" >> $BACKUP_LOG#Upload Dropboxecho "###############################################################" >> $BACKUP_LOGecho "Upload backup." >> $BACKUP_LOGecho "Start Time : `date +%F" "%H:%M:%S`" >> $BACKUP_LOG/usr/local/sbin/dropbox_uploader.sh upload ${BACK_DIR}/${TODAY}/${TODAY}_db_full_back.tar.gz ${Dropbox}/${TODAY}_db_full_back.tar.gz >> $BACKUP_LOG/usr/local/sbin/dropbox_uploader.sh upload ${BACK_DIR}/${TODAY}/${TODAY}_web_full_back.tar.gz ${Dropbox}/${TODAY}_web_full_back.tar.gz >> $BACKUP_LOGecho "Stop Time : `date +%F" "%H:%M:%S`" >> $BACKUP_LOG#Delete old dataecho "###############################################################" >> $BACKUP_LOGecho "Delete expire data." >> $BACKUP_LOGecho "Start Time : `date +%F" "%H:%M:%S`" >> $BACKUP_LOGfind ${BACK_DIR} -mtime +3 | xargs rm -rf/usr/local/sbin/dropbox_uploader.sh delete /$Expire/ >> $BACKUP_LOGecho "Stop Time : `date +%F" "%H:%M:%S`" >> $BACKUP_LOG[/ol]复制代码
然后将该脚本和dropbox_uploader.sh脚本放置于/usr/local/sbin或同一目录下,然后定义crontab计划任务:
根据日志中开始时间和结束时间,小伙伴们可以定义crontab开始的时间,由于我的内容比较少,在1分钟只能即可完成,我的crontab设置如下:
[ol]59 23 * * * /bin/sh /usr/local/sbin/backup.sh > /dev/null 2>&1[/ol]复制代码
同学们下载DropBox客户端安装在自己的PC或Mac上,然后进行同步,手动运行以上脚本或者等待计划任务执行后,待自动同步后,即可在windows或Mac下查看的到刚才同步上去的备份文件啦!
最后~ 小站新开求IP www.vpsbar.im 上百家VPS推荐
|
|