Skip to content

Instantly share code, notes, and snippets.

@fbidu
Created October 16, 2015 17:28
Show Gist options
  • Save fbidu/0a2f218492f90593fe11 to your computer and use it in GitHub Desktop.
Save fbidu/0a2f218492f90593fe11 to your computer and use it in GitHub Desktop.
# coding: latin-1
from datetime import datetime
import commands
# Funcao que gera um backup compactado em BZ2 de um banco de dados MySQL
def backupMySQL(host, database, user, password, path, nome):
sufixo = "_" + datetime.now().strftime('%d_%B_%Y')
cmd = "mysqldump --add-drop-table -u " + user + " --password=" + password + " " + database + " > " + path+nome+sufixo+".sql"
dump = commands.getoutput(cmd)
if dump != '':
raise Exception('Erro na execução do backup: '+dump)
cmd = "bzip2 "+path+nome+sufixo+".sql"
compac = commands.getoutput(cmd)
if compac != '':
raise Exception('Erro na compactação do backup: '+compac)
return ['Backup realizado com sucesso', path+nome+sufixo+".sql.bz2"]
def backupMySQLTotal(host, user, password, path, nome):
sufixo = "_" + datetime.now().strftime('%d_%B_%Y_%Hh%Mmin')
filename = path+nome+sufixo+'.sql'
cmd = ("mysqldump --all-databases -u %s --password=%s > %s" % (user, password, filename))
dump = commands.getoutput(cmd)
if dump != '':
raise Exception('Erro na execução de backup geral: %s com o comando: %s' % (dump, cmd))
cmd = ("bzip2 %s" % (filename))
compac = commands.getoutput(cmd)
if compac != '':
raise Exception('Erro na compactação do backup: %s' % (compac))
return ['Backup realizado com sucesso', filename+".bz2"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment