数据库自动备份到七牛云(省钱的土方案)

公司项目采用了多种编程语言,权衡业界各类API工具,最终采用了eolinker做为API管理工具。

无奈项目人数和关联周边较多,在线免费配额明显不够用(之前可以通过设置管理员可以绕过限制,后面新版本被封了),so 又另谋出路。

后面发现eolinker真是业界良心产品,开源了基础功能代码,完全满足小公司需求:https://github.com/eolinker

自建管理系统就需要考虑备份的问题,目前思路如下:

1、注册七牛账号---有10G免费空间;

2、下载七牛qshell程序文件(七牛云上传文件程序)到linux服务器,然后熟悉使用方法:https://github.com/qiniu/qshell

3、编写数据库导出脚本,脚本导出完成后qshell上传文件到七牛;

4、linux定时执行备份脚本;

linux执行:
crontab -e
插入:
0 */12 * * *     /etc/scripts/mysql_backup.sh  >>/var/log/mysql_backup.log 2>&1

# 00.00, 08:00, 16:00 这几个时间自动执行备份.

注意:需要提前执行qshell命令完成密钥设置(qshell account ak sk name),具体查看:https://developer.qiniu.com/kodo/tools/1302/qshell#4

备份脚本思路如下:

#!/bin/bash
# Author: Sean,[email protected]
# Used for mysql backup

export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
BIN=mysql-backup-customhouse
LOGTIME='eval date "+%Y-%m-%d %H:%M:%S"'
export LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
echo "[ `$LOGTIME` | ${BIN} ]"

# Define the DB's user & password 
DB_HOST=$1
DB_PORT=$2
DB_USER=$3
DB_PASS=123456

# databases like: aaa,bbb,ccc
DB_TO_BACKUP=eolinker_os
MYSQL_CONF_FILE=/etc/my.cnf
BACKUP_PATH=/data/backup

[ -z "$DB_HOST" ] && DB_HOST=localhost
[ -z "$DB_PORT" ] && DB_PORT=3306
[ -z "$DB_USER" ] && DB_USER=root


# use ip address as hostid
if [ "$DB_HOST" = "localhost" -o "$DB_HOST" = "127.0.0.1" ]; then
    HOST_ID=`ip route get 8.8.8.8 | grep src | \
    awk '{ i=1; while(iif ( $i == "src" ) print $(i+1); i++ }}'`
else
    HOST_ID=$DB_HOST
fi
 
# Del the old backup before 30 days, the default unit is "Day"
RESERVE_DAY=30

# backup to the dir 
DATE=`date "+%Y%m%d"`
TIME=`date "+%H%M%S"`

# loop to backup db
if [ -z "$DB_TO_BACKUP" ]; then 
    # zero means all
    DB_TO_BACKUP=`mysql -h${DB_HOST} -P${DB_PORT} -u${DB_USER} -p${DB_PASS} </dev/null
show databases;
EOF
`
    # connection error
    if [ $? -ne 0 ]; then
        echo "[ `$LOGTIME` | ${BIN} ] DB connection error!"
        echo "[ `$LOGTIME` | ${BIN} ] Use: \"mysql -h${DB_HOST} -P${DB_PORT} -u${DB_USER} -p${DB_PASS}\" to check, exit 10" && exit 10
    fi
    
    DB_TO_BACKUP=$(echo "$DB_TO_BACKUP" | egrep -v "Database|information_schema|performance_schema" | xargs)
fi

[ -z "$DB_TO_BACKUP" ] && echo "[ `$LOGTIME` | ${BIN} ] No database found, exit 0" && exit

for i in `echo $DB_TO_BACKUP | tr ',' ' '`; do
    echo "[ `$LOGTIME` | ${BIN} ] Begin to backup $i."
    mkdir -p ${BACKUP_PATH}/${HOST_ID}_${DATE}
    if mysqldump -h${DB_HOST} -P${DB_PORT} -u${DB_USER} -p${DB_PASS} \
    $i 2>/dev/null | gzip >${BACKUP_PATH}/${HOST_ID}_${DATE}/${i}_${TIME}.sql.gz; then
        echo "[ `$LOGTIME` | ${BIN} ] Successful backup to ${BACKUP_PATH}."
        BACK_SUCC="true"
    else
        echo "[ `$LOGTIME` | ${BIN} ] Backup $i failed!"
    fi
done

for i in `echo $DB_TO_BACKUP | tr ',' ' '`; do
 echo ${BACKUP_PATH}/${HOST_ID}_${DATE}/${i}_${TIME}.sql.gz
 /etc/scripts/qshellc/qshell_linux_x64 rput eolinker-mysql-virtual ${i}_${DATE}_${TIME}.sql.gz ${BACKUP_PATH}/${HOST_ID}_${DATE}/${i}_${TIME}.sql.gz -w
done

# Del the directory that old than $RESERVE_DAY
#if [ "$BACK_SUCC" = "true" ]; then
#    find $BACKUP_PATH -type d -mtime +$RESERVE_DAY -exec rm -rf {} \; 2>/dev/null
#fi

# qshell account ak sk name

备份结果:

数据库自动备份到七牛云(省钱的土方案)_第1张图片

 

转载于:https://www.cnblogs.com/huige-you/p/10265733.html

你可能感兴趣的:(数据库自动备份到七牛云(省钱的土方案))