forked from btcpayserver/btcpayserver-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btcpay-restore.sh
executable file
·182 lines (157 loc) · 5.01 KB
/
btcpay-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
181
182
#!/bin/bash -e
set -o pipefail -o errexit
if [ "$(id -u)" != "0" ]; then
printf "\n🚨 This script must be run as root.\n"
printf "➡️ Use the command 'sudo su -' (include the trailing hypen) and try again.\n\n"
exit 1
fi
backup_path=$1
if [ -z "$backup_path" ]; then
printf "\nℹ️ Usage: btcpay-restore.sh /path/to/backup.tar.gz\n\n"
exit 1
fi
if [ ! -f "$backup_path" ]; then
printf "\n🚨 $backup_path does not exist.\n\n"
exit 1
fi
if [[ "$backup_path" == *.gpg && -z "$BTCPAY_BACKUP_PASSPHRASE" ]]; then
printf "\n🔐 $backup_path is encrypted. Please provide the passphrase to decrypt it."
printf "\nℹ️ Usage: BTCPAY_BACKUP_PASSPHRASE=t0pSeCrEt btcpay-restore.sh /path/to/backup.tar.gz.gpg\n\n"
exit 1
fi
# preparation
docker_dir=$(docker volume inspect generated_btcpay_datadir --format="{{.Mountpoint}}" | sed -e "s%/volumes/.*%%g")
restore_dir="$docker_dir/volumes/backup_datadir/_data/restore"
postgres_dump_name=postgres.sql.gz
btcpay_dir="$BTCPAY_BASE_DIRECTORY/btcpayserver-docker"
# ensure clean restore dir
printf "\nℹ️ Cleaning restore directory $restore_dir …\n\n"
rm -rf $restore_dir
mkdir -p $restore_dir
if [[ "$backup_path" == *.gpg ]]; then
echo "🔐 Decrypting backup file …"
{
gpg -o "${backup_path%.*}" --batch --yes --passphrase "$BTCPAY_BACKUP_PASSPHRASE" -d $backup_path
backup_path="${backup_path%.*}"
printf "✅ Decryption done.\n\n"
} || {
echo "🚨 Decryption failed. Please check the error message above."
exit 1
}
fi
cd $restore_dir
echo "ℹ️ Extracting files in $(pwd) …"
tar -xvf $backup_path -C $restore_dir
# basic control checks
if [ ! -f "$postgres_dump_name" ]; then
printf "\n🚨 $postgres_dump_name does not exist.\n\n"
exit 1
fi
if [ ! -d "volumes" ]; then
printf "\n🚨 volumes directory does not exist.\n\n"
exit 1
fi
if [ -f "mariadb.sql.gz" ]; then
mariadb_dump_name=mariadb.sql.gz
fi
cd $btcpay_dir
. helpers.sh
printf "\nℹ️ Stopping BTCPay Server …\n\n"
btcpay_down
cd $restore_dir
{
printf "\nℹ️ Restoring volumes …\n"
# ensure volumes dir exists
if [ ! -d "$docker_dir/volumes" ]; then
mkdir -p $docker_dir/volumes
fi
# copy volume directories over
cp -r volumes/* $docker_dir/volumes/
# ensure datadirs excluded in backup exist
mkdir -p $docker_dir/volumes/generated_postgres_datadir/_data
if [ ! -z "$mariadb_dump_name" ]; then
mkdir -p $docker_dir/volumes/generated_mariadb_datadir/_data
fi
echo "✅ Volume restore done."
} || {
echo "🚨 Restoring volumes failed. Please check the error message above."
printf "\nℹ️ Restarting BTCPay Server …\n\n"
cd $btcpay_dir
btcpay_up
exit 1
}
# Start Postgres database
{
printf "\nℹ️ Starting Postgres database container …\n"
docker-compose -f $BTCPAY_DOCKER_COMPOSE up -d postgres
sleep 10
postgres_container=$(docker ps -a -q -f "name=postgres_1")
if [ -z "$postgres_container" ]; then
echo "🚨 Postgres database container could not be started or found."
printf "\nℹ️ Restarting BTCPay Server …\n\n"
cd $btcpay_dir
btcpay_up
exit 1
fi
} || {
echo "🚨 Starting Postgres database container failed. Please check the error message above."
printf "\nℹ️ Restarting BTCPay Server …\n\n"
cd $btcpay_dir
btcpay_up
exit 1
}
# Optional: Start MariaDB database
if [ ! -z "$mariadb_dump_name" ]; then
{
printf "\nℹ️ Starting MariaDB database container …\n"
docker-compose -f $BTCPAY_DOCKER_COMPOSE up -d mariadb
sleep 10
mariadb_container=$(docker ps -a -q -f "name=mariadb_1")
if [ -z "$mariadb_container" ]; then
echo "🚨 MariaDB database container could not be started or found."
printf "\nℹ️ Restarting BTCPay Server …\n\n"
cd $btcpay_dir
btcpay_up
exit 1
fi
} || {
echo "🚨 Starting MariaDB database container failed. Please check the error message above."
printf "\nℹ️ Restarting BTCPay Server …\n\n"
cd $btcpay_dir
btcpay_up
exit 1
}
fi
cd $restore_dir
# Postgres database
{
printf "\nℹ️ Restoring Postgres database …"
gunzip -c $postgres_dump_name | docker exec -i $postgres_container psql -U postgres postgres
echo "✅ Postgres database restore done."
} || {
echo "🚨 Restoring Postgres database failed. Please check the error message above."
printf "\nℹ️ Restarting BTCPay Server …\n\n"
cd $btcpay_dir
btcpay_up
exit 1
}
# Optional: MariaDB database
if [ ! -z "$mariadb_dump_name" ]; then
{
printf "\nℹ️ Restoring MariaDB database …"
gunzip -c $mariadb_dump_name | docker exec -i $mariadb_container mysql -u root -pwordpressdb
printf "\n✅ MariaDB database restore done."
} || {
echo "🚨 Restoring MariaDB database failed. Please check the error message above."
printf "\nℹ️ Restarting BTCPay Server …\n\n"
cd $btcpay_dir
btcpay_up
exit 1
}
fi
printf "\nℹ️ Restarting BTCPay Server …\n\n"
cd $btcpay_dir
btcpay_up
printf "\nℹ️ Cleaning up …\n\n"
rm -rf $restore_dir
printf "✅ Restore done\n\n"