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 ...
随机推荐
- 团体程序设计天梯赛L2-001 紧急救援 2017-03-22 17:25 93人阅读 评论(0) 收藏
L2-001. 紧急救援 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国 ...
- TSQL--如何突破PRINT的8000大限
相信很多DBA都喜欢干的一件事就是拼SQL语句,我也不例外,但是PRINT只能打印4000的Unicode string或8000的Non-unicode string, 这个蛋疼的限制会导致过长的s ...
- solr特点七:Plugins(扩展点)
http://wiki.apache.org/solr/SolrPlugins 在 Solr 1.3 中,扩展 Solr 以及配置和重新整理扩展变得十分简单.以前,您需要编写一个 SolrReques ...
- 学习React前端框架,报错 'render' is not defined no-undef
报错 'render' is not defined no-undef 原因没有 写 import { render } from 'react-dom'
- sql trace script
CREATE EVENT SESSION [sql_query_tracing] ON SERVER ADD EVENT sqlos.wait_info( ACTION(sqlos.schedu ...
- java学习笔记—使用HttpSession实现QQ的访问记录(31)
1. 编写QQ空间数据类(QQS.java) public class QQS { private static LinkedHashMap<Integer, String> qqs = ...
- 给洛谷填坑的spj……
这里提供了洛谷某些题的$special\ judge$,供需要的oier拿过去对拍. 1.P3825 #include "testlib.h" using namespace st ...
- Java多线程(汇聚页)
Java多线程(汇聚页) Java多线程总结
- Lexicographical Substring Search (spoj7259) (sam(后缀自动机)+第k小子串)
Little Daniel loves to play with strings! He always finds different ways to have fun with strings! K ...
- 苹果笔记本调整 pycharm 字体大小的地方
我想,对于习惯了使用windows版本 或者 乌班图版本 的pycharm 的人而言, mac版本调节字体的地方藏的实在是太坑爹了.