当前位置:网站首页>MySQL scheduled full database backup & rolling deletion of backup data before the specified date
MySQL scheduled full database backup & rolling deletion of backup data before the specified date
2022-06-29 15:29:00 【Walk alone by the river】
Requirements describe
The implementation is as follows MySQL Backup function :
1、 Yes MySQL Library table schema And data Make a scheduled backup
2、 Scroll to delete a specified date ( Such as 30 God ) All backup data before
The test script
vim /data/backup/scripts/MySQL_Backup.sh
#!/bin/bash
# Desc:
# 1. Implements all MySQL databases backup
# 2. Implements Rolling delete data 30 days ago
set -x
USERNAME="DB_Backuper"
PASSWORD="DB_Backuper"
DBHOST="localhost"
PORT=3306
MySQL_BACKUP_DBLIST="db_1|db_2|db_3|db_4|db_5"
MySQL_STORAGE_BACKUP_PATH="/data/backup/data"
MySQL_BIN_PATH="/usr/bin"
Y_DIR=`(date "-d 0 day ago" +%Y)`
M_DIR=`(date "-d 0 day ago" +%Y%m)`
D_DIR=`(date "-d 0 day ago" +%Y%m%d%H%M)`
Y_LAST_DIR=$(date +%Y "-d -1 year")
TWO_YEARS="$Y_LAST_DIR|$Y_DIR"
# N days ago's data will be deleted
N=30
echo -e "*****************************************"
echo ">>>>>>>>>>>>>>>>>>>>>>> backup started on" `date +%Y/%m/%d--%k:%M:%S`;
for DB in `echo ${
MySQL_BACKUP_DBLIST//|/ }`;
do
mkdir -p $MySQL_STORAGE_BACKUP_PATH/$DB/$Y_DIR/$M_DIR/$D_DIR
DB_BACKUP_PATH=$MySQL_STORAGE_BACKUP_PATH/$DB/$Y_DIR/$M_DIR/$D_DIR
$MySQL_BIN_PATH/mysqldump --host=$DBHOST --user=$USERNAME --password=$PASSWORD --port=$PORT --opt --triggers --routines --single-transaction --set-gtid-purged=OFF $DB | sed 's/DEFINER=`[^`]+`@`[^`]+`/DEFINER=CURRENT_USER/g' > $DB_BACKUP_PATH/$DB.sql
# Rolling delete data N days ago
for YEAR in `echo ${
TWO_YEARS//|/ }`;do
PATH_TOBE_CHECK=$MySQL_STORAGE_BACKUP_PATH/$DB/$YEAR
if [ -d $PATH_TOBE_CHECK ];then
find $PATH_TOBE_CHECK -type d -mtime +$N | xargs rm -rf
fi
done
done;
echo ">>>>>>>>>>>>>>>>>>>>>>> backup ended on" `date +%Y/%m/%d--%k:%M:%S`
echo -e "*****************************************"
explain :
- Text substitution
for … in The syntax requires that data should be separated by spaces , You need to replace the database list with text
${MySQL_BACKUP_DBLIST//|/ }
Be careful } There's a space in front of it
You can also use it sed To do text substitution .
- Judge the folder ( Or document ) Non empty
If the folder or file is empty , An error will be reported when deleting , You only need to check the existing files ( clip ) Just delete .
① Determine whether the folder exists : if [ -d $PATH_TOBE_CHECK ]
② Determine that the folder does not exist : if [ ! -d $PATH_TOBE_CHECK ]
③ If it is the file type , Only need to "-d" Replace parameter with "-f" Parameters .
- The data of the previous year cannot be deleted in the cross year period ( Ideas can be optimized )
Specify near 2 Delete the year of
边栏推荐
- 阿里云体验有奖:使用PolarDB-X与Flink搭建实时数据大屏
- MySQL开发规范.pdf
- Lumiprobe 脱氧核糖核酸丨炔烃dT亚磷酰胺
- Unity C basic review 27 - delegation example (p448)
- 高分三号卫星(GF-3)简介
- 遥感典型任务分析
- Is it safe to open a stock account at present? Can I open an account online directly
- Lumiprobe deoxyribonucleic acid phosphate CpG 1000 solid carrier
- LeetCode笔记:Biweekly Contest 81
- 近期工作总结
猜你喜欢

关于SQL+NoSQL : NewSQL数据库

Lumiprobe 活性染料丨杂染料:BDP FL 神经酰胺

Imgutil image processing tool class, text extraction, image watermarking

Create an API rapid development platform, awesome!

Lumiprobe click chemistry - non fluorescent azide: azide-peg3-oh

Uncover the practice of Baidu intelligent test in the field of automatic test execution

又拍云 Redis 的改进之路

使用自定义注解实现Redis分布式锁

Unity C # basic review 26 - first acquaintance Commission (p447)

PyTorch 二维多通道卷积运算方式
随机推荐
PyTorch 二维多通道卷积运算方式
无意发现的【TiDB缓存表】,竟能解决读写热点问题
目前股票开户安全吗?可以直接网上开户吗
js获取上个月第一天以及本月的最后一天
Lumiprobe 活性染料丨环炔染料:AF488 DBCO,5 异构体
Development and application of NFT chain Games: Six noteworthy NFT trends in 2022
Informatics Olympiad all in one 1002: output the second integer
Solution to the problem that the assembly drawing cannot be recognized after the storage position of SolidWorks part drawing is changed
Informatics Olympiad all in one 2062: movie tickets
Is it safe to open a stock account at present? Can I open an account online directly
Sofaregistry source code | data synchronization module analysis
c#Sqlite类库
Biovendor free light chain( κ and λ) Chemical properties of ELISA Kit
Hi, you have a code review strategy to check
雷达发射机
投资reits基金是靠谱吗,reits基金安全吗
Chaîne lumineuse libre biovendor κ Et λ) Propriétés chimiques du kit ELISA
Chapter IX app project test (4) test tools
上次给我们发福利的 TDesign ,今天讲讲它的开源故事
Bash summary online log