数据库备份至百度云

背景

  • 个人小站点用的云主机
  • 数据必须备份
  • 主从备份意味着花更多的钱(穷。。)

思路

  1. dump 整个database
  2. 将文件同步至百度云
  3. 定期执行

工具

  1. mysqldump
  2. bypt-百度云/百度网盘Python客户端
  3. crontab

方案

mysqldump

mysqldump dbname > /home/back/mysqldump/dbname_`date +%Y%m%d_%H%M%S`.sql

注意需要更改my.cnf:

[mysqldump]
user=
password=

python 代码

import os
import datetime

dbs = ['dbname1', 'dbname2']

def dump(dbname):
    cmd = 'mysqldump %s > /home/back/mysqldump/%s_' % (dbname, dbname)
    cmd += '`date +%Y%m%d_%H%M%S`.sql'
    return os.system(cmd) == 0

def upload(dbname):
    cmd = 'bypy upload /home/back/mysqldump/%s_' % dbname
    cmd += '`date +%Y%m%d_%H%M%S`.sql'
    return os.system(cmd) == 0

for db in dbs:
    if not dump(db) or not upload(db):
        print '%s faild to backup database=%s' % (datetime.datetime.now(), db)
    else:
        print '%s success backup database=%s' % (datetime.datetime.now(), db)

crontab

python /xxxx/workspace/scripts/backup.py >>  /home/back/mysqldump/backup.log

你可能感兴趣的:(数据库备份至百度云)