定时备份mysql脚本

定时备份mysql指定数据库脚本,保留60天

#!/bin/bash
# path
cd /opt/pmo/mysql_data
target_directory="/opt/pmo/mysql_data"
# get time now
current_time=$(date +%s)
# cal 2 2months ago
before_time=$(date -d "60 days ago" +%s)
file_name=metersphere_`date +"%Y%m%d%H%M%S"`
echo $file_name
# delete 
find "$target_directory" -type f -name "*.sql" -ctime +$(( (current_time - before_time) / 86400 )) -exec rm {} \;
find "$target_directory" -type f -name "*.tar.gz" -ctime +$(( (current_time - before_time) / 86400 )) -exec rm {} \;
mysqldump -h 192.168.9.27 -ums_backup -pPassword123@mysql -P3307  backup_db> "$file_name".sql
tar czf "$file_name".tar.gz "$file_name".sql
rm -rf "$file_name".sql

 

posted @ 2023-10-10 11:33  testway  阅读(14)  评论(0编辑  收藏  举报