mysqldump
[root@localhost node]# cat /root/mysqlbeifen.sh #!/bin/bash # 定义参数 CONTAINER_NAME="mysql" HOST_BACKUP_DIR="/bak" REMOTE_MACHINE_IP="192.168.0.29" REMOTE_TARGET_DIR="/bak/cd" MYSQL_USER="root" MYSQL_PASSWORD="pass123" # 获取MySQL容器的ID CONTAINER_ID=$(docker ps -f "name=$CONTAINER_NAME" -q) if [ -z "$CONTAINER_ID" ]; then echo "MySQL容器不存在,请确保MySQL容器正在运行." exit 1 fi # 创建备份文件名 BACKUP_FILE="mysql_$(date +%Y%m%d_%H%M%S).sql" # 备份MySQL数据库到主机 docker exec $CONTAINER_ID mysqldump --max_allowed_packet=512M -u $MYSQL_USER -p$MYSQL_PASSWORD --all-databases > $HOST_BACKUP_DIR/$BACKUP_FILE if [ $? -eq 0 ]; then echo "MySQL数据库已成功备份到主机." else echo "备份过程中发生错误,请检查日志." exit 1 fi # 压缩备份文件为tar.gz格式 tar -czvf $HOST_BACKUP_DIR/$BACKUP_FILE.tar.gz -C $HOST_BACKUP_DIR $BACKUP_FILE if [ $? -eq 0 ]; then echo "备份文件已成功压缩为tar.gz格式." else echo "压缩过程中发生错误,请检查日志." exit 1 fi # 远程拷贝到其他机器 scp $HOST_BACKUP_DIR/$BACKUP_FILE.tar.gz $REMOTE_MACHINE_IP:$REMOTE_TARGET_DIR/ if [ $? -eq 0 ]; then echo "备份文件已成功远程拷贝到其他机器." else echo "远程拷贝过程中发生错误,请检查网络连接和远程机器的权限." exit 1 fi # 删除本机的备份文件 rm -rf $HOST_BACKUP_DIR/$BACKUP_FILE $HOST_BACKUP_DIR/$BACKUP_FILE.tar.gz if [ $? -eq 0 ]; then echo "本机上的备份文件已成功删除." else echo "删除本机备份文件过程中发生错误,请检查权限." exit 1 fi
mysql备份工具innobackupex
#!/bin/bash innobackupex --defaults-file=/docker/mysql/conf/my.cnf --host=127.0.0.1 --port=3306 --user=root --password='111111' --datadir=/docker/mysql/data/ --stream=tar ./ | gzip | ssh root@192.168.0.29 \ "cat - > /bak/zhi/`date +%Y-%m-%d`.tar.gz"
标签:tar,--,备份,二种,echo,mysql,备份文件,BACKUP,DIR From: https://www.cnblogs.com/aldary/p/18196823