个人博客的备份

上周vps被墙的问题,让我觉得vps主机还是不太安全,万一挂了所有的资料就都没有了(虽然也没啥)。于是就想备份一下,捣鼓了半天后,备份成功。其实也不难,只是以前没有想去做备份。

备份包括本地备份和异地备份,本地备份即将数据库、博客文件及配置文件打包备份到本地;异地备份为把本地备份上传到第三方存储或网站上(例如DropBox,免费空间有2G,足够个人博客备份使用了)。

操作步骤如下:

1、配置个人的Dropbox,创建一个app
下载dropbox脚本到vps主机上:

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

提示如下:

This is the first time you run this script, please follow the instructions:

 1) Open the following URL in your Browser, and log in using your account: https://www.dropbox.com/developers/apps
 2) Click on "Create App", then select "Dropbox API app"
 3) Now go on with the configuration, choosing the app permissions and access restrictions to your DropBox folder
 4) Enter the "App Name" that you prefer (e.g. MyUploader313092247529194)

 Now, click on the "Create App" button.

 When your new App is successfully created, please click on the Generate button
 under the 'Generated access token' section, then copy and paste the new access token here:

 # Access token: 输入上一步Generated access token生成的访问令牌

 > The access token is -xxxxxxxxxAAAAAAAAAAEGkDdDY4WDkrK7kzzzzzpoFXyyyyyyyyy. Looks ok? [y/N]: y
   The configuration has been saved.

配置完成。
创建APP
ScreenShot_001.jpg
生成访问令牌
ScreenShot_002.jpg

2、创建备份脚本
先创建好备份文件夹/data/backup,拷贝backup.sh、dropbox_uploader.sh到/usr/local/sbin/目录。
脚本如下:(计划每周备份一次,删除半个月以前的本地备份,一个月以前的网络备份)

#!/bin/bash

Today=`date +%Y%m%d`
Bakdir=/data/backup
Baklog=$Bakdir/backup_${Today}.log
Expire=`date -d -1month +%Y%m%d`
sqlpasswd="mysql_passwd"
Dropbox=/${Today}
Webdir=/usr/share/nginx/html

exec 1>>$Baklog
exec 2>>$Baklog

#Create Today BackupDirectory
if [ ! -d $Bakdir/$Today ]
  then
    mkdir $Bakdir/$Today
fi

#Backup Mysql DB
echo "###########################################"
echo "Backup Mysql DB:"
echo "Start Time:`date +"%F %H:%M:%S"`"
/usr/bin/mysqldump -uroot -p$sqlpasswd typecho >${Today}_typecho_bak.sql
tar zcpf ${Today}_typecho_bak.tar.gz ${Today}_typecho_bak.sql
rm -f ${Today}_typecho_bak.sql
mv ${Today}_typecho_bak.tar.gz $Bakdir/$Today/
echo "Stop Time:`date +"%F %H:%M:%S"`"

#Backup Website Data
echo "###########################################"
echo "Backup Website Data:"
echo "Start Time:`date +"%F %H:%M:%S"`"
tar zcpPf ${Today}_web_bak.tar.gz /usr/share/nginx/html
mv ${Today}_web_bak.tar.gz $Bakdir/$Today/
echo "Stop Time:`date +"%F %H:%M:%S"`"

#Upload Dropbox
echo "###########################################"
echo "Upload Backup:"
echo "Start Time:`date +"%F %H:%M:%S"`"
/usr/local/sbin/dropbox_uploader.sh upload $Bakdir/$Today/${Today}_typecho_bak.tar.gz ${Dropbox}/${Today}_typecho_bak.tar.gz
/usr/local/sbin/dropbox_uploader.sh upload $Bakdir/$Today/${Today}_web_bak.tar.gz ${Dropbox}/${Today}_web_bak.tar.gz
echo "Stop Time:`date +"%F %H:%M:%S"`"

#Delete old data
echo "###########################################"
echo "Delete expire data:"
echo "Start Time:`date +"%F %H:%M:%S"`"
find ${Bakdir} -mtime +14 |xargs rm -rf
/usr/local/sbin/dropbox_uploader.sh delete /$Expire
echo "Stop Time:`date +"%F %H:%M:%S"`"

3、备份配置文件
在web站点下新建配置文件备份目录(便于统一打包),拷贝以下配置文件到此目录
例如:

/etc/php.ini
/etc/php-fpm.d/www.conf
/etc/nginx/nginx.conf
/etc/nginx/fastcgi_params
/etc/nginx/conf.d/default.conf

等配置文件。

4、配置计划任务(每周五的晚上11点备份)
备份文件32M,上传到Dropbox只需要15秒左右。

0 23 * * 5 root /usr/bin/sh /usr/local/sbin/backup.sh > /dev/null 2>&1

手动执行脚本,输出日志如下:

# cat /data/backup/backup_20170927.log 
###########################################
Backup Mysql DB:
Start Time:2017-09-27 16:10:50
Stop Time:2017-09-27 16:10:50
###########################################
Backup Website Data:
Start Time:2017-09-27 16:10:50
Stop Time:2017-09-27 16:10:52
###########################################
Upload Backup:
Start Time:2017-09-27 16:10:52
 > Uploading "/data/backup/20170927/20170927_typecho_bak.tar.gz" to "/20170927/20170927_typecho_bak.tar.gz"... DONE
 > Uploading "/data/backup/20170927/20170927_web_bak.tar.gz" to "/20170927/20170927_web_bak.tar.gz"... DONE
Stop Time:2017-09-27 16:11:03
###########################################
Delete expire data:
Start Time:2017-09-27 16:11:03
 > Deleting "/20170827"... FAILED
Stop Time:2017-09-27 16:11:04

相关文章

发表新评论