python 脚本备份 mysql 数据库到 OSS
脚本如下:
#!/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的更多相关文章
- 脚本备份MySQL数据库和binlog日志
用Mysqldump实现全库备份+binlog的数据还原 首先是为mysql做指定库文件的全库备份 vim mysqlbak.sh #!/bin/bash #定义数据库目录,要能找到mysqldump ...
- linux shell脚本备份mysql数据库
#!/bin/sh # 备份数据库 # Mysql 用户名密码 MYSQL_USER=root MYSQL_PASS=root BACKUP_DIR=/data/backup/mysql DATA_D ...
- python多线程备份MYSQL数据库并删除旧的备份。
#!/usr/bin/python # -*- coding=utf-8 -*- import time import os import datetime import threading from ...
- python 脚本备份mssql数据库并删除数据库
一.实现脚本 # -*- coding=utf-8 -*- import pyodbc from datetime import datetime import pymssql import os i ...
- python脚本对 mysql数据库进行增删改查操作
# -*- coding: utf-8 -*-import pymysqlimport xlrd# import codecsconn = pymysql.connect(host='127.0.0. ...
- Windows系统定时备份MySQL数据库
当一个网站投入使用时,定期备份数据库是必要的事.那么,在Windows系统上,我们该如何做呢? 如下语句可以实现备份及还原MySQL数据库: 备份MySQL数据库 mysqldump -uroot - ...
- 使用shell脚本定时执行备份mysql数据库
使用shell脚本定时执行备份mysql数据库 #!/bin/bash ############### common file ################ #本机备份文件存放目录 MYSQLBA ...
- 备份MySQL数据库并上传到阿里云OSS存储
1. 环境配置 要将本地文件上传到阿里云oss中, 必须使用阿里云提供的工具 ossutil, 有32位,也有64位的, Linux和Windows都有.具体可以到阿里云官网下载 官网及文档: htt ...
- 使用Windows任务计划程序和Python备份Mysql数据库
目标:每日定时自动备份Mysql数据库 方案: 1.安装Python: 使用的Python版本是Python3.7.1,下载地址:https://www.python.org/downloads/re ...
随机推荐
- ZOJ 3702 Gibonacci number 2017-04-06 23:28 28人阅读 评论(0) 收藏
Gibonacci number Time Limit: 2 Seconds Memory Limit: 65536 KB In mathematical terms, the normal ...
- MFC中的乱起八糟----字符编码:LPTSTR,LPCTSTR, TCHAR等
注意,编写有 Unicode 意识的代码总是一件好事,比如: CString graycat = CString(_T("Gray")) + _T("Cat") ...
- Anti-Anti dylib(反 反-dylib钩子(Anti-tweak))
版主提供了 anti dylib 的文章,http://bbs.chinapyg.com/thread-76158-1-1.html原理很简单,看下面源代码即可~ 在Build Settings中找 ...
- XML--使用XML来将字符串分隔成行数据
DECLARE @xml XML SET @xml=CAST(REPLACE('<ROOT><X>'+'AA,AB,AC,AD'+'</X></ROOT> ...
- html开发基础
1 Doctype Doctype告诉浏览器使用什么样的html或xhtml规范来解析html文档 有和无的区别 BackCompat:标准兼容模式未开启(或叫怪异模式[Quirks mode].混杂 ...
- Java中Io流操作-File类的常用操作-创建文件,创建文件夹
package com.hxzy.IOSer; import java.io.File;import java.io.IOException; public class Demo03 { public ...
- spring指导的index.html在spring文件夹中的位置
- jzoj100029
陪审团制度历来是司法研究中的一个热议话题,由于陪审团的成员组成会对案件最终的结果产生巨大的影响,诉讼双方往往围绕陪审团由哪些人组成这一议题激烈争夺. 小 W 提出了一个甲乙双方互相制衡的陪审团成员挑选 ...
- 详解sizeof与strlen
一,sizeof是C语言的一种单目运算符,与C语言的其他运算符++,--一样,它并不是函数:sizeof()以字节为单位给出了操作数的大小:sizeof的值是无符号int. strlen是一个函数,只 ...
- delphi 10.2---非常简单的数组用法求和
unit Unit9; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...