Mysql, Maria DB

mariadb remote backup python script using mysqldump

Pydole 2020. 5. 11. 13:04

 

import pymysql
from os import system
from datetime import datetime

host = ' '
dbuser = ' '
password = ' '
dbname = ' '


# get tables name

conn = pymysql.connect(host=host, user=dbuser, password=password,db=dbname)
c = conn.cursor()
c.execute('SHOW TABLES')
tablelist = [ x[0] for x in c.fetchall() ]
c.close()
conn.close()




# mysqldump.exe backup (prefix_date)

prefix_date = str(datetime.today().date())
program = 'mysqldump.exe'

for table in tablelist:
    command = '%s -h %s --user=%s --password=%s --tables %s %s > %s_%s_backup.sql' %(program,host,dbuser,password,dbname,table,prefix_date,table)
    system(command)