脚本如下:

 #!/usr/bin/python
###########################################################
########################################################## # Import required python libraries
import os
import time
import datetime # MySQL database details to which backup to be done. Make sure below user having enough privileges to take databases backup.
# To take multiple databases backup, create any file like /backup/dbnames.txt and put databses names one on each line and assignd to DB_NAME variable. DB_HOST = 'localhost'
DB_USER = 'root'
DB_USER_PASSWORD = ''
DB_NAME = '/home/dbbackup/dbnames.txt'
#DB_NAME = 'db_name'
BACKUP_PATH = '/mnt/backup/mysql/' # Getting current datetime to create seprate backup folder like "12012013-071334".
DATETIME = time.strftime('%Y%m%d-%H%M%S') TODAYBACKUPPATH = BACKUP_PATH + DATETIME # Checking if backup folder already exists or not. If not exists will create it.
print "creating backup folder"
if not os.path.exists(TODAYBACKUPPATH):
os.makedirs(TODAYBACKUPPATH) # Code for checking if you want to take single database backup or assinged multiple backups in DB_NAME.
print "checking for databases names file."
if os.path.exists(DB_NAME):
file1 = open(DB_NAME)
multi = 1
print "Databases file found..."
print "Starting backup of all dbs listed in file " + DB_NAME
else:
print "Databases file not found..."
print "Starting backup of database " + DB_NAME
multi = 0 # Starting actual database backup process.
if multi:
in_file = open(DB_NAME,"r")
flength = len(in_file.readlines())
in_file.close()
p = 1
dbfile = open(DB_NAME,"r") while p <= flength:
db = dbfile.readline() # reading database name from file
db = db[:-1] # deletes extra line
dumpcmd = "mysqldump -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + TODAYBACKUPPATH + "/" + db + ".sql"
os.system(dumpcmd)
p = p + 1
dbfile.close()
else:
db = DB_NAME
dumpcmd = "mysqldump -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + TODAYBACKUPPATH + "/" + db + ".sql"
os.system(dumpcmd) print "Backup script completed"
print "Your backups has been created in '" + TODAYBACKUPPATH + "' directory" print "current path: " + os.getcwd()
os.chdir(BACKUP_PATH)
print "current path: " + os.getcwd()
compress_file = TODAYBACKUPPATH + ".tar.gzip"
print compress_file
compress_cmd = "tar -czvf " + compress_file + " " + DATETIME
print "compress_cmd " + compress_cmd
os.system(compress_cmd)
print "Compress success" remove_cmd = "rm -rf " + TODAYBACKUPPATH
print "remove_cmd " + remove_cmd
os.system(remove_cmd)
print "Remove success" print "Start put OSS" import shutil
import oss2 access_key_id = ''
access_key_secret = ''
bucket_name = 'x-backup'
endpoint = 'oss-cn-hangzhou-internal.aliyuncs.com' bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
bucket.put_object_from_file('mysql/'+DATETIME+'.tar.gzip',compress_file)
print "Put OSS success"

再加上crontab 就搞定了

crontab -e

*/ * * * * python /home/dbbackup/dbbackup.py >> /home/dbbackup/mylog.log >&

python 脚本备份 mysql 数据库到 OSS的更多相关文章

  1. 脚本备份MySQL数据库和binlog日志

    用Mysqldump实现全库备份+binlog的数据还原 首先是为mysql做指定库文件的全库备份 vim mysqlbak.sh #!/bin/bash #定义数据库目录,要能找到mysqldump ...

  2. linux shell脚本备份mysql数据库

    #!/bin/sh # 备份数据库 # Mysql 用户名密码 MYSQL_USER=root MYSQL_PASS=root BACKUP_DIR=/data/backup/mysql DATA_D ...

  3. python多线程备份MYSQL数据库并删除旧的备份。

    #!/usr/bin/python # -*- coding=utf-8 -*- import time import os import datetime import threading from ...

  4. python 脚本备份mssql数据库并删除数据库

    一.实现脚本 # -*- coding=utf-8 -*- import pyodbc from datetime import datetime import pymssql import os i ...

  5. python脚本对 mysql数据库进行增删改查操作

    # -*- coding: utf-8 -*-import pymysqlimport xlrd# import codecsconn = pymysql.connect(host='127.0.0. ...

  6. Windows系统定时备份MySQL数据库

    当一个网站投入使用时,定期备份数据库是必要的事.那么,在Windows系统上,我们该如何做呢? 如下语句可以实现备份及还原MySQL数据库: 备份MySQL数据库 mysqldump -uroot - ...

  7. 使用shell脚本定时执行备份mysql数据库

    使用shell脚本定时执行备份mysql数据库 #!/bin/bash ############### common file ################ #本机备份文件存放目录 MYSQLBA ...

  8. 备份MySQL数据库并上传到阿里云OSS存储

    1. 环境配置 要将本地文件上传到阿里云oss中, 必须使用阿里云提供的工具 ossutil, 有32位,也有64位的, Linux和Windows都有.具体可以到阿里云官网下载 官网及文档: htt ...

  9. 使用Windows任务计划程序和Python备份Mysql数据库

    目标:每日定时自动备份Mysql数据库 方案: 1.安装Python: 使用的Python版本是Python3.7.1,下载地址:https://www.python.org/downloads/re ...

随机推荐

  1. Android-自定义联系人快速索引

    效果图: 布局去指定 view.custom.shangguigucustomview.MyCustomIndexView 自定义View对象 <!-- 自定义联系人快速索引 --> &l ...

  2. kubernetes yaml

    apiVersion: v1 #指定api版本,此值必须在kubectl apiversion中 kind: Deployment #指定创建资源的角色/类型 metadata: #资源的元数据/属性 ...

  3. ajax 与 form 提交的区别

    有如下几种区别: 1. Ajax在提交.请求.接收时,都是异步进行的,网页不需要刷新:Form提交则是新建一个页面,哪怕是提交给自己本身的页面,也是需要刷新的: 2. A在提交时,是在后台新建一个请求 ...

  4. eCharts IE8兼容性问题

    使用Echart的图表柱状图,里面用了Float32Array,IE8下面会提示无法找到Float32Array,黄色叹号. 网上查找后使用如下方法解决:不明觉厉 在<head></ ...

  5. sql-修改每条数据的某一个字段的值

    update B set B.maildata =(select SUBSTRING(maildata,0,3) from basedata where basedata.cid = B.cid)+( ...

  6. sqlhelper写调用存储过程方法

    public static object Proc(string ProcName, SqlParameter[] parm) { conn.Open(); //最后一个参数为输出参数 parm[pa ...

  7. C#使用LitJson对Json数据解析

    JSON 介绍 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - ...

  8. 【.net】Dictionary<TKey, TValue>源码分析

    一图胜过千言万语~

  9. 【01】Kubernets:捋一捋概念性东西

    写在前面的话 docker 先告一段,现在开始进入 Kubernets(K8S) 的学习阶段,在学习过程中,可结合之前学的 docker swarm 比对着理解. 啥是 K8S 先来看一下两个 log ...

  10. Delphi XE7编译安卓程序出错了

    昨天编译一个先前可以正常运行的程序,忽然就不能编译了,总是提示这个错误,经过一番排查,终于搞定了,原因:删除了安卓lib引用的JAR和单元文件.如果你也出现这个问题,打开工程全部目录,看一下是否有打小 ...