备份
2024年12月10日小于 1 分钟
备份指定表
#!/bin/bash
DB_USER="root"
DB_PASSWORD="Mysql@2023"
DB_NAME="cs"
# Regular expression pattern for table names
TABLE_PATTERN="t%"
# Get a list of table names matching the pattern
TABLES_TO_DUMP=$(mysql -u $DB_USER -p$DB_PASSWORD $DB_NAME -e "SHOW TABLES LIKE '$TABLE_PATTERN';" | tail -n +2)
echo $TABLES_TO_DUMP
# Run mysqldump with the list of tables to exclude
mysqldump -u $DB_USER -p$DB_PASSWORD $DB_NAME $TABLES_TO_DUMP > backup.sql
备份
0 1 * * * sh /data/backup/backup.sh
# 获取当前日期并格式化为YYYYMMDD
current_date=$(date +"%Y%m%d")
# 打印当前日期
echo $current_date
mysqldump --default-character-set=utf8mb4 -h 127.0.0.1 -u root -pMysql@2023 --opt report_data > /data/backup/report_data${current_date}.sql
mysqldump --default-character-set=utf8mb4 -h 127.0.0.1 -u root -pMysql@2023 --opt report_conf > /data/backup/report_conf${current_date}.sql
# 删除七天前文件
find /data/backup/ -type f -mtime +900 -exec rm {} \;