forked from GeneBO98/tradetally
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollback-postgres-15.sh
More file actions
executable file
·145 lines (116 loc) · 5.03 KB
/
rollback-postgres-15.sh
File metadata and controls
executable file
·145 lines (116 loc) · 5.03 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/bash
# PostgreSQL 16 to 15 Rollback Script for TradeTally
# This script rolls back from PostgreSQL 16 to 15 using backups
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration - auto-detect compose file
if [[ -f "docker-compose.yaml" ]]; then
DOCKER_COMPOSE_FILE="docker-compose.yaml"
elif [[ -f "docker-compose.yml" ]]; then
DOCKER_COMPOSE_FILE="docker-compose.yml"
else
DOCKER_COMPOSE_FILE="docker-compose.dev.yaml"
fi
# Allow override via environment variable
DOCKER_COMPOSE_FILE="${COMPOSE_FILE:-$DOCKER_COMPOSE_FILE}"
echo -e "${BLUE}[INFO] Using compose file: $DOCKER_COMPOSE_FILE${NC}"
BACKUP_DIR="./postgres-migration-backup"
echo -e "${BLUE}=== TradeTally PostgreSQL 16 to 15 Rollback ===${NC}"
echo -e "${YELLOW}[WARNING] This will roll back your database to PostgreSQL 15${NC}"
echo -e "${YELLOW}[WARNING] Any data changes since migration will be lost${NC}"
echo ""
# Check if backup directory exists
if [[ ! -d "$BACKUP_DIR" ]]; then
echo -e "${RED}[ERROR] Backup directory not found: $BACKUP_DIR${NC}"
echo -e "${RED}[ERROR] Cannot perform rollback without backups${NC}"
exit 1
fi
# List available backups
echo -e "${BLUE}[INFO] Available backups:${NC}"
ls -la "$BACKUP_DIR"
# Find the most recent SQL backup
SQL_BACKUP=$(ls -t "$BACKUP_DIR"/tradetally_backup_*.sql 2>/dev/null | head -1)
VOLUME_BACKUP=$(ls -t "$BACKUP_DIR"/postgres_volume_backup_*.tar.gz 2>/dev/null | head -1)
if [[ -z "$SQL_BACKUP" ]]; then
echo -e "${RED}[ERROR] No SQL backup found in $BACKUP_DIR${NC}"
exit 1
fi
if [[ -z "$VOLUME_BACKUP" ]]; then
echo -e "${RED}[ERROR] No volume backup found in $BACKUP_DIR${NC}"
exit 1
fi
echo -e "${BLUE}[INFO] Using SQL backup: $(basename "$SQL_BACKUP")${NC}"
echo -e "${BLUE}[INFO] Using volume backup: $(basename "$VOLUME_BACKUP")${NC}"
# Prompt for confirmation
read -p "Do you want to continue with the rollback? (y/N): " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo -e "${YELLOW}[CANCELLED] Rollback cancelled by user.${NC}"
exit 0
fi
echo -e "${BLUE}[INFO] Starting PostgreSQL rollback process...${NC}"
# Step 1: Stop current containers
echo -e "${BLUE}[STEP 1/5] Stopping current containers...${NC}"
docker-compose -f "$DOCKER_COMPOSE_FILE" down
# Determine volume name based on compose file
if [[ "$DOCKER_COMPOSE_FILE" == *"dev"* ]]; then
VOLUME_NAME="tradetally_postgres_data_dev"
else
VOLUME_NAME="tradetally_postgres_data"
fi
echo -e "${BLUE}[INFO] Using volume: $VOLUME_NAME${NC}"
# Step 2: Remove PostgreSQL 16 data volume
echo -e "${BLUE}[STEP 2/5] Removing PostgreSQL 16 data volume...${NC}"
docker volume rm "$VOLUME_NAME" || true
# Step 3: Restore PostgreSQL 15 volume backup
echo -e "${BLUE}[STEP 3/5] Restoring PostgreSQL 15 volume...${NC}"
docker run --rm -v "$VOLUME_NAME":/target -v "$(pwd)/$BACKUP_DIR":/backup alpine tar xzf "/backup/$(basename "$VOLUME_BACKUP")" -C /target
if [[ $? -eq 0 ]]; then
echo -e "${GREEN}[SUCCESS] PostgreSQL 15 volume restored${NC}"
else
echo -e "${RED}[ERROR] Failed to restore PostgreSQL 15 volume!${NC}"
exit 1
fi
# Step 4: Update docker-compose to use PostgreSQL 15
echo -e "${BLUE}[STEP 4/5] Updating docker-compose to use PostgreSQL 15...${NC}"
sed -i.bak 's/postgres:16-alpine/postgres:15-alpine/g' "$DOCKER_COMPOSE_FILE"
# Step 5: Start PostgreSQL 15
echo -e "${BLUE}[STEP 5/5] Starting PostgreSQL 15...${NC}"
docker-compose -f "$DOCKER_COMPOSE_FILE" up -d
# Wait for PostgreSQL to be ready
echo -e "${BLUE}[INFO] Waiting for PostgreSQL 15 to be ready...${NC}"
for i in {1..30}; do
if docker-compose -f "$DOCKER_COMPOSE_FILE" exec -T postgres pg_isready -U trader -d tradetally; then
echo -e "${GREEN}[SUCCESS] PostgreSQL 15 is ready${NC}"
break
fi
if [[ $i -eq 30 ]]; then
echo -e "${RED}[ERROR] PostgreSQL 15 failed to start within 30 seconds${NC}"
exit 1
fi
echo -e "${YELLOW}[INFO] Waiting for PostgreSQL... (${i}/30)${NC}"
sleep 1
done
# Verify the rollback
echo -e "${BLUE}[INFO] Verifying rollback...${NC}"
sleep 5
# Check PostgreSQL version
PG_VERSION=$(docker-compose -f "$DOCKER_COMPOSE_FILE" exec -T postgres psql -U trader -d tradetally -t -c "SELECT version();" | head -1 | xargs)
echo -e "${BLUE}[INFO] Current PostgreSQL version: $PG_VERSION${NC}"
# Check if we can connect and query data
TRADE_COUNT=$(docker-compose -f "$DOCKER_COMPOSE_FILE" exec -T postgres psql -U trader -d tradetally -t -c "SELECT COUNT(*) FROM trades;" 2>/dev/null | xargs || echo "0")
echo -e "${BLUE}[INFO] Trade count in database: $TRADE_COUNT${NC}"
# Clean up backup file from docker-compose
rm -f "$DOCKER_COMPOSE_FILE.bak"
echo ""
echo -e "${GREEN}=== ROLLBACK COMPLETED SUCCESSFULLY ===${NC}"
echo -e "${GREEN}[SUCCESS] PostgreSQL has been rolled back to version 15${NC}"
echo -e "${BLUE}[INFO] Your data has been restored from the backup${NC}"
echo -e "${YELLOW}[INFO] Backup files are still available in: $BACKUP_DIR${NC}"
echo ""
echo -e "${GREEN}[COMPLETE] Rollback process finished!${NC}"