Advertisement
Guest User

Untitled

a guest
May 1st, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  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.  
  62.  
  63. #set backupname
  64. createDate = datetime.today().strftime("%Y%m%d-%H%M%S")
  65. if STREAMING:
  66. createName = "mysql_backups_" + HOSTNAME + "_" + createDate + "-streaming"
  67. createNameCompressed = "mysql_backups_" + HOSTNAME + "_" + createDate + "-streaming" + backupSuffix
  68. else:
  69. createName = "mysql_backups_" + HOSTNAME + "_" + createDate
  70. createNameCompressed = "mysql_backups_" + HOSTNAME + "_" + createDate + backupSuffix
  71.  
  72. #skip disk check if .skipcheck is present in backupdir, remove after the run
  73. def skipCheck():
  74. if os.path.isfile(BACKUPDIR + ".skipcheck"):
  75. now = datetime.today().strftime("%Y-%m-%d %H:%M:%S")
  76. print(now + ": Skipping prerun disk space checks and running backups. skipcheck file will be removed")
  77. #function that runs backups
  78. os.remove(BACKUPDIR + ".skipcheck")
  79. else:
  80. isRunnable()
  81.  
  82. #check total freeDisk disk space on partition containting BACKUPDIR, in bytes
  83. def freeSpace(BACKUPDIR):
  84. st = os.statvfs(BACKUPDIR)
  85. freeDisk = int(st.f_bavail * st.f_frsize)
  86. return freeDisk
  87.  
  88. #get the size of the backup specified
  89. def listBackups(BACKUPDIR, backupSuffix):
  90. backupNameRegex = os.path.join(BACKUPDIR + 'mysql_backups_' + HOSTNAME + '_' + '*' + backupSuffix)
  91. listBackups = glob.glob(backupNameRegex)
  92. return listBackups
  93.  
  94. def lastBackupSize():
  95. backupNames = listBackups(BACKUPDIR, backupSuffix)
  96. print (backupNames)
  97. lastSize = os.path.getsize(max(backupNames , key = os.path.getctime))
  98. return lastSize
  99.  
  100. #get size of the mysql datadir
  101. def dirSize(path):
  102. total_size = 0
  103. for dirpath, dirnames, filenames in os.walk(path):
  104. for f in filenames:
  105. fp = os.path.join(dirpath, f)
  106. total_size += os.path.getsize(fp)
  107. return total_size
  108.  
  109. #prerun checks
  110. def isRunnable():
  111. oldAge = 1
  112. lastSize = lastBackupSize()
  113. freeDisk = freeSpace(BACKUPDIR)
  114. #if streaming backup, we dont care about size of the datadir
  115. if STREAMING:
  116. datadirSize = 0
  117. else:
  118. datadirSize = dirSize(DATADIR)
  119. print("Size of last backup")
  120. print(lastSize)
  121. print("Free disk size")
  122. print(freeDisk)
  123. print("Datadir size")
  124. print(datadirSize)
  125. if ((freeDisk - lastSize - datadirSize) * 1.05) < (lastSize + datadirSize):
  126. msgBody = "Cant run backups as there is not enough free disk space"
  127. sendMail(msgBody)
  128. print("All gucci")
  129. runBackup()
  130.  
  131. def runBackup():
  132. #how to split it
  133. #https://docs.python.org/2/library/subprocess.html
  134. #
  135. print("runBackup")
  136. if not STREAMING:
  137. print(INNOBACKUPEX + " " + CUSTOMOPT + " --slave-info " + BACKUPDIR)
  138. else:
  139. print("stuff")
  140. #$innobackupex $innobackupex_backup_options --stream=tar $WORKDIR 2> $lockdir/innobackupex-run.log | $COMPRESS $COMPRESS_ARGS -c > $BACKUPDIR/$FILEPREFIX.$FILESUFFIX
  141. #bashCommand = ['ls', '-lah', '/home/backups']
  142. #subprocess.run(bashCommand)
  143. #when the innobackupex compresses the data to workdir add apply log.
  144. #if streaming, no apply log!
  145. #when this is done, compress the directory using COMPRESSION
  146. #add checks if the backup finished fine
  147.  
  148. #sendmail and exit function
  149. def sendMail(msgBody):
  150. sender = 'root@' + HOSTNAME + '.ded.reflected.net'
  151. receivers = ALERTMAIL
  152. msgBody = msgBody
  153.  
  154. #print ("this works")
  155. #message = """From: """ + sender + """\nTo: """ + receivers + """\nSubject: backup failed on """ + HOSTNAME + """\n\n""" + msgBody + """\n"""
  156.  
  157. #its still ugly, but ugly in mutliple lines
  158. message = ("From: " + sender + "\n"
  159. "To:" + receivers + "\n"
  160. "Subject: Backup failed on " + HOSTNAME + "\n\n"
  161. + msgBody + "\n")
  162.  
  163. #smtpObj = smtplib.SMTP('smtp-out.reflected.net')
  164. smtpObj.sendmail(sender, receivers, message)
  165. print ("Successfully sent email")
  166. sys.exit()
  167.  
  168.  
  169. skipCheck()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement