-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore.sh
executable file
·180 lines (146 loc) · 5.74 KB
/
restore.sh
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/bin/bash
# Function to detect and use the correct Docker Compose command
function get_docker_compose_command {
if command -v docker-compose >/dev/null 2>&1; then
echo "docker-compose"
else
echo "docker compose"
fi
}
DOCKER_COMPOSE=$(get_docker_compose_command)
# Check if the date is passed as a parameter
if [ -z "$1" ]; then
echo "Please provide a date as a parameter (format: YYYY-MM-DD)"
exit 1
fi
# Set the backup date
BACKUP_DATE=$1
COMBINED_BACKUP_FILE="solectrus-backup-$BACKUP_DATE.tar.gz"
# Check if .env file exists
if [ ! -f ".env" ]; then
echo "Error: .env file is missing. Please ensure the .env file is present."
exit 1
fi
# Check if Docker Compose configuration is valid
if ! $DOCKER_COMPOSE config >/dev/null 2>&1; then
echo "Error: Docker Compose configuration is invalid or missing. Please ensure your Docker Compose configuration is present and valid."
exit 1
fi
# Check if the combined backup file exists
if [ ! -f "$COMBINED_BACKUP_FILE" ]; then
echo "Backup file $COMBINED_BACKUP_FILE does not exist."
exit 1
fi
# Extract the combined tar.gz file to validate the content
echo "Validating backup files in $COMBINED_BACKUP_FILE..."
tar -tzf $COMBINED_BACKUP_FILE | grep "solectrus-postgresql-backup-$BACKUP_DATE.sql.gz" >/dev/null
PG_BACKUP_VALID=$?
tar -tzf $COMBINED_BACKUP_FILE | grep "solectrus-influxdb-backup-$BACKUP_DATE.tar.gz" >/dev/null
INFLUX_BACKUP_VALID=$?
# Check if both PostgreSQL and InfluxDB backups are present
if [ $PG_BACKUP_VALID -ne 0 ]; then
echo "PostgreSQL backup file is missing in the combined backup."
exit 1
fi
if [ $INFLUX_BACKUP_VALID -ne 0 ]; then
echo "InfluxDB backup file is missing in the combined backup."
exit 1
fi
echo "Ok, backup file contains backups for both PostgreSQL and InfluxDB."
echo
# Check if PostgreSQL, InfluxDB, and Redis are running
echo "Checking if PostgreSQL, InfluxDB, and Redis containers are running..."
# Check for 'postgresql' or 'db' service for PostgreSQL and remember the service name
POSTGRES_SERVICE=$($DOCKER_COMPOSE ps --services --filter "status=running" | grep -E '^(postgresql|db)$')
if [ -z "$POSTGRES_SERVICE" ]; then
echo "Error: PostgreSQL container is not running (neither as 'postgresql' nor 'db')."
exit 1
fi
if ! $DOCKER_COMPOSE ps --services --filter "status=running" | grep -q '^influxdb$'; then
echo "Error: InfluxDB container is not running."
exit 1
fi
if ! $DOCKER_COMPOSE ps --services --filter "status=running" | grep -q '^redis$'; then
echo "Error: Redis container is not running."
exit 1
fi
echo "Ok, PostgreSQL, InfluxDB, and Redis containers are all running."
echo
# Confirmation prompt
echo "WARNING: This will overwrite all existing data in PostgreSQL and InfluxDB!"
read -p "Are you sure you want to continue? Type 'yes' to proceed: " confirmation
if [ "$confirmation" != "yes" ]; then
echo "Restore process aborted."
exit 1
fi
echo
# Check all running containers except PostgreSQL, InfluxDB, and Redis
echo "Checking running containers..."
RUNNING_CONTAINERS=$($DOCKER_COMPOSE ps --services --filter "status=running" | grep -vE '^(postgresql|db|influxdb|redis)$')
echo "Stopping all containers except PostgreSQL, InfluxDB, and Redis..."
$DOCKER_COMPOSE stop $(echo "$RUNNING_CONTAINERS")
echo "All other containers stopped."
echo
# Extract the combined tar.gz file
echo "Extracting backup files from $COMBINED_BACKUP_FILE..."
tar -xzf $COMBINED_BACKUP_FILE
echo "Extraction completed."
echo
# PostgreSQL Restore
PG_BACKUP_FILE="solectrus-postgresql-backup-$BACKUP_DATE.sql.gz"
if [ -f "$PG_BACKUP_FILE" ]; then
echo "Restoring PostgreSQL backup..."
gunzip -c $PG_BACKUP_FILE | $DOCKER_COMPOSE exec -T $POSTGRES_SERVICE psql -U postgres -d solectrus_production >/dev/null
if [ $? -eq 0 ]; then
echo "PostgreSQL restore completed successfully."
else
echo "PostgreSQL restore failed."
fi
else
echo "PostgreSQL backup file $PG_BACKUP_FILE not found."
fi
echo
# InfluxDB Restore
INFLUX_BACKUP_FILE="solectrus-influxdb-backup-$BACKUP_DATE.tar.gz"
INFLUX_BACKUP_PATH=/tmp/solectrus-influxdb-backup-$BACKUP_DATE
INFLUX_TOKEN=$(grep '^INFLUX_ADMIN_TOKEN=' .env | cut -d '=' -f2-)
BUCKET_NAME=$(grep '^INFLUX_BUCKET=' .env | cut -d '=' -f2-)
ORG_NAME=$(grep '^INFLUX_ORG=' .env | cut -d '=' -f2-)
if [ -f "$INFLUX_BACKUP_FILE" ]; then
echo "Restoring InfluxDB backup..."
# Delete existing bucket before restoring
$DOCKER_COMPOSE exec influxdb influx bucket delete --name $BUCKET_NAME --org $ORG_NAME -t $INFLUX_TOKEN
# Copy and extract the backup
docker cp $INFLUX_BACKUP_FILE $($DOCKER_COMPOSE ps -q influxdb):/tmp/
$DOCKER_COMPOSE exec influxdb tar -xzf /tmp/solectrus-influxdb-backup-$BACKUP_DATE.tar.gz -C /tmp
# Restore the InfluxDB backup
$DOCKER_COMPOSE exec influxdb influx restore /tmp/solectrus-influxdb-backup-$BACKUP_DATE/ -t $INFLUX_TOKEN
# Cleanup after restore
$DOCKER_COMPOSE exec influxdb rm -rf /tmp/solectrus-influxdb-backup-$BACKUP_DATE /tmp/solectrus-influxdb-backup-$BACKUP_DATE.tar.gz
echo "InfluxDB restore completed."
else
echo "InfluxDB backup file $INFLUX_BACKUP_FILE not found."
fi
echo
# Optional: Cleanup extracted files
rm $PG_BACKUP_FILE
rm $INFLUX_BACKUP_FILE
# Redis flush
echo "Flushing Redis..."
$DOCKER_COMPOSE exec redis redis-cli FLUSHALL
if [ $? -eq 0 ]; then
echo "Redis cache flushed successfully."
else
echo "Redis flush failed."
exit 1
fi
echo
# Restart only previously running containers
echo "Restarting previously running containers..."
if [ -n "$RUNNING_CONTAINERS" ]; then
$DOCKER_COMPOSE start $(echo "$RUNNING_CONTAINERS")
echo "Previously running containers restarted."
else
echo "No containers were previously running."
fi
echo "Restore process completed."