我正在将mariadb备份作业自动化为python程序。但是,由于某些原因,看来似乎--target-directory参数虽然正确传递并通过打印进行了验证,但在从os.system运行实际命令时却被忽略了。下面的代码:
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")
打印命令示例: sudo mariabackup --backup --databases =“ testdbBA” --target-directory = / backups / testdbBA / 2019 / 03-06 / 1659 / --user pytest --password = pytest --no-lock
即使通过打印块单独运行-它也会尝试写入我的本地主目录而不是指定的目标目录。
答案 0 :(得分:0)
它的--target-dir不是--target-directory
感谢MFisherKDX