Skip to content

Instantly share code, notes, and snippets.

@ShahriyarR
Last active December 22, 2015 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShahriyarR/6389175 to your computer and use it in GitHub Desktop.
Save ShahriyarR/6389175 to your computer and use it in GitHub Desktop.
Remote Binary Log backup script
# Binary log backup script
import os
import configparser
import subprocess
import shlex
class BinlogSyncClass:
def __init__(self,conf = 'datadir.conf'):
# Opening config file
con = configparser.ConfigParser()
con.read('datadir.conf')
bolme = con.sections()
DB = bolme[0]
# Giving values from config file
self.mysql = con[DB]['mysql']
self.mycnf = con[DB]['mycnf']
self.mysqlbinlog = con[DB]['mysqlbinlog']
self.myremoteconnection = con[DB]['remoteconnection']
self.mysqladmin = con[DB]['mysqladmin']
self.binlogdir = con[DB]['binlogsyncdir']
self.myoptions=con[DB]['myoptions']
# Checking for remote MySQL Up or NOT
statusargs = '%s %s status' % (self.mysqladmin,self.myremoteconnection)
statusargs = shlex.split(statusargs)
myadmin = subprocess.Popen(statusargs,stdout = subprocess.PIPE)
if not ('Uptime' in str(myadmin.stdout.read())):
print('Server is NOT Up')
else:
print('Server is Up')
if not os.path.exists(self.mycnf):
print('Check MySQL configuration file path')
if not os.path.exists(self.mysql):
print('Can not found MySQL')
if not os.path.exists(self.mysqladmin):
print('Check MySQL Admin')
if not os.path.exists(self.mysqlbinlog):
print('Check mysqlbinlog tool')
if not (os.path.exists(self.binlogdir)):
print('directory does not exist. Creating directory...')
os.makedirs(self.binlogdir)
print('Created')
# Function To get binary logs from remote host
def sync_binlog_func(self):
#Changing Directory
os.chdir(self.binlogdir)
print(os.getcwd())
# Making running commands
args = '%s %s %s mysql-bin.000001' % (self.mysqlbinlog, self.myremoteconnection, self.myoptions)
args=shlex.split(args)
p=subprocess.Popen(args)
a = BinlogSyncClass()
a.sync_binlog_func()
[MySQL]
mysql = /usr/bin/mysql
mycnf = /usr/my.cnf
mysqlbinlog = /usr/bin/mysqlbinlog
mysqladmin = /usr/bin/mysqladmin
remoteconnection = --user=remote --password=12345 --host=192.168.1.18
binlogsyncdir = /home/sh/remote_binlogs
myoptions= --read-from-remote-server --raw --to-last-log
originalbinlogdir = /home/sh/remote_binlogs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment