Advertisement
Guest User

Untitled

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