Sunday, January 25, 2015

python mysqlbackup scirpt 2

root@vm1:~/pyscript# cat back.py.back
#!/usr/bin/python
db_name_file = '/root/pyscript/dbname.txt'
backup_dest = '/root/pyscript/sqlbackup/'
import os
import time
import datetime
dbhost = 'localhost'
dbuser = 'root'
dbpass = 'redhat'
datetime = time.strftime("%Y-%m-%d_%H-%M-%S")
backuppathwithDT = backup_dest + datetime
#create directory if it does not exist
if not os.path.exists(backuppathwithDT):
    os.makedirs(backuppathwithDT, 0500)
print "Checking for db names in file"
if os.path.exists(db_name_file):
    file1 = open(db_name_file)
    multi = 1
    print "Database names found"
    print "starting backup of databases.. "
else:
    print "Database file not found in file"
#Starting backup
if multi:
        dumpcmd = "mysqldump -u " + dbuser + " -p" + dbpass + " --all-databases --events " + " > " + backuppathwithDT + "/completedb.sql"
        os.system(dumpcmd)
 
else:
    print "no database to dump"
print "Exiting..."

No comments:

Post a Comment