Create import-db.sh
Browse files- import-db.sh +47 -0
import-db.sh
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
|
3 |
+
# 设置变量
|
4 |
+
DUMP_URL="https://mysite.site/backups.dump"
|
5 |
+
DUMP_FILE="/tmp/backups.dump"
|
6 |
+
DB_NAME="$POSTGRES_DB"
|
7 |
+
DB_USER="$POSTGRES_USER"
|
8 |
+
|
9 |
+
# 检查是否需要导入数据库
|
10 |
+
if [ "$DB_IMPORT" = "yes" ]; then
|
11 |
+
echo "开始导入数据库..."
|
12 |
+
|
13 |
+
# 下载 dump 文件
|
14 |
+
echo "正在下载数据库备份文件..."
|
15 |
+
if curl -f -o "$DUMP_FILE" "$DUMP_URL"; then
|
16 |
+
echo "下载成功."
|
17 |
+
else
|
18 |
+
echo "下载失败. 退出导入过程."
|
19 |
+
exit 1
|
20 |
+
fi
|
21 |
+
|
22 |
+
# 检查数据库是否存在
|
23 |
+
if psql -lqt | cut -d \| -f 1 | grep -qw "$DB_NAME"; then
|
24 |
+
echo "数据库 $DB_NAME 已存在. 删除现有数据库..."
|
25 |
+
dropdb -U "$DB_USER" "$DB_NAME"
|
26 |
+
fi
|
27 |
+
|
28 |
+
# 创建新的数据库
|
29 |
+
echo "创建新的数据库 $DB_NAME..."
|
30 |
+
createdb -U "$DB_USER" "$DB_NAME"
|
31 |
+
|
32 |
+
# 导入数据
|
33 |
+
echo "正在导入数据到 $DB_NAME..."
|
34 |
+
if pg_restore -U "$DB_USER" -d "$DB_NAME" "$DUMP_FILE"; then
|
35 |
+
echo "数据导入成功."
|
36 |
+
else
|
37 |
+
echo "数据导入失败."
|
38 |
+
exit 1
|
39 |
+
fi
|
40 |
+
|
41 |
+
# 删除临时文件
|
42 |
+
rm -f "$DUMP_FILE"
|
43 |
+
|
44 |
+
echo "数据库导入过程完成."
|
45 |
+
else
|
46 |
+
echo "跳过数据库导入 (DB_IMPORT 不是 'yes')."
|
47 |
+
fi
|