备份同步数据
#!/bin/bash # 定义变量 user="root" pass="un1ware" host="" file=$(date +"%Y-%m-%d") # 使用日期作为文件夹名称 # 获取主从状态信息 master_status=$(mysql --user="$user" --password="$pass" -h "$host" <<EOF SHOW MASTER STATUS; EOF ) # 创建备份目录 mkdir "$file" # 备份数据库 mydumper -u root -p un1ware123 -h xxxx -t 10 -v 3 -G -R -E -C -o -d "$file" &>> sqlbak.log # 重置并启动从库 mysql --user="$user" --password="$pass" <<EOF STOP SLAVE; RESET SLAVE; CHANGE MASTER TO MASTER_HOST='$host', MASTER_USER='$user', MASTER_PASSWORD='$pass', MASTER_LOG_FILE='$master_status', MASTER_LOG_POS=$(echo "$master_status" | awk '{print $2}'); START SLAVE; EOF # 检查从库状态 slave_status=$(mysql -uroot -pun1ware123 -e "SHOW SLAVE STATUS\G") echo "$slave_status" | grep -E "Master|Slave|SQL|Relay"
数据备份
#!/bin/bash # MySQL 用户名、密码及备份目录定义 MYSQL_USER="your_mysql_username" MYSQL_PASS="your_mysql_password" BACKUP_DIR="/path/to/backup/directory" # 检查是否传入了至少一个库名参数 if [ $# -eq 0 ]; then echo "Usage: $0 database1 [database2 ...]" exit 1 fi for DB in "$@"; do # 备份文件名格式:库名_当前日期时间.sql.gz BACKUP_FILE="$BACKUP_DIR/${DB}_$(date +'%Y%m%d%H%M%S').sql.gz" # 执行mysqldump并压缩备份文件 mysqldump -u$MYSQL_USER -p$MYSQL_PASS --single-transaction $DB | gzip > $BACKUP_FILE # 输出备份状态信息 echo "Database ${DB} backed up to ${BACKUP_FILE}" done # 检查备份目录权限,确保可写,并且执行完后清理临时文件等(根据实际情况调整) if [ ! -w "$BACKUP_DIR" ]; then echo "Backup directory is not writable!" exit 1 fi # 可以添加日志记录功能,如将备份结果输出到日志文件 echo "Backup script completed at $(date)" >> /path/to/backup/logfile.log exit 0
这个是导出数据库的时候 写入日志 进度
mysqldump -u$MYSQL_USER -p$MYSQL_PASS --single-transaction $DB | gzip > $BACKUP_FILE |tee -a xxx.log
标签:--,备份,DB,user,MYSQL,BACKUP,仅供参考 From: https://www.cnblogs.com/JIKes/p/17986753