MySQL scheduled backup in production environment

Use Linux's own crontab timing task function to execute scripts that back up the database regularly

Implementation function list

  • Backup
  • Scheduled backup
  • Compress the backup file
  • Delete expired backup files
  • Restore backup files

Data backup dump

The database has a command to export the data and structure in the database, which is a backup.
Restoring the backed-up data will delete the table in the original data and rebuild, and then insert the data in the backup. This is recovery.
This point needs to be noted. If there is more data before the restoration than the backup, there will be no more data after the restoration.

Backup and mysqldump -h -u [用户名] -p [库名] > [导出的.sql 文件]
restore mysql -u [用户名] -p [库名] < [导出的.sql 文件]

shell script

To complete a fully functional backup program, shell scripts are needed.
We want this script to be backed up to the specified path, and compressed and stored, up to 30, delete the oldest if more than 30, and record the operation log.
Nothing is said, the words are in the script, do it!

#用户名
username=root
#密码
password=nicai
#将要备份的数据库
database_name=l_love_you

#保存备份文件最多个数
count=30
#备份保存路径
backup_path=/app/mysql_backup
#日期
date_time=`date +%Y-%m-%d-%H-%M`

#如果文件夹不存在则创建
if [ ! -d $backup_path ]; 
then     
    mkdir -p $backup_path; 
fi
#开始备份
mysqldump -u $username -p$password $database_name > $backup_path/$database_name-$date_time.sql
#开始压缩
cd $backup_path
tar -zcvf $database_name-$date_time.tar.gz $database_name-$date_time.sql
#删除源文件
rm -rf $backup_path/$database_name-$date_time.sql
#更新备份日志
echo "create $backup_path/$database_name-$date_time.tar.gz" >> $backup_path/dump.log

#找出需要删除的备份
delfile=`ls -l -crt  $backup_path/*.tar.gz | awk '{print $9 }' | head -1`

#判断现在的备份数量是否大于阈值
number=`ls -l -crt  $backup_path/*.tar.gz | awk '{print $9 }' | wc -l`

if [ $number -gt $count ]
then
  #删除最早生成的备份,只保留count数量的备份
  rm $delfile
  #更新删除文件日志
  echo "delete $delfile" >> $backup_path/dump.log
fi

As the name suggests to the script from a beautiful name dump_mysql.sh
to the script given execute permission chmod +x dump_mysql.sh, after executing the script file is a viable green
execution method: ./ plus the script name

chmod命令参数含义--
+ 代表添加某些权限
x 代表可执行权限

Crontab

Crontab is a timed task function that comes with Linux, and we can use it to execute a dump_mysql.shscript every morning .

Crontab usage:
  • crontab -l View the list of scheduled tasks
  • crontab -e edit (add/delete) timing tasks

Run the crontab -e command to open an editable text, enter the 00 01 * * * /app/dump_mysql.sh
preservation and exit to complete the addition.

Content explanation:

00 01 * * * /app/dump_mysql.shLook at it in two parts. The
first part 00 01 * * *is the period of the timed task, and the second part is /app/dump_mysql.shthe time to do things.
The period expression is five placeholders, representing: minute, hour, day, month, week

Placeholders are used to *indicate that they are used in the first digit every minute, the second digit is every hour, and so on.
Placeholders are used to 具体数字indicate 具体时间that 10 is used in the first digit to mean 10 points, and when used in the third digit, it means 10th. such push
placeholders -represent 区间, by 5-7 in the first 5 is assigned to seven points with a fifth place in the week represents 5 to Sunday, and so on
placeholders /represent 间隔, 5-10 / 2 with In the first place, it is 5 minutes to 10 minutes, and the interval is 2 minutes. In the second place, it is used to indicate the interval of 2 hours from 5 to 10, and so on.
Placeholders are used to ,indicate 列表. 5,10 in the first place is 5 minutes and 10 points, used in the fourth place to indicate May and October, and so on

It will be improved and optimized later.
Reference:
https://segmentfault.com/a/1190000023285648

Guess you like

Origin blog.csdn.net/abu935009066/article/details/112334195