Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import os
- import sys
- import datetime
- from datetime import datetime, timedelta
- import glob
- import string
- import smtplib
- import email
- #basic config - please do not change
- HOSTNAME = os.uname()[1]
- LOCKDIR = "/tmp/rn-innobackup.lock" #LOCKDIR
- LOGFILE = "/var/log/rn-mysqlbackup.log" #LOGFILE
- INNOBACKUPEX = "/usr/bin/innobackupex"
- ALERTMAIL = "mmaros@reflected.net" #to use multiple recepients, this needs to be turned into a list ["email1@bla.com", "email2@bla.com", "email3@bla.com", ...]
- #ALERTMAIL = "support@reflected.net" #to use multiple recepients, this needs to be turned into a list ["email1@bla.com", "email2@bla.com", "email3@bla.com", ...]
- #backup configuration
- #to use one binary over other, please put it at the start of the list, if it doesnt exist it will get skipped
- COMPRESS = ["/usr/bin/pbzip2","/usr/local/sbin/pbzip2","/usr/bin/pigz","/usr/local/sbin/pigz"]
- BACKUPDIR = '/home/backups/mysql/'
- RETENTION =int(7)
- STREAMING = False
- PARALEL = int(4)
- USEMEMORY = "1G"
- CUSTOMOPT = "" #separate them with a space between, use syntax same as on the cli "--option=value --option2=value
- DATADIR = '/home/mysql/data'
- ########Bunch of checks that need to be ran before we proceed
- ########
- #check which compression to use
- compression = ""
- for i in range(0, len(COMPRESS)):
- if os.path.isfile(COMPRESS[i]):
- compression = COMPRESS[i]
- break
- #exit if compression is not set up
- if compression == "":
- print ("none of compressions are available, please check which ones are available and which ones are provided")
- sys.exit()
- #exit if datadir doesnt exist
- if os.path.isdir(DATADIR) is False:
- print (DATADIR + "does not exit")
- sys.exit()
- #compression being used:
- if ("pbzip2" in compression):
- backupSuffix = ".tar.bz2"
- elif ("pigz" in compression):
- backupSuffix = ".tar.gz"
- else:
- print ("wrong compression")
- sys.exit()
- curDate = ""
- #skip disk check if .skipcheck is present in backupdir, remove after the run
- def skipCheck():
- if os.path.isfile(BACKUPDIR + ".skipcheck"):
- now = datetime.today().strftime("%Y-%m-%d %H:%M:%S")
- print(now + ": Skipping prerun disk space checks and running backups. skipcheck file will be removed")
- #function that runs backups
- os.remove(BACKUPDIR + ".skipcheck")
- else:
- isRunnable()
- #check total freeDisk disk space on partition containting BACKUPDIR, in bytes
- def freeSpace(BACKUPDIR):
- st = os.statvfs(BACKUPDIR)
- freeDisk = int(st.f_bavail * st.f_frsize)
- return freeDisk
- #get the size of the backup specified
- def lastBackup(BACKUPDIR, backupSuffix, oldDate):
- backupName = os.path.join(BACKUPDIR + 'mysql_backups_' + HOSTNAME + '_' + '*' + backupSuffix)
- print (backupName)
- listBackups = glob.iglob(backupName)
- print (listBackups)
- lastSize = os.path.getsize(max(glob.iglob(backupName) , key = os.path.getctime))
- print (lastSize)
- return lastSize
- #get current date in "20180409"/"YYYYMMDD" format, minus N days
- def curDate(oldAge):
- curDate = datetime.today() - timedelta(days=oldAge)
- curDate = curDate.strftime("%Y%m%d")
- return curDate;
- #get size of the mysql datadir
- def dirSize(path):
- total_size = 0
- for dirpath, dirnames, filenames in os.walk(path):
- for f in filenames:
- fp = os.path.join(dirpath, f)
- total_size += os.path.getsize(fp)
- return total_size
- #prerun checks
- def isRunnable():
- oldAge = 1
- oldDate = curDate(oldAge)
- lastSize = lastBackup(BACKUPDIR , backupSuffix, oldDate)
- freeDisk = freeSpace(BACKUPDIR)
- #if streaming backup, we dont care about size of the datadir
- if STREAMING:
- datadirSize = 0
- else:
- datadirSize = dirSize(DATADIR)
- print("Size of last backup")
- print(lastSize)
- print("Free disk size")
- print(freeDisk)
- print("Datadir size")
- print(datadirSize)
- if ((freeDisk - lastSize - datadirSize) * 1.05) < (lastSize + datadirSize):
- msgBody = "Cant run backups as there is not enough free disk space"
- sendMail(msgBody)
- print ("All gucci")
- #sendmail and exit function
- def sendMail(msgBody):
- sender = 'root@' + HOSTNAME + '.ded.reflected.net'
- receivers = ALERTMAIL
- msgBody = msgBody
- #print ("this works")
- #message = """From: """ + sender + """\nTo: """ + receivers + """\nSubject: backup failed on """ + HOSTNAME + """\n\n""" + msgBody + """\n"""
- #its still ugly, but ugly in mutliple lines
- message = ("From: " + sender + "\n"
- "To:" + receivers + "\n"
- "Subject: Backup failed on " + HOSTNAME + "\n\n"
- + msgBody + "\n")
- #smtpObj = smtplib.SMTP('smtp-out.reflected.net')
- #smtpObj.sendmail(sender, receivers, message)
- print ("Successfully sent email")
- sys.exit()
- skipCheck()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement