File size: 1,457 Bytes
ee6ea45 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#!/bin/bash
# 检查是否需要导入数据库
if [ "$DB_IMPORT" = "yes" ]; then
echo "Starting database import..."
# 读取下载 URL 和解压密码(如果有)
DUMP_URL=${DUMP_URL:-""} # 数据库 dump 文件的下载 URL
DUMP_PASSWORD=${DUMP_PASSWORD:-""} # 如果 dump 文件有密码保护
# 检查 URL 是否为空
if [ -z "$DUMP_URL" ]; then
echo "DUMP_URL is not set. Skipping database import."
# 不退出,仅跳过导入步骤
exit 0
fi
# 下载数据库 dump 文件
echo "Downloading dump file from $DUMP_URL..."
wget -O /tmp/dump.sql "$DUMP_URL" || { echo "Failed to download dump file."; exit 1; }
# 检查文件是否有密码保护,并使用 pg_restore 或 psql 进行导入
if [ -n "$DUMP_PASSWORD" ]; then
echo "Dump file is protected with a password."
# 使用 pg_restore 导入
PGPASSWORD="$DUMP_PASSWORD" pg_restore -U $DB_POSTGRESDB_USER -d $DB_POSTGRESDB_DATABASE /tmp/dump.sql || {
echo "Failed to import dump file with password."; exit 1;
}
else
echo "Dump file is not password protected."
# 使用 psql 直接导入
psql -U $DB_POSTGRESDB_USER -d $DB_POSTGRESDB_DATABASE -f /tmp/dump.sql || {
echo "Failed to import dump file."; exit 1;
}
fi
echo "Database import completed successfully."
else
echo "Skipping database import."
fi
|