|
#!/bin/bash |
|
|
|
|
|
DUMP_FILE="/tmp/backups.dump" |
|
DB_NAME="$DB_POSTGRESDB_DATABASE" |
|
DB_USER="$DB_POSTGRESDB_USER" |
|
DB_PASS="$DB_POSTGRESDB_PASSWORD" |
|
WEBDAV_URL="$WEBDAV_URL" |
|
WEBDAV_USER="$WEBDAV_USER" |
|
WEBDAV_PASSWORD="$WEBDAV_PASSWORD" |
|
DB_IMPORT="$DB_IMPORT" |
|
|
|
|
|
if [ "$DB_IMPORT" = "yes" ]; then |
|
echo "开始导入数据库..." |
|
|
|
|
|
echo "正在下载数据库备份文件..." |
|
if curl -u "$WEBDAV_USER:$WEBDAV_PASSWORD" -f -o "$DUMP_FILE" "${WEBDAV_URL}backup.dump"; then |
|
echo "下载成功." |
|
else |
|
echo "下载失败. 无法从 ${WEBDAV_URL}backup.dump 获取数据库备份文件.用于认证的用户名为$WEBDAV_USER" |
|
echo "跳过数据库导入流程..." |
|
exit 1 |
|
fi |
|
|
|
|
|
echo "检查数据库是否存在..." |
|
if psql -U "$DB_USER" -lqt | cut -d \| -f 1 | grep -qw "$DB_NAME"; then |
|
echo "数据库 ${DB_NAME} 已存在,准备删除现有数据库..." |
|
if dropdb -U "$DB_USER" "$DB_NAME"; then |
|
echo "数据库 ${DB_NAME} 已删除." |
|
else |
|
echo "删除数据库失败,退出." |
|
exit 1 |
|
fi |
|
else |
|
echo "数据库 ${DB_NAME} 不存在,将创建新数据库." |
|
fi |
|
|
|
|
|
echo "创建新的数据库 ${DB_NAME}..." |
|
if createdb -U "$DB_USER" "$DB_NAME"; then |
|
echo "数据库 ${DB_NAME} 创建成功." |
|
else |
|
echo "创建数据库失败,退出." |
|
exit 1 |
|
fi |
|
|
|
|
|
echo "正在导入数据到 ${DB_NAME}..." |
|
if pg_restore -U "$DB_USER" -d "$DB_NAME" "$DUMP_FILE"; then |
|
echo "数据导入成功." |
|
else |
|
echo "数据导入失败." |
|
exit 1 |
|
fi |
|
|
|
|
|
echo "清理临时文件..." |
|
rm -f "$DUMP_FILE" |
|
|
|
echo "数据库导入过程完成." |
|
else |
|
echo "跳过数据库导入 (DB_IMPORT 不是 'yes')." |
|
fi |
|
|