mysqldump全量&增量备份脚本,生产可用
路径和密码等相关变量根据实际情况修改,经过验证,数据量不大可用
全量脚本
#!/bin/env bash
full_dir=/opt
incremental_dir=/opt
log_file=/opt/full_backup.log
commence=$(date +%s)
mysqldump --single-transaction --flush-logs --quick --all-databases --master-data=2 --events --routines --triggers \
| gzip -9 --quiet --force - > $full_dir/$(date +%F__%H-%M-%S).sql.gz
# Another way to backup
#sleep 2
#
#pipe=$(mktemp)
#rm -f $pipe
#mkfifo $pipe
#gzip -9 --quiet --force < $pipe > $full_dir/$(date +%F__%H-%M-%S).sql.gz &
#mysqldump --single-transaction --flush-logs --quick --all-databases --master-data=2 --events --routines --triggers >> $pipe
#rm -f $pipe
closure=$(date +%s)
rm -rf $incremental_dir/binlog.*
abidance=$((closure-commence))
echo -e '\n' >> $log_file
echo "Commence: `date --date=@$commence +%F__%T`, Closure: `date --date=@$closure +%F__%T`" >> $log_file
function convert_seconds() {
local T=$1
local D=$((T/60/60/24))
local H=$((T/60/60%24))
local M=$((T/60%60))
local S=$((T%60))
(($D > 0)) && printf ' %d days' $D
(($H > 0)) && printf ' %d hours' $H
(($M > 0)) && printf ' %d minutes' $M
((($D > 0 || $H > 0 || $M > 0) && $S != 0)) && printf ' and '
(($S > 0 )) && printf '%d seconds\n' $S
}
echo full backup abidance `convert_seconds $abidance` >> $log_file
增量脚本
#!/bin/bash
incremental_dir=/opt
data_dir=/var/lib/mysql
log_file=/opt/incremental_backup.log
binlog_index=/var/lib/mysql/binlog.index
criterion=`date -d 'last sunday' +%s` # record last full backup time
mysql -uroot --password='' -e 'flush logs' >& /dev/null # flush logs
quantity=`wc --lines $binlog_index | awk '{print $1}'` # number of binlog files
quantum=0
for file in `cat $binlog_index`;do
base=`basename $file`
quantum=$((++quantum))
destination_file=$incremental_dir/$base
source_file=${data_dir}/$base
timestamp=`stat --format=%Y $source_file` # timestamp of the binlog
if [[ $timestamp -lt $criterion ]];then
continue
fi
if [[ $quantum -eq $quantity ]];then
echo skip newly flush logs created $source_file >> $log_file
else
if [[ $quantum -eq 1 ]];then
echo -e '\n' >> $log_file
echo `date +"%F %T %u %A %B"` binlog backup starting >> $log_file
fi
if ! [[ $source_file -nt $destination_file ]];then
echo $source_file exist! >> $log_file
else
cp -a $source_file $incremental_dir
echo $source_file copyed >> $log_file
fi
fi
done
echo `date +"%F %T %u %A %B"` binlog backup finished >> $log_file
定时任务
0 2 * * 0 /bin/bash -x /opt/full_backup.sh > /dev/null 2>&1
0 2 * * 1-6 /bin/bash -x /opt/incremental_backup.sh > /dev/null 2>&1
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律