I am trying to write a python script that backs up our production database, and logs the result of the backup success/error. I am working on it in the script console. What I have so far is -
import os
import time
import subprocess
import system.util
logger = system.util.getLogger("Database Backup")
# MySQL database details to which backup to be done. Make sure below user having enough privileges to take databases backup.
# To take multiple databases backup, create any file like /backup/dbnames.txt and put databses names one on each line and assignd to DB_NAME variable.
DB_HOST = 'localhost'
DB_PORT = '3306'
DB_USER = 'root'
DB_USER_PASSWORD = 'youwish'
DB_NAME = 'tab'
BACKUP_PATH = 'C:\\Users\\someuser\\Documents\\dumps\\'
MysqldumpPath = 'c:\\Program Files\\MySQL\\MySQL Server 5.6\\bin'
# Getting current datetime to create seprate backup folder like "12012013".
DATETIME = time.strftime('%m%d%Y')
TODAYBACKUPPATH = BACKUP_PATH + DATETIME
logger.info("Todays backup path: " + str(TODAYBACKUPPATH))
FILENAME = "PSM4_"+time.strftime("%H%M%S")+".sql"
# Checking if backup folder already exists or not. If not exists will create it.
if not os.path.exists(TODAYBACKUPPATH):
logger.info("Creating backup folder")
os.makedirs(TODAYBACKUPPATH)
FILEPATH = TODAYBACKUPPATH + FILENAME
dumpcommand = "cd C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin & .\\mysqldump --user=%s --password=%s --host=%s --port=%s --all-databases > %s"%(DB_USER,DB_USER_PASSWORD,DB_HOST,DB_PORT,FILEPATH)
print dumpcommand
subprocess.call(dumpcommand, shell=True)
logger.info("Backup script completed. Your backups has been created in '" +TODAYBACKUPPATH + '\\' + db + '.sql'+" directory")
For some reason my subprocess.call is not working. I realize I can’t even trouble shoot this as I cannot see what the shell is spitting back out. I would like it if I could get hte output of this shell command so that I could eventually log success or error if one occurs. How can I make that happen with this subprocess library?