Skip to content

Commit 7de407b

Browse files
committed
added docker_mode for MYSQL_SSH (mysqldump)
1 parent 0ec141b commit 7de407b

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

rsnapshot_backup/rsnapshot_backup.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -925,15 +925,15 @@ def run_cmd_pipe(cmd):
925925
if item["source"] == "ALL":
926926
script_dump_part = textwrap.dedent(
927927
"""\
928-
mysql --defaults-file=/etc/mysql/debian.cnf --skip-column-names --batch -e "SHOW DATABASES;" | grep -v -e information_schema -e performance_schema {grep_db_filter} > {mysql_dump_dir}/db_list.txt
928+
{dump_run} 'mysql {dump_creds} --skip-column-names --batch -e "SHOW DATABASES;"' | grep -v -e information_schema -e performance_schema {grep_db_filter} > {mysql_dump_dir}/db_list.txt
929929
WAS_ERR=0
930930
for db in $(cat {mysql_dump_dir}/db_list.txt); do
931931
set +e
932932
if [[ ! -f {mysql_dump_dir}/$db.gz ]]; then
933933
{exec_before_dump}
934934
if [[ $? -ne 0 ]]; then WAS_ERR=1; fi
935935
for DUMP_ATTEMPT in $(seq 1 {dump_attempts}); do
936-
{dump_prefix_cmd} mysqldump --defaults-file=/etc/mysql/debian.cnf --force --opt --single-transaction --quick --skip-lock-tables {mysql_events} --databases $db --max_allowed_packet=1G {mysqldump_args} | gzip > {mysql_dump_dir}/$db.gz
936+
{dump_run} '{dump_prefix_cmd} mysqldump {dump_creds} --force --opt --single-transaction --quick --skip-lock-tables {mysql_events} --databases $1 --max_allowed_packet=1G {mysqldump_args}' -- $db | gzip > {mysql_dump_dir}/$db.gz
937937
if [[ $? -ne 0 ]]; then
938938
WAS_ERR=1
939939
echo "ERROR: Dump failed, attempt $DUMP_ATTEMPT of {dump_attempts}"
@@ -960,7 +960,9 @@ def run_cmd_pipe(cmd):
960960
exec_after_dump=item["exec_after_dump"],
961961
mysqldump_args=item["mysqldump_args"],
962962
grep_db_filter=grep_db_filter,
963-
dump_attempts=item["dump_attempts"]
963+
dump_attempts=item["dump_attempts"],
964+
dump_run="docker exec {container} sh -lc".format(container=item["docker_container"]) if item["docker_mode"] else "bash -c",
965+
dump_creds="-u\"$MYSQL_USER\" -p\"$MYSQL_PASSWORD\"" if item["docker_mode"] else "--defaults-file=/etc/mysql/debian.cnf"
964966
)
965967
else:
966968
script_dump_part = textwrap.dedent(
@@ -971,7 +973,7 @@ def run_cmd_pipe(cmd):
971973
{exec_before_dump}
972974
if [[ $? -ne 0 ]]; then WAS_ERR=1; fi
973975
for DUMP_ATTEMPT in $(seq 1 {dump_attempts}); do
974-
{dump_prefix_cmd} mysqldump --defaults-file=/etc/mysql/debian.cnf --force --opt --single-transaction --quick --skip-lock-tables {mysql_events} --databases {source} --max_allowed_packet=1G {mysqldump_args} | gzip > {mysql_dump_dir}/{source}.gz
976+
{dump_run} '{dump_prefix_cmd} mysqldump {dump_creds} --force --opt --single-transaction --quick --skip-lock-tables {mysql_events} --databases {source} --max_allowed_packet=1G {mysqldump_args}' | gzip > {mysql_dump_dir}/{source}.gz
975977
if [[ $? -ne 0 ]]; then
976978
WAS_ERR=1
977979
echo "ERROR: Dump failed, attempt $DUMP_ATTEMPT of {dump_attempts}"
@@ -998,7 +1000,9 @@ def run_cmd_pipe(cmd):
9981000
mysqldump_args=item["mysqldump_args"],
9991001
grep_db_filter=grep_db_filter,
10001002
source=item["source"],
1001-
dump_attempts=item["dump_attempts"]
1003+
dump_attempts=item["dump_attempts"],
1004+
dump_run="docker exec {container} sh -lc".format(container=item["docker_container"]) if item["docker_mode"] else "bash -c",
1005+
dump_creds="-u\"$MYSQL_USER\" -p\"$MYSQL_PASSWORD\"" if item["docker_mode"] else "--defaults-file=/etc/mysql/debian.cnf"
10021006
)
10031007

10041008
# If hourly retains are used keep dumps only for 59 minutes
@@ -1007,7 +1011,7 @@ def run_cmd_pipe(cmd):
10071011
#!/bin/bash
10081012
set -e
10091013
1010-
ssh {ssh_args} -p {port} {user}@{host} '
1014+
ssh {ssh_args} -p {port} {user}@{host} bash -se <<'EOF'
10111015
set -x
10121016
set -e
10131017
set -o pipefail
@@ -1021,7 +1025,7 @@ def run_cmd_pipe(cmd):
10211025
cd {mysql_dump_dir}
10221026
find {mysql_dump_dir} -type f -name "*.gz" -mmin +{mmin} -delete
10231027
{script_dump_part}
1024-
'
1028+
EOF
10251029
"""
10261030
).format(
10271031
ssh_args=ssh_args,

0 commit comments

Comments
 (0)