当前位置:网站首页>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
边栏推荐
- Rust基础知识
- Construction and application of medical field Atlas of dingxiangyuan
- Wechat official account - menu
- Lumiprobe reactive dye miscellaneous dye: BDP FL ceramide
- Lumiprobe 点击化学丨非荧光叠氮化物:叠氮化物-PEG3-OH
- 又拍云 Redis 的改进之路
- Lumiprobe 活性染料丨环炔染料:AF488 DBCO,5 异构体
- Lumiprobe click chemistry - non fluorescent azide: azide-peg3-oh
- SSL v** technical principle
- 雷达基本组成
猜你喜欢

postgresql源码学习(24)—— 事务日志⑤-日志写入WAL Buffer

Basic use of text preprocessing library Spacy (quick start)

深度学习遥感数据集

明德扬XILINX-K7-325T/410T核心板数据手册

Lumiprobe reactive dye carboxylic acid: sulfo cyanine7.5 carboxylic acid

又拍云 Redis 的改进之路

无意发现的【TiDB缓存表】,竟能解决读写热点问题

File常用工具類, 流相關運用 (記錄)

分页sql(rownum、row_number、dense_rank、rank)
![中序和后序遍历构建二叉树[递归划分区间与回溯拼接子树+中后序和中前序的相似与不同]](/img/c6/a2780620fef8bc5f7b564ef173f589.png)
中序和后序遍历构建二叉树[递归划分区间与回溯拼接子树+中后序和中前序的相似与不同]
随机推荐
Take another picture of cloud redis' improvement path
Biovendor free light chain( κ and λ) Chemical properties of ELISA Kit
Lumiprobe 脱氧核糖核酸丨炔烃dT亚磷酰胺
About sql+nosql: newsql database
阿里云体验有奖:使用PolarDB-X与Flink搭建实时数据大屏
第九章 APP项目测试(此章完结)
材质 动态自发光
Lumiprobe 点击化学丨非荧光炔烃:己酸STP酯
MCS: multivariate random variable polynomial distribution
MySQL为什么选择B+树存储索引
Is it safe to open a stock account at present? Can I open an account online directly
Informatics Olympiad all in one 2062: movie tickets
Middle order and post order traversal to construct binary tree [recursive partition interval and backtracking splicing subtree + similarity and difference between middle post order and middle pre orde
Lumiprobe reactive dye cycloalkyne dye: af488 dbco, 5 isomer
雷达相关内容简介
Informatics Olympiad 1001:hello, world!
JS 会有变量提升和函数提升
14.IP协议-bite
Real software testers = "half product + Half development"?
MySQL开发规范.pdf