docker scheduled task backup mysql database and recovery

1. Write an execution script to back up the database

#! /bin/bash

#读取用户环境变量
source /etc/profile

#数据库备份目录
DATA_PATH=/home/java/backup/mysql

#容器名
DOCKER_NAME=mysql

#数据库用户及密码
DB_NAME=
DB_USER=root
PASSWORD=XXXXXX

#linux:备份mysql数据库
#mysqldump -u$DB_USER -p$PASSWORD --all-databases > $DATA_PATH/backups_$(date +%F).sql

#docker:备份容器中的数据
docker exec $DOCKER_NAME mysqldump -u$DB_USER -p$PASSWORD --all-databases > $DATA_PATH/backups_$(date +"%y-%m-%d-%H:%M").sql

#重命名文件,时间格式':'符号不能作为文件名进行ssh传输
cp $DATA_PATH/backups_$(date +"%y-%m-%d-%H:%M").sql $DATA_PATH/remote_backup.sql

#将生成的备份文件远程传到备份服务器备份, 没需求的可以注释(需要设置ssh公钥不然会失败)
scp $DATA_PATH/remote_backup.sql  [email protected]:./backup/remote_mysql/remote_backup.sql

#删除10天前的备份文件
find /var/lib/mysql -ctime +10 -name "*.sql" -exec rm -rf {} \;

Script file storage path (you can place it according to the situation)

/home/java/shell/dbBackup.sh

2. Add scheduled tasks

Open the scheduled task configuration file and add execution script commands. Here I use system-level timing tasks to avoid some permission issues, and user-level timing tasks can also be used as needed

sudo vim /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
#每隔8小时定时备份数据库
0 */8 * * * root /home/java/shell/dbBackup.sh

3. Restore backup data

Copy the backup data to the container
$ docker cp backups_01.sql mysql-stage(容器名):/tmp/
into the container
$ docker exec -it mysql-stage(容器名) /bin/bash
enter mysql
/bin/bash: mysql -uroot -p密码
restore database
mysql> source /tmp/backups_01.sql

You can also use the following command to directly restore the data

docker exec -it mysql-stage /bin/bash -c 'mysql -uroot -pcierp123. < /tmp/backups_01.sql'

4. Common problem solving of scheduled task execution failure:

View scheduled task log records
$ sudo tail /var/log/cron
1.the input device is not a TTY:

Executing dbBackup.sh directly can be executed normally. After creating crontab, execute the scheduled task, it will prompt that the input device is not a TTY! Find information and test found, as long as the -it can be removed.

There is no terminal device when Linux executes timing tasks. The word TTY comes from Teletypes, or teletypewriters. In fact, this error is related to one of our habits. Generally speaking, we need to interact with the container after starting the container. This is to add the "-it" parameter. In the scheduled task, if the script is in the background Running, there will be no interactive terminal, which will cause the error shown in the title. The solution is to remove the "-it" parameter.

2.[Warning] Using a password on the command line interface can be insecure.

Meaning: It is not safe to directly enter password account information on the command line. It can be ignored directly without affecting the specific results

You can also modify the mysql configuration file and add the following content

[mysqldump]
user=root
password=你的密码

Then restart mysql, modify the mysqldump statement, and remove the -uroot -p parameter

3. Problems caused by environment variables

Direct execution of dbBackup.sh can be executed normally, but the result of the scheduled task execution is wrong. There may be an environment variable problem. The scheduled task will not load the user's environment variable. The solution: the script file actively introduces the environment variable

#读取用户环境变量
source /etc/profile

Guess you like

Origin blog.csdn.net/weixin_44863237/article/details/128892356