Advertisement
anjarwebid

simplefullbackup v1

Feb 3rd, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.05 KB | None | 0 0
  1. """
  2. Name : simplefullbackup v1
  3.  
  4. Author : Anjar Fiandrianto / @anjarwebid
  5.  
  6. Version : 1.0
  7.  
  8. Date : 11-12-2012
  9.  
  10. Full backup for file and database, run by cron.
  11. """
  12.  
  13. import sys, os, time, ConfigParser, tarfile, ftplib
  14.  
  15. #global variable here
  16. folder = ""
  17. server = ""
  18. usr = ""
  19. passwd = ""
  20. path = ""
  21. ftphost = ""
  22. ftpuser = ""
  23. ftppassword = ""
  24. ftpport = ""
  25. ftpdirectory = ""
  26. appname = "fullbackup.py"
  27.  
  28. def chkcfg():
  29.     global folder
  30.     global server
  31.     global usr
  32.     global passwd
  33.     global path
  34.     global ftphost
  35.     global ftpuser
  36.     global ftppassword
  37.     global ftpport
  38.     global ftpdirectory
  39.     if not os.path.isfile(os.path.abspath(__file__).strip(appname)+"fullbackup-config.cfg"):
  40.         #create config file
  41.         writecfg = ConfigParser.RawConfigParser()
  42.         folder = raw_input("[DB]Enter your folder address (without ""/"" or slash on the end):")
  43.         server = raw_input("[DB]Server :")
  44.         usr = raw_input("[DB]MySQL User :")
  45.         passwd = raw_input("[DB]MySQL Password :")
  46.         path = raw_input("[FILE]Target folder backup :")
  47.         ftphost = raw_input("[FTP]Host :")
  48.         ftpuser = raw_input("[FTP]User FTP :")
  49.         ftppassword = raw_input("[FTP]Password FTP :")
  50.         ftpport = raw_input("[FTP]Port FTP :")
  51.         ftpdirectory = raw_input("[FTP]Directory FTP :")
  52.         writecfg.add_section('basic')
  53.         writecfg.set('basic','folder', folder)
  54.         writecfg.set('basic','server', server)
  55.         writecfg.set('basic','user', usr)
  56.         writecfg.set('basic','password', passwd)
  57.         writecfg.set('basic','path', path)
  58.         writecfg.set('basic','ftphost', ftphost)
  59.         writecfg.set('basic','ftpuser', ftpuser)
  60.         writecfg.set('basic','ftppassword', ftppassword)
  61.         writecfg.set('basic','ftpport', ftpport)
  62.         writecfg.set('basic','ftpdirectory', ftpdirectory)
  63.         with open(os.path.abspath(__file__).strip(appname)+'fullbackup-config.cfg', 'wb') as configfile:
  64.             writecfg.write(configfile)
  65.         backupdb()
  66.     else:
  67.         #if config file exist, just read config file
  68.         readcfg = ConfigParser.ConfigParser()
  69.         readcfg.read(os.path.abspath(__file__).strip(appname)+'fullbackup-config.cfg')
  70.         folder = readcfg.get('basic', 'folder')
  71.         server = readcfg.get('basic', 'server')
  72.         usr = readcfg.get('basic', 'user')
  73.         passwd = readcfg.get('basic', 'password')
  74.         path = readcfg.get('basic','path')
  75.         ftphost = readcfg.get('basic','ftphost')
  76.         ftpuser = readcfg.get('basic','ftpuser')
  77.         ftppassword = readcfg.get('basic','ftppassword')
  78.         ftpport = readcfg.get('basic','ftpport')
  79.         ftpdirectory = readcfg.get('basic','ftpdirectory')
  80.         backupdb()
  81.  
  82. def backupdb():
  83.     #check and create log
  84.     f = open("/var/log/fullbackup", "w")
  85.     filestamplog = time.strftime('%Y-%m-%d %H:%M')
  86.     f.write ("backup on "+filestamplog+"\n")
  87.     # get database list
  88.     filestamp = time.strftime('%Y-%m-%d')
  89.     database_list_command="mysql -u %s -p%s -h %s --silent -N -e 'show databases'" % (usr, passwd, server)
  90.     #split database list
  91.     for database in os.popen(database_list_command).readlines():
  92.         database = database.strip()
  93.         #skip "information_scema" database backup
  94.         if database == 'information_schema':
  95.             continue
  96.         #set folder for database backup files
  97.         newpath = "%s/%s/database" % (folder,filestamp)
  98.         if not os.path.exists(newpath):
  99.             os.makedirs(newpath)
  100.         filename = "%s/%s-%s.sql" % (newpath, database, filestamp)
  101.         #generate backup file
  102.         os.popen("mysqldump -u %s -p%s -h %s -e --opt -c %s | gzip -c > %s.gz" % (usr, passwd, server, database, filename))
  103.         f.write ("Backup database "+database+" finished\n")
  104.     f.write ("Database Backup finished\n")
  105.     f.close
  106.     backupfile()
  107.  
  108. def backupfile():
  109.     f = open("/var/log/fullbackup", "w")
  110.     filestamp = time.strftime('%Y-%m-%d')
  111.     filestamplog = time.strftime('%Y-%m-%d %H:%M')
  112.     folderdata = os.listdir(path)
  113.     for foldername in folderdata:
  114.         full_dir = os.path.join(path, foldername)
  115.         newpath = "%s/%s/file" % (folder,filestamp)
  116.         if not os.path.exists(newpath):
  117.             os.makedirs(newpath)
  118.         tar = tarfile.open(os.path.join(newpath, foldername+'.tar.gz'), 'w:gz')
  119.         tar.add(full_dir)
  120.         tar.close()
  121.         f.write ("Backup folder "+foldername+" finished\n")
  122.         print foldername
  123.     f.write ("All finished\n")
  124.     f.close
  125.     uploadbackup()
  126.  
  127. def uploadbackup():
  128.     filestamp = time.strftime('%Y-%m-%d')
  129.     tar = tarfile.open(os.path.join(folder, filestamp+'.tar.gz'), 'w:gz')
  130.     tar.add(os.path.join(folder, filestamp))
  131.     tar.close()
  132.     ftp = ftplib.FTP()
  133.     ftp.connect(ftphost, ftpport)
  134.     ftp.login(ftpuser, ftppassword)
  135.     ftp.cwd(ftpdirectory)
  136.     fp = open(os.path.abspath(__file__).strip(appname)+filestamp+'.tar.gz','rb')
  137.     ftp.storbinary('STOR '+filestamp+'.tar.gz', fp)
  138.     fp.close()
  139.     ftp.quit()
  140.    
  141.  
  142. if __name__ == "__main__":
  143.     chkcfg()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement