I'm in the process of automating our mariadb backup jobs into a python program. However for some reason it appears that the --target-directory parameter, although being passed correctly and validated by printing, is ignored when running the actual command from os.system. code below:
import os
import datetime, time
import mysql.connector as mariadb
unix_socket = "/var/lib/mysql/mysql.sock"
currentdate = datetime.datetime.now()
bkp_path = time.strftime("/%Y/%m-%d/%H%M")
ro_status = ("show global variables like 'read_only%'")
dblist = ("show databases")
db = mariadb.connect(user='pytest', password='pytest', unix_socket=unix_socket)
cur = db.cursor()
cur.execute(ro_status)
result = cur.fetchall()
for r in result:
if "OFF" in r:
cur.execute(dblist)
dbs = cur.fetchall()
for d in dbs:
dbstr = ''.join(d)
bkp_path = "/backups/" + dbstr + time.strftime("/%Y/%m-%d/%H%M/")
bkp_cmd = "sudo mariabackup --backup --databases='" + dbstr + "' --target-directory=" + bkp_path +" --user pytest --password=pytest --no-lock"
try:
os.stat(bkp_path)
except:
os.makedirs(bkp_path)
try:
# print(bkp_cmd)
os.system(bkp_cmd)
except:
print("Problem running backup on this host")
else:
print(h + " is read only and will not be backed up")
Example of the printed command: sudo mariabackup --backup --databases="testdbBA" --target-directory=/backups/testdbBA/2019/03-06/1659/ --user pytest --password=pytest --no-lock
even when run seperatly via the printed block - it attempts to write to my local home directory and not to the specified target dir.