|
#!/bin/bash |
|
|
|
|
|
DUMP_URL="https://mysite.site/backups.dump" |
|
DUMP_FILE="/tmp/backups.dump" |
|
DB_NAME="$POSTGRES_DB" |
|
DB_USER="$POSTGRES_USER" |
|
|
|
|
|
if [ "$DB_IMPORT" = "yes" ]; then |
|
echo "开始导入数据库..." |
|
|
|
|
|
echo "正在下载数据库备份文件..." |
|
if curl -f -o "$DUMP_FILE" "$DUMP_URL"; then |
|
echo "下载成功." |
|
else |
|
echo "下载失败. 退出导入过程." |
|
exit 1 |
|
fi |
|
|
|
|
|
if psql -lqt | cut -d \| -f 1 | grep -qw "$DB_NAME"; then |
|
echo "数据库 $DB_NAME 已存在. 删除现有数据库..." |
|
dropdb -U "$DB_USER" "$DB_NAME" |
|
fi |
|
|
|
|
|
echo "创建新的数据库 $DB_NAME..." |
|
createdb -U "$DB_USER" "$DB_NAME" |
|
|
|
|
|
echo "正在导入数据到 $DB_NAME..." |
|
if pg_restore -U "$DB_USER" -d "$DB_NAME" "$DUMP_FILE"; then |
|
echo "数据导入成功." |
|
else |
|
echo "数据导入失败." |
|
exit 1 |
|
fi |
|
|
|
|
|
rm -f "$DUMP_FILE" |
|
|
|
echo "数据库导入过程完成." |
|
else |
|
echo "跳过数据库导入 (DB_IMPORT 不是 'yes')." |
|
fi |