-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_failover.sh
More file actions
executable file
·75 lines (63 loc) · 2.17 KB
/
test_failover.sh
File metadata and controls
executable file
·75 lines (63 loc) · 2.17 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
#!/bin/bash
# test_failover.sh
echo "=== Manual Failover Test ==="
echo "WARNING: This will trigger a failover!"
read -p "Continue? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
DB1_IP="10.116.238.92"
DB2_IP="10.116.238.25"
DB3_IP="10.116.238.228"
# Find current master
current_master=""
for ip in "$DB1_IP" "$DB2_IP" "$DB3_IP"; do
role=$(PGPASSWORD=postgres psql -h "$ip" -U postgres -d postgres -t -c "SELECT CASE WHEN pg_is_in_recovery() THEN 'replica' ELSE 'master' END;" 2>/dev/null | tr -d '[:space:]')
if [ "$role" = "master" ]; then
current_master="$ip"
break
fi
done
echo "Current master: $current_master"
# Create test data
echo "Creating test data before failover..."
PGPASSWORD=postgres psql -h "$current_master" -U postgres -d postgres -c "
CREATE TABLE IF NOT EXISTS failover_test (
id SERIAL PRIMARY KEY,
data TEXT,
created_at TIMESTAMP DEFAULT NOW()
);
INSERT INTO failover_test (data) VALUES ('Before failover $(date)');
"
# Get target for switchover (not current master)
target=""
for ip in "$DB1_IP" "$DB2_IP" "$DB3_IP"; do
if [ "$ip" != "$current_master" ]; then
target="$ip"
break
fi
done
# Trigger failover
echo "Triggering failover to $target..."
ssh postgres@"$current_master" "patronictl -c /etc/patroni/patroni.yml switchover --master $current_master --candidate $(echo $target | sed 's/.*\.//') --force" || echo "Failover command executed"
# Wait and check
echo "Waiting for failover to complete..."
sleep 15
# Find new master
new_master=""
for ip in "$DB1_IP" "$DB2_IP" "$DB3_IP"; do
role=$(PGPASSWORD=postgres psql -h "$ip" -U postgres -d postgres -t -c "SELECT CASE WHEN pg_is_in_recovery() THEN 'replica' ELSE 'master' END;" 2>/dev/null | tr -d '[:space:]')
if [ "$role" = "master" ]; then
new_master="$ip"
break
fi
done
echo "New master: $new_master"
# Test write to new master
echo "Testing write to new master..."
PGPASSWORD=postgres psql -h "$new_master" -U postgres -d postgres -c "
INSERT INTO failover_test (data) VALUES ('After failover $(date)');
SELECT * FROM failover_test ORDER BY id;
"
echo "Failover test completed!"