Advertisement
Guest User

Untitled

a guest
Aug 14th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.90 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import sys,optparse
  3. import os
  4. import tarfile
  5. import commands
  6. import tempfile
  7. import shutil
  8. from ftplib import FTP
  9.  
  10. def convert_bytes(bytes):
  11.     bytes = float(bytes)
  12.     if bytes >= 1099511627776:
  13.         terabytes = bytes / 1099511627776
  14.         size = '%.2fT' % terabytes
  15.     elif bytes >= 1073741824:
  16.         gigabytes = bytes / 1073741824
  17.         size = '%.2fG' % gigabytes
  18.     elif bytes >= 1048576:
  19.         megabytes = bytes / 1048576
  20.         size = '%.2fM' % megabytes
  21.     elif bytes >= 1024:
  22.         kilobytes = bytes / 1024
  23.         size = '%.2fK' % kilobytes
  24.     else:
  25.         size = '%.2fb' % bytes
  26.     return size
  27.    
  28.    
  29. def backup_no_ftp(location):
  30.     print('*******************************************************')
  31.     print('*********************backing up************************')
  32.     print('*******************************************************')
  33.     blacklist=['backup']
  34.     homedir=os.getenv('HOME')
  35.     minecraftdir=homedir+'/.minecraft/saves/'
  36.     contents = os.listdir(minecraftdir)
  37.     backupdir=minecraftdir+'backup'
  38.    
  39.     if (location != None):
  40.         backupdir=location
  41.         if (not os.path.exists(location)):
  42.             try:
  43.                 os.mkdir(location)
  44.                 print('Created directory '+location)
  45.             except:
  46.                 pass
  47.         else:
  48.             try:
  49.                 os.mkdir(minecraftdir+'backup')
  50.             except:
  51.                 pass
  52.                
  53.     for item in contents:
  54.         if item in blacklist:
  55.             continue
  56.        
  57.         print('Backing up '+item+' to '+backupdir+'/'+item+'.tar.gz...')
  58.         tar = tarfile.open(backupdir+'/'+item+'.tar.gz', "w:gz")
  59.         tar.add(minecraftdir+'/'+item+'/')
  60.         tar.close()
  61.        
  62.  
  63. def backup_ftp(ftp_opts):
  64.     backup_no_ftp(tempfile.gettempdir()+'/MCBackItUp')
  65.     print('*******************************************************')
  66.     print('*********************uploading*************************')
  67.     print('*******************************************************')
  68.     user=ftp_opts.split(':')[0]
  69.     password=ftp_opts.split(':')[1].split('@')[0]
  70.     host=ftp_opts.split('@')[1].split(':')[0]
  71.     path=ftp_opts.split('@')[1].split(':')[1]
  72.     ftp=FTP(host, user, password)
  73.     contents = os.listdir(tempfile.gettempdir()+'/MCBackItUp')
  74.     for file in contents:
  75.         filesize=convert_bytes(os.path.getsize(tempfile.gettempdir()+'/MCBackItUp/'+file))
  76.         print('Uploading '+file+'('+filesize+') to '+user+'@'+host+' at ~'+path+''+file+'...')
  77.         f=open(tempfile.gettempdir()+'/MCBackItUp/'+file, 'rb')
  78.         ftp.storbinary('STOR '+file, f, 512)
  79.     ftp.quit()
  80.     shutil.rmtree(tempfile.gettempdir()+'/MCBackItUp')
  81.  
  82.  
  83. def backup(location, ftp_opts, override):
  84.     if ((override != True) and (commands.getoutput('ps aux | grep "java.*minecraft" | grep -v "grep"') != '')):
  85.         print('Minecraft is open, please close it and try again')
  86.         print('If you want to take the risky road and override this, use mcbackitup.py -o')
  87.         sys.exit(0)
  88.     elif (override == True):
  89.         if ftp_opts == None:
  90.             backup_no_ftp(location)
  91.         else:
  92.             backup_ftp(ftp_opts)
  93.     else:
  94.         if ftp_opts == None:
  95.             backup_no_ftp(location)
  96.         else:
  97.             backup_ftp(ftp_opts)
  98.  
  99. def main(argv):
  100.     cmdparser=optparse.OptionParser()
  101.     cmdparser.add_option("-o", "--override", action="store_true", dest="override", help="Overrides the need to have minecraft closed before saving, this could cause save games to become corrupt!")
  102.     cmdparser.add_option("-f", "--ftp", action="store", dest="ftp_opts", help="This allows save games to be uploaded to and ftp server, the format is: user:pass@host:remoteSavePath.")
  103.     cmdparser.add_option("-l", "--location", action="store", dest="location", help="The location to save the backup at. This defualts to $HOME/.minecraft/saves/backup")
  104.     (options,args)=cmdparser.parse_args()
  105.    
  106.     #print all options for debugging
  107.     #for item in (options,args):
  108.         #print(item)
  109.    
  110.     backup(options.location, options.ftp_opts, options.override)
  111.     return 1
  112.  
  113. if (__name__ == "__main__"):
  114.     #we call the main function passing a list of args, and exit with the return code passed back.
  115.     sys.exit(main(sys.argv))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement