Getting text output from shell command to log?

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?

Consider using subprocess.Popen() instead.

Try using either check_call or check_output instead of just call.

Those two functions will raise a CalledProcessError if the return code is non-zero

I was trying that before and kept getting this error

Both folder locations referenced in my dump command, the MySQL and the Users dump folder, both exist, I copied and pasted them from my dump command into a file explorer and was able to see them fine.

I got this error with trying

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
p = subprocess.Popen(dumpcommand, stdout=subprocess.PIPE)
result = p.communicate()[0]
print result

but it doesn’t get to the print result.

Heres the full dump command -
cd C:\Program Files\MySQL\MySQL Server 5.6\bin & .\mysqldump --user=root --password=youwish --host=localhost --port=3307 --all-databases > C:\Users\bkarabinchak\Documents\dumps\01122021\PSM4_131547.sql

Edit: Fixed this. Needed a shell=True parameter.

1 Like