This repository was archived by the owner on May 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestoredb.sh
More file actions
executable file
·124 lines (101 loc) · 3.75 KB
/
Copy pathrestoredb.sh
File metadata and controls
executable file
·124 lines (101 loc) · 3.75 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/bash
# Version: 1.0.1
# Get script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Load environment variables
if [[ ! -f "${SCRIPT_DIR}/.env" ]]; then
echo "ERROR: Environment file .env not found."
exit 1
fi
source "${SCRIPT_DIR}/.env"
# Setup basic configuration
DB_NAME=$DB_NAME
DB_USER=$DB_USER
DB_HOST=$DB_HOST
DB_PORT=$DB_PORT
export PGPASSWORD="$DB_PASSWD"
BACKUP_DIR=${BACKUP_DIR:-"dbBackups"}
MONTHLY_BACKUP_DIR=${BACKUP_DIR_MONTHLY:-"dbBackups/monthly"}
REMOTE_NAME=${REMOTE_NAME:-"gdrive"}
# Main restore function
restore_database() {
local backup_file="$1"
echo -e "\n🔄 Restoring database from backup file: $(basename "$backup_file")\n"
# Check if database exists, create if not
if ! psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -lqt | cut -d \| -f 1 | grep -qw "$DB_NAME"; then
echo -e "📦 Creating database $DB_NAME...\n"
createdb -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" "$DB_NAME" || {
echo "❌ Failed to create database."
return 1
}
else
# Terminate existing connections
echo -e "🔌 Disconnecting existing users...\n"
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -c "
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '$DB_NAME'
AND pid <> pg_backend_pid();" postgres >/dev/null
fi
# Restore database using pg_restore
echo "⏳ Restoring data... (this may take a while)"
if pg_restore -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" --clean --if-exists "$backup_file"; then
echo -e "\n✅ Database restored successfully!"
return 0
else
echo -e "\n❌ Failed to restore database."
return 1
fi
}
# Simple interactive menu
simple_restore_menu() {
echo -e "\n📂 Select backup source:"
echo "1) Daily backups"
echo "2) Monthly backups"
echo -e "q) Quit\n"
read -rp "Enter choice [1/2/q]: " choice
case "$choice" in
1) backup_dir="$BACKUP_DIR" ;;
2) backup_dir="$MONTHLY_BACKUP_DIR" ;;
q|Q) echo "Exiting."; exit 0 ;;
*) echo "Invalid choice"; exit 1 ;;
esac
echo -e "\n📋 Retrieving available backups...\n"
# Get list of backups and save as array and use mapfile to read into array
mapfile -t backups < <(rclone ls "$REMOTE_NAME:$backup_dir" | grep -E '\.pgdump$' | awk '{print $2}')
if [[ ${#backups[@]} -eq 0 ]]; then
echo "❌ No backups found in $backup_dir"
exit 0
fi
clear
echo -e "\n📂 Available backups (${#backups[@]} found):"
for i in "${!backups[@]}"; do
echo "$((i+1))) ${backups[$i]}"
done
echo ""
read -rp "Select backup to restore [1-${#backups[@]}]: " backup_num
if [[ ! "$backup_num" =~ ^[0-9]+$ ]] || [ "$backup_num" -lt 1 ] || [ "$backup_num" -gt "${#backups[@]}" ]; then
echo "❌ Invalid selection"
exit 1
fi
selected_backup="${backups[$((backup_num-1))]}"
echo -e "\n📥 Downloading backup: $selected_backup"
temp_file="/tmp/$(basename "$selected_backup")"
if ! rclone copy "$REMOTE_NAME:$backup_dir/$selected_backup" "/tmp/"; then
echo "❌ Failed to download backup file."
exit 1
fi
echo -e "\n⚠️ WARNING: This will overwrite the current database ($DB_NAME)!"
read -rp "Are you sure you want to continue? (yes/no): " confirm
if [[ "$confirm" == "yes" ]]; then
restore_database "$temp_file"
rm -f "$temp_file"
else
echo "Restore canceled."
rm -f "$temp_file"
fi
}
# Main script
echo -e "\n📊 Database Restore Tool"
echo "=============================="
simple_restore_menu