互联网服务
2012 年 8 月 23 日
自动备份OpenShift数据到Dropbox(2)—自动备份脚本和cron任务的设置
本文介绍了如何使用Dropbox Uploader实现网站数据自动备份并上传到Dropbox。备份流程主要包括备份数据库、备份网站文件、将数据库和网站文件打包上传到Dropbox以及添加cron任务实现定期备份等步骤。作者提供了一个简单的例子,可以实现通用OpenShift网站文件和数据库的备份,也可用于普通的VPS数据备份。读者只需根据自己的需求修改相应配置即可。最后,作者还建议添加日志功能以供日后分析。
配置完Dropbox Uploader之后就可以编写脚本实现自动备份网站的数据了。开始之前,我们先理清一下所有流程,网站自动备份到Dropbox大致需要以下几个步骤:
- 备份数据库;
- 备份网站文件;
- 将数据库和网站文件打包上传到dropbox;
- 添加cron任务自动完成上面的步骤,实现定期备份。
另外最好有日志功能供日后分析. 我写了个简单的例子,稍作修改可以实现通用OpenShift网站文件和数据库的备份,也可用于普通的VPS数据备份。点击 这里下载。
下面是具体的配置:
#!/bin/sh
#
# Dropbox Backup Cron Job Script v0.1
#
# Copyright (C) 2012 http://shuyz.com
#
# This script is expected to run on OpenShift, you may port it to other VPS by doing a little coding.
#
# 1: debug 0: run
debug=0
# Change the configuration below according to your apps
#
# OpenShift 应用名称
appname=apps
# 需要备份的目录路径,如果目录有多个,请用空格分割
src_folder=~/${appname}/repo/php
# 新建数据库的时候OpenShift的数据库信息
db_host=127.1.2.3
db_port=3306
db_user=admin
db_pass=pass123
db_name=wordpress
# dropbox uploader脚本的绝对路径
dropbox_uploader=~/${appname}/repo/dropbox/dropbox_uploader.sh
# 备份到Dropbox的哪个目录
remote_loc=/backup/openshift
# 配置完成!
# You may just keep the configuration below
bak_time=$(date +%Y%m%d%H%M%S)
tmp_folder=~/${appname}/tmp/bak_${bak_time}/
file_name=bak_${bak_time}.zip
bak_file=~/${appname}/tmp/${file_name}
db_file=${tmp_folder}${db_name}.sql
log_file=~/${appname}/logs/dropbox_bak_${bak_time}
# save message to log file or echo message on the screen
function showmsg() {
if [ ${debug} -eq 1 ]; then
echo [$(date +%Y/%m/%d.%H:%M:%S)] "$1"
else
if [ -f ${log_file} ]; then
echo [$(date +%Y/%m/%d.%H:%M:%S)] "$1" >> ${log_file}
else
echo [$(date +%Y/%m/%d.%H:%M:%S)] "$1" > ${log_file}
fi
fi
}
# remove temporary files and exit with error code
function endnow() {
showmsg "remove temporary files and directories"
if [ -d ${tmp_folder} ]; then
rm -rf ${tmp_folder}
fi
if [ -f ${bak_file} ]; then
rm ${bak_file}
fi
# exit
if [ "$1" -eq 0 ]; then
showmsg "backup succeed!"
exit 0
else
showmsg "backup failed!"
exit 1
fi
}
# check the exit code of previous command
function checkresult() {
if [ $? -ne 0 ]; then
showmsg "$1"
endnow 1
fi
}
showmsg "creating backup directory on ${tmp_folder}"
mkdir ${tmp_folder}
checkresult "failed to create backup directory"
showmsg "exporting databases: ${db_name}"
mysqldump -h${db_host} -p${db_port} -u${db_user} -p${db_pass} ${db_name} > ${db_file}
checkresult "failed to export database"
showmsg "archiving files in folder ${src_folder}"
cd ${src_folder}
checkresult "cannot change working directory to ${src_folder}"
zip -rvy ${tmp_folder}files.zip * >> ${log_file}
checkresult "failed to archive files"
showmsg "pack all files and database as ${bak_file}"
cd ${tmp_folder}
checkresult "cannot change working directory to ${tmp_folder}"
zip -rvy ${bak_file} * >> ${log_file}
checkresult "failed to pack all files"
showmsg "uploading ${bak_file} to dropbox"
$dropbox_uploader upload ${bak_file} ${remote_loc}/${file_name} >> ${log_file}
checkresult "upload failed!"
endnow 0
# 定时备份
ln -s ~/{appname}/repo/dropbox/bak_all_weekly.sh ~/{appname}/repo/.openshift/cron/weekly/dropbox_backup
本文为原创文章,未经允许请勿非法盗用资源。我们保护知识产权,尊重每一个创作者的劳动成果。如需转载,请务必注明出处并获得授权,谢谢合作!
作者:Lance | 发表日期:2012 年 8 月 23 日