Advertisement
Guest User

Untitled

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