AZLABS commited on
Commit
4da4818
·
verified ·
1 Parent(s): f991908

Update import-db.sh

Browse files
Files changed (1) hide show
  1. import-db.sh +27 -27
import-db.sh CHANGED
@@ -1,6 +1,6 @@
1
  #!/bin/bash
2
 
3
- # 设置变量
4
  DUMP_FILE="/tmp/backups.dump"
5
  DB_NAME="$DB_POSTGRESDB_DATABASE"
6
  DB_USER="$DB_POSTGRESDB_USER"
@@ -10,57 +10,57 @@ WEBDAV_USER="$WEBDAV_USER"
10
  WEBDAV_PASSWORD="$WEBDAV_PASSWORD"
11
  DB_IMPORT="$DB_IMPORT"
12
 
13
- # 检查是否需要导入数据库
14
  if [ "$DB_IMPORT" = "yes" ]; then
15
- echo "开始导入数据库..."
16
 
17
- # 下载 dump 文件
18
- echo "正在下载数据库备份文件..."
19
  if curl -u "$WEBDAV_USER:$WEBDAV_PASSWORD" -f -o "$DUMP_FILE" "${WEBDAV_URL}backup.dump"; then
20
- echo "下载成功."
21
  else
22
- echo "下载失败. 无法从 ${WEBDAV_URL}backup.dump 获取数据库备份文件.用于认证的用户名为$WEBDAV_USER"
23
- echo "跳过数据库导入流程..."
24
  exit 1
25
  fi
26
 
27
- # 检查数据库是否存在
28
- echo "检查数据库是否存在..."
29
  if psql -U "$DB_USER" -lqt | cut -d \| -f 1 | grep -qw "$DB_NAME"; then
30
- echo "数据库 ${DB_NAME} 已存在,准备删除现有数据库..."
31
  if dropdb -U "$DB_USER" "$DB_NAME"; then
32
- echo "数据库 ${DB_NAME} 已删除."
33
  else
34
- echo "删除数据库失败,退出."
35
  exit 1
36
  fi
37
  else
38
- echo "数据库 ${DB_NAME} 不存在,将创建新数据库."
39
  fi
40
 
41
- # 创建新的数据库
42
- echo "创建新的数据库 ${DB_NAME}..."
43
  if createdb -U "$DB_USER" "$DB_NAME"; then
44
- echo "数据库 ${DB_NAME} 创建成功."
45
  else
46
- echo "创建数据库失败,退出."
47
  exit 1
48
  fi
49
 
50
- # 导入数据
51
- echo "正在导入数据到 ${DB_NAME}..."
52
  if pg_restore -U "$DB_USER" -d "$DB_NAME" "$DUMP_FILE"; then
53
- echo "数据导入成功."
54
  else
55
- echo "数据导入失败."
56
  exit 1
57
  fi
58
 
59
- # 删除临时文件
60
- echo "清理临时文件..."
61
  rm -f "$DUMP_FILE"
62
 
63
- echo "数据库导入过程完成."
64
  else
65
- echo "跳过数据库导入 (DB_IMPORT 不是 'yes')."
66
- fi
 
1
  #!/bin/bash
2
 
3
+ # Set variables
4
  DUMP_FILE="/tmp/backups.dump"
5
  DB_NAME="$DB_POSTGRESDB_DATABASE"
6
  DB_USER="$DB_POSTGRESDB_USER"
 
10
  WEBDAV_PASSWORD="$WEBDAV_PASSWORD"
11
  DB_IMPORT="$DB_IMPORT"
12
 
13
+ # Check if database import is needed
14
  if [ "$DB_IMPORT" = "yes" ]; then
15
+ echo "Starting database import..."
16
 
17
+ # Download dump file
18
+ echo "Downloading database backup file..."
19
  if curl -u "$WEBDAV_USER:$WEBDAV_PASSWORD" -f -o "$DUMP_FILE" "${WEBDAV_URL}backup.dump"; then
20
+ echo "Download successful."
21
  else
22
+ echo "Download failed. Unable to retrieve database backup file from ${WEBDAV_URL}backup.dump. Authentication failed with username $WEBDAV_USER"
23
+ echo "Skipping database import process..."
24
  exit 1
25
  fi
26
 
27
+ # Check if database exists
28
+ echo "Checking if database exists..."
29
  if psql -U "$DB_USER" -lqt | cut -d \| -f 1 | grep -qw "$DB_NAME"; then
30
+ echo "Database $DB_NAME already exists, preparing to delete existing database..."
31
  if dropdb -U "$DB_USER" "$DB_NAME"; then
32
+ echo "Database $DB_NAME deleted."
33
  else
34
+ echo "Failed to delete database, exiting."
35
  exit 1
36
  fi
37
  else
38
+ echo "Database $DB_NAME does not exist, creating new database."
39
  fi
40
 
41
+ # Create new database
42
+ echo "Creating new database $DB_NAME..."
43
  if createdb -U "$DB_USER" "$DB_NAME"; then
44
+ echo "Database $DB_NAME created successfully."
45
  else
46
+ echo "Failed to create database, exiting."
47
  exit 1
48
  fi
49
 
50
+ # Import data
51
+ echo "Importing data into $DB_NAME..."
52
  if pg_restore -U "$DB_USER" -d "$DB_NAME" "$DUMP_FILE"; then
53
+ echo "Data import successful."
54
  else
55
+ echo "Data import failed."
56
  exit 1
57
  fi
58
 
59
+ # Remove temporary file
60
+ echo "Removing temporary file..."
61
  rm -f "$DUMP_FILE"
62
 
63
+ echo "Database import process complete."
64
  else
65
+ echo "Skipping database import (DB_IMPORT is not 'yes')."
66
+ fi