crontab 使用docker mysqldump备份

#!/bin/bash
# ignore report and report_detail
now=$(date "+%Y-%-m-%d_%H-%M-%S")
test_ignore_report="/root/test_data_backup/test_ignore_report{$now}.sql"

# don't use -it, it's a trap
$ docker exec  {container} mysqldump --ignore-table={database}.{table} -u{user} -p{password} {database} > {target filename}
docker exec  mysql mysqldump --ignore-table=test.report --ignore-table=test.report_detail -utest -ptest2020 test > $test_ignore_report

# delete the first line, because there's mysqldump useing password at command waring at first line.
sed -i '1d' $test_ignore_report

# only table struct, use -d
# specify table, use --tables {table}
report="/root/test_data_backup/report{$now}.sql"
docker exec  mysql mysqldump -utest -ptest2020  -d test --tables report report_detail > $report
sed -i '1d' $report

一个实际的例子:

backup_dir="/data/hsh/mysql_backup"
time="$(date +"%Y%m%d_%H%M%S")"
username="root"
password="joycode"

data_file1="$backup_dir/fxpay"_"$time.sql"

data_file2="$backup_dir/merunion"_"$time.sql"

rm /data/hsh/mysql_backup/*.sql

docker exec mysql mysqldump -u"$username" -p"$password" fxpay  > $data_file1

docker exec mysql mysqldump -u"$username" -p"$password" merunion  > $data_file2

tar -zcf "$backup_dir/fxpay"_"$time.sql".tar "$data_file1"

tar -zcf "$backup_dir/merunion"_"$time.sql".tar "$data_file2"

find $backup_dir -mtime +3 -type f | xargs rm -f

参考:

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享