-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_restore.sh
More file actions
executable file
·63 lines (51 loc) · 1.84 KB
/
db_restore.sh
File metadata and controls
executable file
·63 lines (51 loc) · 1.84 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash
# Usage: ./restore_from_drive.sh <db_name> <db_user> <db_password>
DOCKER_SERVICE_NAME="db"
DOCKER_DB_NAME="$1"
DOCKER_DB_USER="$2"
DOCKER_DB_PASSWORD="$3"
if [ -z "$3" ]; then
echo "Usage: $0 <db_name> <db_user> <db_password>"
exit 1
fi
# === CONFIGURATION ===
FILEID="1qKZwd1N9ie95ciM2jrxn5qMfzxDwm_Qu"
DUMP_URL="https://drive.usercontent.google.com/download?id=${FILEID}&confirm=t"
BACKUP_FILE_LOCAL="db/full_restore.dump"
BACKUP_FILE_IN_CONTAINER="/tmp/full_restore.dump"
# === Step 1: Download the dump using curl or wget ===
mkdir -p db
echo "⬇️ Downloading dump from $DUMP_URL..."
if command -v curl > /dev/null; then
wget --no-check-certificate "$DUMP_URL" -O "$BACKUP_FILE_LOCAL"
else
echo "❌ wget is available. Cannot download."
exit 1
fi
if [ $? -ne 0 ] || [ ! -f "$BACKUP_FILE_LOCAL" ]; then
echo "❌ Failed to download dump file."
exit 1
fi
echo "✅ Dump file downloaded to $BACKUP_FILE_LOCAL"
# === Step 2: Find container and copy the dump ===
DOCKER_CONTAINER_NAME=$(docker compose ps -q "$DOCKER_SERVICE_NAME")
if [ -z "$DOCKER_CONTAINER_NAME" ]; then
echo "❌ Docker container for service '$DOCKER_SERVICE_NAME' not found."
exit 1
fi
echo "📦 Copying dump file into container..."
docker cp "$BACKUP_FILE_LOCAL" "$DOCKER_CONTAINER_NAME:$BACKUP_FILE_IN_CONTAINER"
# === Step 3: Restore the dump ===
echo "🛠 Restoring database from dump..."
docker exec -e PGPASSWORD="$DOCKER_DB_PASSWORD" "$DOCKER_CONTAINER_NAME" \
pg_restore -x --no-owner -U "$DOCKER_DB_USER" -d "$DOCKER_DB_NAME" "$BACKUP_FILE_IN_CONTAINER"
if [ $? -ne 0 ]; then
echo "❌ Restore failed."
exit 1
fi
echo "✅ Restore completed successfully."
# === Step 4: Cleanup ===
echo "🧹 Cleaning up..."
rm -f "$BACKUP_FILE_LOCAL"
docker exec "$DOCKER_CONTAINER_NAME" rm -f "$BACKUP_FILE_IN_CONTAINER"
echo "✅ Cleanup done."