Advertisement
Guest User

Untitled

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