- 安裝命令 mysqldump
sudo apt update
sudo apt install mysql-client
- 編輯腳本內(nèi)容 export_mysql.sh
#!/bin/bash
# Docker 容器的 IP 地址
container_ip="172.26.77.246"
# MySQL 數(shù)據(jù)庫的用戶名和密碼
mysql_user="root"
mysql_password="your_password"
# 要導(dǎo)出的數(shù)據(jù)庫列表
databases=("website1" "website2" "website3")
# 導(dǎo)出文件的目錄
export_dir="/path/to/export/directory"
# 創(chuàng)建導(dǎo)出文件目錄
mkdir -p $export_dir
# 循環(huán)導(dǎo)出每個數(shù)據(jù)庫
for db_name in "${databases[@]}"
do
# 導(dǎo)出文件名
export_file="$export_dir/$db_name.sql"
# 使用 mysqldump 導(dǎo)出數(shù)據(jù)庫到宿主機(jī)上
mysqldump -h $container_ip -u $mysql_user -p$mysql_password $db_name > $export_file
# 輸出導(dǎo)出完成信息
echo "數(shù)據(jù)庫 $db_name 已導(dǎo)出到 $export_file"
done
# 壓縮sql文件
tar -cvzf $export_dir/archive_$(date +%Y%m%d).tar.gz $export_dir/*.sql
# 刪除sql原文件
rm -rf $export_dir/*.sql
- 執(zhí)行以下命令給腳本添加執(zhí)行權(quán)限:
chmod +x export_mysql.sh
文章來源地址http://www.zghlxwxcb.cn/news/detail-833411.html
文章來源:http://www.zghlxwxcb.cn/news/detail-833411.html
到了這里,關(guān)于導(dǎo)出docker MySQL中的數(shù)據(jù)庫寫一個shell 腳本的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!