Want more features on Pastebin? Sign Up, it's FREE!

backup script

By: theman55 on Mar 7th, 2013  |  syntax: Python  |  size: 4.13 KB  |  views: 39  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/python
  2. import os, sys, shutil, filecmp, time
  3.  
  4. #convert the standard time to pacific local time, not important but makes it easier for me to test
  5. def getPacificTime():
  6.         timeStruct = time.gmtime()
  7.         hour = None
  8.         if timeStruct[3] < 8:
  9.                 hour = 24-(8-timeStruct[3])
  10.         else:
  11.                 hour = timeStruct[3]-8
  12.         timeTuple = hour, timeStruct[4], timeStruct[5]
  13.         return timeTuple
  14.  
  15.  
  16. def backup_files(folderPath, destPath="/usr/backups"):
  17.         MAXBACKUPS = 20
  18.         #if the folderPath is a file, just copy it over. no need for fancy stuff
  19.         if os.path.isfile(folderPath):
  20.                 shutil.copy2(folderPath, destPath)
  21.                 return true
  22.         fufilled = False
  23.         #if folderPath is written with a "\" at the end, remove it
  24.         folderPath = folderPath.rstrip(os.sep)
  25.         #acquire the destFolder, the function takes the folder name of folderPath and creates a folder in the destPath with a number attached to it
  26.         for i in range(MAXBACKUPS):
  27.                 destPathTest = os.path.join(destPath,(os.path.basename(folderPath) + str(i)))
  28.                 if not os.path.exists(destPathTest):
  29.                         fufilled = True
  30.                         destPath = destPathTest
  31.                         break
  32.         #if there are MAXBACKUPS number of folders than it cycles through again until it gets to the folder that is the oldest, it then deletes the folder contents and makes the destPath the empty folder
  33.         if not fufilled:
  34.                 for i in range(MAXBACKUPS):                    
  35.                         destPathTest = os.path.join(destPath,(os.path.basename(folderPath) + str(i)))
  36.                         fmTime = os.path.getmtime(destPathTest)
  37.                         destPathTest = os.path.join(destPath,(os.path.basename(folderPath) + str(i+1)))
  38.                         if not os.path.exists(destPathTest):                           
  39.                                 destPathTest = os.path.join(destPath,(os.path.basename(folderPath) + str(0)))
  40.                         ldTime = os.path.getmtime(destPathTest)
  41.                         if ldTime < fmTime:
  42.                                 destPath = destPathTest
  43.                                 shutil.rmtree(destPath)
  44.                                 break
  45.                        
  46.         os.mkdir(destPath)
  47.         #walks through the entire directory tree of the folderPath and copies it over to destPath
  48.         for root,dirs,files in os.walk(folderPath):
  49.                 curDestPath = os.path.join(destPath, root[len(folderPath)+1:])
  50.                 if not os.path.exists(curDestPath):
  51.                         os.mkdir(curDestPath)
  52.                 for s in dirs:
  53.                         os.mkdir(os.path.join(curDestPath, s))
  54.                 for s in files:
  55.                         tempSrc = os.path.join(root, s)
  56.                         tempDest = os.path.join(curDestPath, s)
  57.                         if os.path.exists(tempSrc):
  58.                                 shutil.copy2(tempSrc, tempDest)
  59.                         else:
  60.                                 msg = "error: " + tempSrc + " doesnt exist"
  61.                                 print msg
  62.  
  63.                
  64. SERVERPATH ="/home/theman515/SigmaServer/world"
  65. SCREEN = "625.minecraft"
  66. #main loop checks to see if the time to backup the world file is now and to see if the server crashed (we were running plus+ modpack, it happened a lot)
  67. #this assumes you're running your minecraft server through a gnu screen using a launch script named "launch" without the quotes
  68. while(1):
  69.         timeStruct = getPacificTime()
  70.         if (timeStruct[0] == 4 and timeStruct[1] == 0 and timeStruct[2] == 0) or (timeStruct[0] == 16 and timeStruct[1] == 0 and timeStruct[2] == 0):
  71.                 print "alerting the server, 60 seconds"
  72.                 curTime = time.time()
  73.                 os.system("screen -S " + SCREEN + " -p 0 -X stuff \"/say server is shutting down in 60 seconds\015\"")
  74.                 while time.time()-curTime<=30:
  75.                         pass
  76.                 os.system("screen -S " + SCREEN + " -p 0 -X stuff \"/say server is shutting down in 30 seconds\015\"")
  77.                 while time.time()-curTime <= 50:
  78.                         pass
  79.                 os.system("screen -S " + SCREEN + " -p 0 -X stuff \"/say server is shutting down in 10 seconds\015\"")
  80.                 while time.time()-curTime <= 60:
  81.                         pass
  82.                 print "shutting down the server"
  83.                 os.system("screen -S " + SCREEN + " -p 0 -X stuff \"stop\015\"")
  84.                 print "backing up the server"
  85.                 backup_files(SERVERPATH)
  86.                 curTime = time.time()
  87.                 while time.time()-curTime < 10:
  88.                         pass
  89.                 print "relaunching the server"
  90.                 os.system("screen -S " + SCREEN + " -p 0 -X stuff \"./launch\015\"")
  91.                 print "done"
  92.         fil = os.popen("ps -e")
  93.         output = ""
  94.         #gets all the output from os.popen
  95.         while True:
  96.                 stri = fil.readline()
  97.                 output += stri
  98.                 if stri == "":
  99.                         break
  100.         if output.find("launch") == -1:
  101.                 msg = "server crashed, restarting"
  102.                 print msg
  103.                 os.system("screen -S " + SCREEN + " -p 0 -X stuff \"./launch\015\"")
  104.                 curTime = time.time()
  105.                 while time.time() -curTime < 1:
  106.                         pass
clone this paste RAW Paste Data