-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclickhouse-restore.sh
executable file
·43 lines (34 loc) · 1.37 KB
/
clickhouse-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
#!/usr/bin/env bash
#
# Bash script for performing restores of the ClickHouse database
# using clickhouse-client.
#
# The script checks for the existence and executability of clickhouse-client,
# as well as the presence of the backup directory. If conditions are met, it
# generates a backup query for the plausible_events_db database
set -eEu -o pipefail
backups_path="/app/data/clickhouse/backups"
# Check if clickhouse-client is installed
if ! command -v clickhouse-client &> /dev/null; then
echo "=> Unable to perform restore of clickhouse database"
echo " The clickhouse-client executable does not exist."
exit 1
fi
# Check if backup path is accessible
if [ ! -d "$backups_path" ]; then
echo "=> Unable to perform restore of clickhouse database."
echo "${backups_path} does not exist"
exit 1
fi
echo "=> Restoring clickhouse database with clickhouse-client"
# Retrieve a count of backups
counter=$(find "${backups_path}" -type f -name '*.zip' | wc --lines)
# Backup names are zero-indexed
latest_backup_name="plausible_events_db-$((counter - 1)).zip"
# Template query
query_str="DROP DATABASE IF EXISTS plausible_events_db; RESTORE DATABASE plausible_events_db FROM Disk('backups', '${latest_backup_name}');"
echo "${query_str}"
# Run the restore
/usr/bin/clickhouse-client \
--config-file /app/data/clickhouse-client.xml \
--multiquery "${query_str}"