以下是一些实用的MySQL功能的Linux shell脚本分享:
#!/bin/bash
DB_USER="your_username"
DB_PASS="your_password"
DB_NAME="your_database"
BACKUP_DIR="/path/to/backup/directory"
timestamp=$(date +%Y%m%d%H%M%S)
backup_file="$BACKUP_DIR/$DB_NAME-$timestamp.sql.gz"
mysqldump -u $DB_USER -p$DB_PASS $DB_NAME | gzip > $backup_file
echo "Database backup created: $backup_file"
将上述脚本保存为backup_database.sh
,然后使用以下命令运行脚本:
chmod +x backup_database.sh
./backup_database.sh
#!/bin/bash
DB_USER="your_username"
DB_PASS="your_password"
DB_NAME="your_database"
BACKUP_FILE="/path/to/backup/file.sql.gz"
gunzip < $BACKUP_FILE | mysql -u $DB_USER -p$DB_PASS $DB_NAME
echo "Database restored from: $BACKUP_FILE"
将上述脚本保存为restore_database.sh
,然后使用以下命令运行脚本:
chmod +x restore_database.sh
./restore_database.sh
#!/bin/bash
BACKUP_DIR="/path/to/backup/directory"
DAYS_TO_KEEP=7
find $BACKUP_DIR -name "*.sql.gz" -type f -mtime +$DAYS_TO_KEEP -exec rm {} \;
将上述脚本保存为cleanup_backup.sh
,然后使用以下命令运行脚本:
chmod +x cleanup_backup.sh
./cleanup_backup.sh
这些脚本可以根据实际需求进行修改和定制,以适应不同的MySQL环境和备份策略。
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站