自动备份OpenShift数据到Dropbox(2)—自动备份脚本和cron任务的设置

dropbox-openshift

配置完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  

配置完成之后上传到OpenShift,然后用SSH登录测试一下运行是否正常,如果出现错误,你可以将配置里的debug设为1,这样在运行是就可以看到错误提示了;另外OpenOpenhift的logs文件夹里也有详细的日志,

backup test

file in dropbox

backup logs

测试没有问题之后就可以添加一个cron任务实现自动备份了。首先给OpenShift添加cron模块,然后将脚本拷贝到cron下对应的daily, weekly等文件夹。建议在这些文件夹下使用软链接到真实脚本,这样比较方便修改脚本。比如设置一个每周一次的整站备份,只需要在weekly文件夹下建立一个软链接到备份脚本。

ln -s ~/{appname}/repo/dropbox/bak_all_weekly.sh ~/{appname}/repo/.openshift/cron/weekly/dropbox_backup 

softlink to backup script

注意的是如果cron任务的目录下有jobs.allow文件的话,还需要将脚本名称添加到这个白名单文件里,否则脚本不会运行。

cron script whitelist

到此全部配置完成,脚本每周会定时备份到Dropbox,下面是一张备份的文件列表:

backup file list

2013-06-24更新: 由于OpenShift对路径的细微调整,请将下载的脚本中的"appname"赋值为"app-root", 即"appname=app-root".

关键字:备份, OpenShift, PaaS, dropbox, cron

本文链接:树叶的BLOG >> 自动备份OpenShift数据到Dropbox(2)—自动备份脚本和cron任务的设置

本作品采用知识共享署名-非商业性使用-相同方式共享 3.0 Unported许可协议进行许可。

上一篇 : 自动备份OpenShift数据到Dropbox(1)—Dropbox Uploader的配置 下一篇 : cloudControl 绑定自定义域名