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 ...
随机推荐
- Intel Cyclone SoC FPGA介绍
3.1 Intel Cyclone SoC FPGA介绍 3.1.1 SoC FPGA的基本概念 Intel Cyclone V SoC FPGA是Intel PSG(原Altera)于2013年发布 ...
- redis修改端口号
为redis分配一个8888端口,操作步骤如下: 1.$REDIS_HOME/redis.conf重新复制一份,重命名为redis8888.conf. 2.打开redis8888.conf配置文件,找 ...
- Android各国语言Values文件夹命名规则
android多国语言文件夹文件汇总如下: 中文(中国):values-zh-rCN 中文(台湾):values-zh-rTW 中文(香港):values-zh-rHK 英语(美国):values-e ...
- [翻译]Component Registration in Script System 在脚本系统中注册组件
Component Registration in Script System 在脚本系统中注册组件 To refer to our component from a script, the cl ...
- [leetcode] 6. Balanced Binary Tree
这个题目纠结了一会儿,终于从二叉树转化到AVL了.题目如下: Given a binary tree, determine if it is height-balanced. For this pro ...
- vs2008快捷键极其技巧
vs2008快捷键极其技巧 1. 工具: Microsoft Visual Studio 2008 Version 9.0.21022.8 RTM Microsoft .NET Framework V ...
- c# HighCharts使用
最近接到个图形报表的需求,网络上找了几个插件,最后决定用highcharts 需要的文件 1.bll文件,添加到项目引用 http://files.cnblogs.com/files/loveju ...
- c# 生成二维码图片
转载自:https://blog.csdn.net/hyunbar/article/details/78271778 1.在C#中直接引用ThoughtWorks.QRCode.dll 类 2.封装方 ...
- markdown字体或者图片居中
1.图片居中实例: 图片居中效果: 2.文字居中实例: 文字居中效果: 你的名字
- 工具IDEA 配置springboot+maven项目
工具IDEA 配置springboot+maven项目 首先安装IDEA,至于怎么安装就不介绍了.. 第一步 配置maven环境 首先安装maven,先在网上下载一个maven包.在IDEA的sett ...