Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import sys,optparse
- import os
- import tarfile
- import commands
- import tempfile
- import shutil
- from ftplib import FTP
- def convert_bytes(bytes):
- bytes = float(bytes)
- if bytes >= 1099511627776:
- terabytes = bytes / 1099511627776
- size = '%.2fT' % terabytes
- elif bytes >= 1073741824:
- gigabytes = bytes / 1073741824
- size = '%.2fG' % gigabytes
- elif bytes >= 1048576:
- megabytes = bytes / 1048576
- size = '%.2fM' % megabytes
- elif bytes >= 1024:
- kilobytes = bytes / 1024
- size = '%.2fK' % kilobytes
- else:
- size = '%.2fb' % bytes
- return size
- def backup_no_ftp(location):
- print('*******************************************************')
- print('*********************backing up************************')
- print('*******************************************************')
- blacklist=['backup']
- homedir=os.getenv('HOME')
- minecraftdir=homedir+'/.minecraft/saves/'
- contents = os.listdir(minecraftdir)
- backupdir=minecraftdir+'backup'
- if (location != None):
- backupdir=location
- if (not os.path.exists(location)):
- try:
- os.mkdir(location)
- print('Created directory '+location)
- except:
- pass
- else:
- try:
- os.mkdir(minecraftdir+'backup')
- except:
- pass
- for item in contents:
- if item in blacklist:
- continue
- print('Backing up '+item+' to '+backupdir+'/'+item+'.tar.gz...')
- tar = tarfile.open(backupdir+'/'+item+'.tar.gz', "w:gz")
- tar.add(minecraftdir+'/'+item+'/')
- tar.close()
- def backup_ftp(ftp_opts):
- backup_no_ftp(tempfile.gettempdir()+'/MCBackItUp')
- print('*******************************************************')
- print('*********************uploading*************************')
- print('*******************************************************')
- user=ftp_opts.split(':')[0]
- password=ftp_opts.split(':')[1].split('@')[0]
- host=ftp_opts.split('@')[1].split(':')[0]
- path=ftp_opts.split('@')[1].split(':')[1]
- ftp=FTP(host, user, password)
- contents = os.listdir(tempfile.gettempdir()+'/MCBackItUp')
- for file in contents:
- filesize=convert_bytes(os.path.getsize(tempfile.gettempdir()+'/MCBackItUp/'+file))
- print('Uploading '+file+'('+filesize+') to '+user+'@'+host+' at ~'+path+''+file+'...')
- f=open(tempfile.gettempdir()+'/MCBackItUp/'+file, 'rb')
- ftp.storbinary('STOR '+file, f, 512)
- ftp.quit()
- shutil.rmtree(tempfile.gettempdir()+'/MCBackItUp')
- def backup(location, ftp_opts, override):
- if ((override != True) and (commands.getoutput('ps aux | grep "java.*minecraft" | grep -v "grep"') != '')):
- print('Minecraft is open, please close it and try again')
- print('If you want to take the risky road and override this, use mcbackitup.py -o')
- sys.exit(0)
- elif (override == True):
- if ftp_opts == None:
- backup_no_ftp(location)
- else:
- backup_ftp(ftp_opts)
- else:
- if ftp_opts == None:
- backup_no_ftp(location)
- else:
- backup_ftp(ftp_opts)
- def main(argv):
- cmdparser=optparse.OptionParser()
- 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!")
- 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.")
- cmdparser.add_option("-l", "--location", action="store", dest="location", help="The location to save the backup at. This defualts to $HOME/.minecraft/saves/backup")
- (options,args)=cmdparser.parse_args()
- #print all options for debugging
- #for item in (options,args):
- #print(item)
- backup(options.location, options.ftp_opts, options.override)
- return 1
- if (__name__ == "__main__"):
- #we call the main function passing a list of args, and exit with the return code passed back.
- sys.exit(main(sys.argv))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement