Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os, time, sys, zipfile, datetime,calendar
- from os import sep
- import shutil
- # Script Parameters
- # Target Directory for final Zip files
- target_dir = r'target'
- copy_dir = r'copy'
- # The name of the sql file (this will be the same for all backups)
- file_name = 'databases.sql'
- # Authentication
- user = '-'
- password = '-'
- # The command for the mysql dump. This could include the entire path to the executable if mysqldump is not on the path environment variable
- dumpcmd = r'mysqldump'
- # The host you want to connect to
- host = 'localhost'
- args = [dumpcmd, '-C', '-f', '--host=%s' %host, '--user=%s' %user, '--password=%s' %password, '--result-file=%s' %file_name, '--all-databases']
- os.chdir(target_dir)
- cmd = ' '.join(args)
- print 'Running Command: %s' %cmd.replace(password, '*'*len(password))
- os.system(cmd)
- # Below is for zipping the files and creating a redundant backup
- def getZipFileName():
- return '{name}.zip'.format(name=str(datetime.datetime.now()))
- # Write the zip file
- zip_name = 'export-' + str(datetime.date.today()) + '-' + getZipFileName()
- zip_file = os.path.abspath(zip_name)
- zip = zipfile.ZipFile(zip_file, 'w', zipfile.ZIP_DEFLATED)
- zip.write(file_name)
- zip.close()
- # Remove uncompressed sql file
- os.remove(file_name)
- to_path = os.path.join(copy_dir, zip_name)
- to_dir = sep.join(to_path.split(sep)[:-1])
- if not os.access(to_dir, os.F_OK):
- os.makedirs(to_dir)
- if os.access(to_path, os.F_OK):
- os.remove(to_path)
- shutil.copy(zip_file, to_path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement