Advertisement
Guest User

Untitled

a guest
Apr 27th, 2018
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 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. #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 listBackups(BACKUPDIR, backupSuffix):
  81. backupNameRegex = os.path.join(BACKUPDIR + 'mysql_backups_' + HOSTNAME + '_' + '*' + backupSuffix)
  82. listBackups = glob.glob(backupNameRegex)
  83. return listBackups
  84.  
  85. def lastBackupSize():
  86. backupNames = listBackups(BACKUPDIR, backupSuffix)
  87. print (backupNames)
  88. lastSize = os.path.getsize(max(backupNames , key = os.path.getctime))
  89. return lastSize
  90.  
  91. #get size of the mysql datadir
  92. def dirSize(path):
  93. total_size = 0
  94. for dirpath, dirnames, filenames in os.walk(path):
  95. for f in filenames:
  96. fp = os.path.join(dirpath, f)
  97. total_size += os.path.getsize(fp)
  98. return total_size
  99.  
  100. #prerun checks
  101. def isRunnable():
  102. oldAge = 1
  103. lastSize = lastBackupSize()
  104. freeDisk = freeSpace(BACKUPDIR)
  105. #if streaming backup, we dont care about size of the datadir
  106. if STREAMING:
  107. datadirSize = 0
  108. else:
  109. datadirSize = dirSize(DATADIR)
  110. print("Size of last backup")
  111. print(lastSize)
  112. print("Free disk size")
  113. print(freeDisk)
  114. print("Datadir size")
  115. print(datadirSize)
  116. if ((freeDisk - lastSize - datadirSize) * 1.05) < (lastSize + datadirSize):
  117. msgBody = "Cant run backups as there is not enough free disk space"
  118. sendMail(msgBody)
  119. print("All gucci")
  120. runBackup()
  121.  
  122. def runBackup():
  123. #how to split it
  124. #https://docs.python.org/2/library/subprocess.html
  125. #
  126. print("runBackup")
  127. if not STREAMING:
  128. print(INNOBACKUPEX + " " + CUSTOMOPT + " --slave-info " + BACKUPDIR)
  129. else:
  130. print("stuff")
  131. #$innobackupex $innobackupex_backup_options --stream=tar $WORKDIR 2> $lockdir/innobackupex-run.log | $COMPRESS $COMPRESS_ARGS -c > $BACKUPDIR/$FILEPREFIX.$FILESUFFIX
  132. #bashCommand = ['ls', '-lah', '/home/backups']
  133. #subprocess.run(bashCommand)
  134. #when the innobackupex compresses the data to workdir add apply log.
  135. #if streaming, no apply log!
  136. #when this is done, compress the directory using COMPRESSION
  137. #add checks if the backup finished fine
  138.  
  139. #sendmail and exit function
  140. def sendMail(msgBody):
  141. sender = 'root@' + HOSTNAME + '.ded.reflected.net'
  142. receivers = ALERTMAIL
  143. msgBody = msgBody
  144.  
  145. #print ("this works")
  146. #message = """From: """ + sender + """\nTo: """ + receivers + """\nSubject: backup failed on """ + HOSTNAME + """\n\n""" + msgBody + """\n"""
  147.  
  148. #its still ugly, but ugly in mutliple lines
  149. message = ("From: " + sender + "\n"
  150. "To:" + receivers + "\n"
  151. "Subject: Backup failed on " + HOSTNAME + "\n\n"
  152. + msgBody + "\n")
  153.  
  154. #smtpObj = smtplib.SMTP('smtp-out.reflected.net')
  155. smtpObj.sendmail(sender, receivers, message)
  156. print ("Successfully sent email")
  157. sys.exit()
  158.  
  159.  
  160. skipCheck()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement