daily pastebin goal
63%
SHARE
TWEET

Untitled

a guest Apr 27th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2.  
  3. import os
  4. import subprocess
  5. import sys
  6. import datetime
  7. from datetime import datetime, timedelta
  8.  
  9. import glob
  10. import string
  11.  
  12. import smtplib
  13. import email
  14.  
  15. #basic config - please do not change
  16. HOSTNAME = os.uname()[1]
  17. LOCKDIR = "/tmp/rn-innobackup.lock" #LOCKDIR
  18. LOGFILE = "/var/log/rn-mysqlbackup.log" #LOGFILE
  19. INNOBACKUPEX = "/usr/bin/innobackupex"
  20. 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", ...]
  21. #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", ...]
  22.  
  23.  
  24. #backup configuration
  25. #to use one binary over other, please put it at the start of the list, if it doesnt exist it will get skipped
  26. COMPRESS = ["/usr/bin/pbzip2","/usr/local/sbin/pbzip2","/usr/bin/pigz","/usr/local/sbin/pigz"]
  27. BACKUPDIR = '/home/backups/mysql/'
  28. RETENTION  =int(7)
  29. STREAMING = False
  30. PARALEL = int(4)
  31. USEMEMORY = "1G"
  32. CUSTOMOPT = "" #separate them with a space between, use syntax same as on the cli "--option=value --option2=value
  33. DATADIR = '/home/mysql/data'
  34.  
  35.  
  36. ########Bunch of checks that need to be ran before we proceed
  37. ########
  38. #check which compression to use
  39. compression = ""
  40. for i in range(0, len(COMPRESS)):
  41.     if os.path.isfile(COMPRESS[i]):
  42.         compression = COMPRESS[i]
  43.         break
  44. #exit if compression is not set up
  45. if compression == "":
  46.     print ("none of compressions are available, please check which ones are available and which ones are provided")
  47.     sys.exit()
  48. #exit if datadir doesnt exist
  49. if os.path.isdir(DATADIR) is False:
  50.     print (DATADIR + "does not exit")
  51.     sys.exit()
  52.  
  53. #compression being used:
  54. if ("pbzip2" in compression):
  55.     backupSuffix = ".tar.bz2"
  56. elif ("pigz" in compression):
  57.     backupSuffix = ".tar.gz"
  58. else:
  59.     print ("wrong  compression")
  60.     sys.exit()
  61. curDate = ""
  62.  
  63. #skip disk check if .skipcheck is present in backupdir, remove after the run
  64. def skipCheck():
  65.     if os.path.isfile(BACKUPDIR + ".skipcheck"):
  66.         now = datetime.today().strftime("%Y-%m-%d %H:%M:%S")
  67.         print(now + ": Skipping prerun disk space checks and running backups. skipcheck file will be removed")
  68.         #function that runs backups
  69.         os.remove(BACKUPDIR + ".skipcheck")
  70.     else:
  71.         isRunnable()
  72.  
  73. #check total freeDisk disk space on partition containting BACKUPDIR, in bytes
  74. def freeSpace(BACKUPDIR):
  75.     st = os.statvfs(BACKUPDIR)
  76.     freeDisk = int(st.f_bavail * st.f_frsize)
  77.     return freeDisk
  78.  
  79. #get the size of the backup specified
  80. def lastBackup(BACKUPDIR, backupSuffix):
  81.     backupName = os.path.join(BACKUPDIR + 'mysql_backups_' + HOSTNAME + '_' + '*' + backupSuffix)
  82.     print ("+++++++++++++++++++++++++++++++++++++++++++++++")
  83.     listBackups = glob.glob(backupName)
  84.     print (listBackups)
  85.     print ("+++++++++++++++++++++++++++++++++++++++++++++++")
  86.     lastSize = os.path.getsize(max(glob.glob(backupName) , key = os.path.getctime))
  87.     return lastSize
  88.  
  89. #get size of the mysql datadir
  90. def dirSize(path):
  91.     total_size = 0
  92.     for dirpath, dirnames, filenames in os.walk(path):
  93.         for f in filenames:
  94.             fp = os.path.join(dirpath, f)
  95.             total_size += os.path.getsize(fp)
  96.     return total_size
  97.  
  98. #prerun checks
  99. def isRunnable():
  100.     oldAge = 1
  101.     lastSize = lastBackup(BACKUPDIR , backupSuffix)
  102.     freeDisk = freeSpace(BACKUPDIR)
  103.     #if streaming backup, we dont care about size of the datadir
  104.     if STREAMING:
  105.         datadirSize = 0
  106.     else:
  107.         datadirSize = dirSize(DATADIR)
  108.     print("Size of last backup")
  109.     print(lastSize)
  110.     print("Free disk size")
  111.     print(freeDisk)
  112.     print("Datadir size")
  113.     print(datadirSize)
  114.     if ((freeDisk - lastSize - datadirSize) * 1.05) < (lastSize + datadirSize):
  115.         msgBody = "Cant run backups as there is not enough free disk space"
  116.         sendMail(msgBody)
  117.     print("All gucci")
  118.     runBackup()
  119.  
  120. def runBackup():
  121.     #how to split it
  122.     #https://docs.python.org/2/library/subprocess.html
  123.     #
  124.     print("runBackup")
  125.     if not STREAMING:
  126.         print(INNOBACKUPEX + " " + CUSTOMOPT + " --slave-info " + BACKUPDIR)
  127.     else:
  128.         print("stuff")
  129.         #$innobackupex $innobackupex_backup_options --stream=tar $WORKDIR 2> $lockdir/innobackupex-run.log | $COMPRESS $COMPRESS_ARGS -c > $BACKUPDIR/$FILEPREFIX.$FILESUFFIX
  130.     #bashCommand = ['ls', '-lah', '/home/backups']
  131.     #subprocess.run(bashCommand)
  132.     #when the innobackupex compresses the data to workdir add apply log.
  133.     #if streaming, no apply log!
  134.     #when this is done, compress the directory using COMPRESSION
  135.     #add checks if the backup finished fine
  136.  
  137. #sendmail and exit function
  138. def sendMail(msgBody):
  139.     sender = 'root@' + HOSTNAME + '.ded.reflected.net'
  140.     receivers = ALERTMAIL
  141.     msgBody = msgBody
  142.  
  143.     #print ("this works")
  144.     #message = """From: """ + sender + """\nTo: """ + receivers + """\nSubject: backup failed on """ + HOSTNAME + """\n\n""" + msgBody + """\n"""
  145.  
  146.     #its still ugly, but ugly in mutliple lines
  147.     message = ("From: " + sender + "\n"
  148.     "To:" + receivers + "\n"
  149.     "Subject: Backup failed on " + HOSTNAME + "\n\n"
  150.     + msgBody + "\n")
  151.  
  152.     #smtpObj = smtplib.SMTP('smtp-out.reflected.net')
  153.     smtpObj.sendmail(sender, receivers, message)
  154.     print ("Successfully sent email")
  155.     sys.exit()
  156.  
  157.  
  158. skipCheck()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top