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 ...
随机推荐
- openfire搭建spackweb在线即时聊天
1.首先去openFire官网下载openFire以及spackweb,以下地址可以2样东西一次打包下载.http://download.csdn.net/detail/a315157973/8048 ...
- SqlServer循环执行存储过程
begin --申明变量 ) declare @zycs int --赋值变量 --申明游标 declare order_cursor cursor for (select blh, zycs fro ...
- threadpoolExecutor----自动执行任务
使用threadpoolExecutor,主要是任务的提交的执行和获取结果. 提交任务的方法有: 1.submit 2.execute 3.queue的add 其中1和2的使用必须是threadpoo ...
- Http 安全检测
httpsecurityreport.com www.ssllabs.com
- 常用脚本--Kill所有连接到指定数据库上的回话
USE [master] GO /****** Object: StoredProcedure [dbo].[Sp_KillAllProcessInDB] Script Date: 02/07/201 ...
- Orchard 介绍
0.Introduction 下载地址 https://orchard.codeplex.com/ Orchard 是由微软公司创建,基于 ASP.NET MVC 技术的免费开源内容管理系统: 可用于 ...
- WPF文字间距
代码: <ItemsControl ItemsSource="{Binding Info}" FontFamily="微软雅黑" FontSize=&qu ...
- Web Api 内部数据思考 和 利用http缓存优化 Api
在上篇<Web Api 端点设计 与 Oauth>后,接着我们思考Web Api 的内部数据: 其他文章:<API接口安全加强设计方法> 第一 实际使用应该返回怎样的数据 ? ...
- linux进程管理(二)
接上[linux进程管理(一)] 终止进程的工具 kill .killall.pkill 终止一个进程或终止一个正在运行的程序,一般是通过 kill .killall.pkill.xkill 等进行. ...
- 面向对象之-@classmethod、@staticmethod和@classonlymethod的区别
实例方法.静态方法与类方法的含义 实例方法(普通方法)的含义就是需要类对象实例之后才能调用的方法,该方法的基本格式为: def test(self,*args,**kwargs): # 第一个参数必须 ...