Advertisement
Guest User

Untitled

a guest
Nov 6th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. import os, time, sys, zipfile, datetime,calendar
  2. from os import sep
  3. import shutil
  4.  
  5. # Script Parameters
  6. # Target Directory for final Zip files
  7. target_dir = r'target'
  8. copy_dir = r'copy'
  9. # The name of the sql file (this will be the same for all backups)
  10. file_name = 'databases.sql'
  11. # Authentication
  12. user = '-'
  13. password = '-'
  14. # The command for the mysql dump. This could include the entire path to the executable if mysqldump is not on the path environment variable
  15. dumpcmd = r'mysqldump'
  16. # The host you want to connect to
  17. host = 'localhost'
  18.  
  19. args = [dumpcmd, '-C', '-f', '--host=%s' %host, '--user=%s' %user, '--password=%s' %password, '--result-file=%s' %file_name, '--all-databases']
  20.  
  21. os.chdir(target_dir)
  22. cmd = ' '.join(args)
  23. print 'Running Command: %s' %cmd.replace(password, '*'*len(password))
  24. os.system(cmd)
  25.  
  26. # Below is for zipping the files and creating a redundant backup
  27. def getZipFileName():
  28. return '{name}.zip'.format(name=str(datetime.datetime.now()))
  29.  
  30. # Write the zip file
  31. zip_name = 'export-' + str(datetime.date.today()) + '-' + getZipFileName()
  32. zip_file = os.path.abspath(zip_name)
  33. zip = zipfile.ZipFile(zip_file, 'w', zipfile.ZIP_DEFLATED)
  34. zip.write(file_name)
  35. zip.close()
  36.  
  37. # Remove uncompressed sql file
  38. os.remove(file_name)
  39. to_path = os.path.join(copy_dir, zip_name)
  40. to_dir = sep.join(to_path.split(sep)[:-1])
  41. if not os.access(to_dir, os.F_OK):
  42. os.makedirs(to_dir)
  43. if os.access(to_path, os.F_OK):
  44. os.remove(to_path)
  45. shutil.copy(zip_file, to_path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement