Shell script regularly backs up the MySQL database deployed by docker

Because the server has been attacked before, the database is always ransomware. In the previous article, I mentioned some ways to improve security, but I think it is still a bit unreliable. Also combined with the previous study of the script for scheduled backup, today I will write an article about scheduled backup data.

One, the script

My MySQL is deployed using docker, so the backup command is to enter the container through docker, and then execute the backup command.

#!/bin/bash
# 设置mysql的登录用户名和密码(根据实际情况填写)
mysql_user="root"
mysql_password="root"
mysql_host="localhost"
mysql_port="3306"
mysql_charset="utf8mb4"
 
# 备份文件存放地址(根据实际情况填写)
backup_location=/usr/local
 
# 是否删除过期数据
expire_backup_delete="ON"
expire_days=7
backup_time=`date +%Y%m%d%H%M`
backup_dir=$backup_location
welcome_msg="Welcome to use MySQL backup tools!"
# 备份指定数据库中数据(此处假设数据库是mysql_backup_test)
 docker exec -it mysql mysqldump -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password -B test1 > $backup_dir/mysql_backup_test-$backup_time.sql
 
# 删除过期数据
if [ "$expire_backup_delete" == "ON" -a  "$backup_location" != "" ];then
        `find $backup_location/ -type f -mtime +$expire_days | xargs rm -rf`
        echo "Expired backup data delete complete!"
fi

Note: The
above is mainly the definition of variables, which has been explained in detail. I am mainly here to explain the backup commands

docker exec -it mysql mysqldump -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password -B test1 > $backup_dir/mysql_backup_test-$backup_time.sql

Leaving aside these, let’s enter the container to test the backup commands, and first disassemble and analyze the commands

# docker进入容器的命令
docker exec -it 容器名
# 备份数据库的命令
mysqldump -uroot -p123456 -B 数据库名 > /usr/111.sql

Connect it up (this is my order, you can modify it according to your actual situation)

docker exec -it mysql01 mysqldump -uroot -p123456 -B cj > $backup_dir/cj-$backup_time.sql

Two, timing tasks

# 启动定时任务
crontab -e

# 将定时任务写入其中 分 时 日 月 年
* * * * * /bin/bash /usr/data/backup/backupdb.sh

# 查看定时任务列表
crontab -l

Second supplement at 19:43:44 on July 6, 2020

Woke up this morning and went to the server to check whether the scheduled backup script written last night was executed successfully. Go to the backup directory and found that the file was generated. Then the cat check found that there was no output. It was strange. The manual execution was successful yesterday, but it was put into crontab. Why?

With this problem, I have been on Baidu for a long time, a long time, and a long time. There are generally two cases on the Internet. First, the path of the mysqldump command is not fully provided, and second, the environment variables are not added;

Combining these two directions, I tried all solutions, all ended in failure;

Failure one, complete the path of the mysqldump command.
If you install MySQL directly on the server, it may not be very complicated for you. Most of the online solutions are based on this, but my MySQL is installed by docker. So I need to enter the container to find the location of his mysqldump. Then complete

docker exec -it mysql01 /usr/bin/mysqldump -uroot -p123456 -B cj > $backup_dir/cj-$backup_time.sql

Among them, /usr/bin is the path of the mysqldump file in the container. The
result: failed

Failure two, add environment variable
way1: add it in the script

source /etc/profile
is annotated by me here
Insert picture description here

Result: failed

way2: add in the script that executes the task

crontab -e

*/1 0 * * * source profile;/home/backup/mysql_backup_test_backup.sh 2>&1

Result: failed

Then Lin Lin always looked for a lot of ways, but the starting point and direction were these two, and it has not been successful. I have vowed to say that it will succeed on the Internet, and the comments have also been successful, but I can’t succeed, making me very suspicious of life; Finally I began to wonder if there was a problem with this command? Although manual execution is normal, there may be problems with automatic execution for some reason, so I started Baidu "crontab to execute docker". I have to say that Baidu is really important to the direction and focus. Find the problem in Baidu

Insert picture description here
It is related to the addition of -it parameter

Your docker exec command says it needs “pseudo terminal and runs in interactive mode” (-it flags) while cron doesn't attach to any TTYs.
Generally speaking, exec adds the -it parameter to open a terminal, and the scheduled task cannot be entered Any terminal.

The problem is solved after removing the parameter -it of docker exec.

After reading this, I realized that this was the reason, and then I thought about their previous posts. It seems that there is no -it parameter, and then put in the scheduled task execution. It really succeeded, hey, it's really hard!

docker exec mysql01 mysqldump -uroot -p123456 -B cj > $backup_dir/cj-$backup_time.sql

Guess you like

Origin blog.csdn.net/Curtisjia/article/details/107143830