-
Notifications
You must be signed in to change notification settings - Fork 0
/
koha-backup-config-s3
executable file
·209 lines (178 loc) · 6.19 KB
/
koha-backup-config-s3
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/bin/bash
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root or by a user with sudo privileges" >&2
exit 1
fi
set -euo pipefail
# Include helper functions
if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
. "/usr/share/koha/bin/koha-functions.sh"
else
echo "Error: /usr/share/koha/bin/koha-functions.sh not present." >&2
exit 1
fi
# Default values
quiet="no"
exclude_indexes="no"
without_db_name="no"
schema_only="no"
instances=()
s3_url=""
excludes=""
usage() {
local scriptname=$(basename "$0")
cat <<EOF
$scriptname
This script dumps your Koha instance data to an Amazon S3 bucket for backup or migration. It requires the awscli to be installed and configured.
The schema only option can be used to compare your existing database schema to the expected Koha structure. If you specify this option, the configuration files will be skipped from the backup.
Usage:
$scriptname [--quiet|-q] [--exclude-indexes] [--schema-only] [--without-db-name] [-i instance1 [instance2...]] [-s s3://bucket-name]
$scriptname -h|--help
Options:
--schema-only Dump only the database schema
Example: $scriptname --schema-only -i instance1
--exclude-indexes Exclude Zebra indexes from the backup
Example: $scriptname --exclude-indexes -i instance1
--without-db-name Do not include the database name in the dump
Example: $scriptname --without-db-name -i instance1
-i One or more Koha instance names to dump (separated by spaces)
Example: $scriptname -i instance1 instance2
-s URL of the S3 bucket to copy the backups
Example: $scriptname -i instance1 -s s3://bucket-name
--quiet|-q Make the script avoid printing to STDOUT
(useful for calling from another script)
Example: $scriptname -q -i instance1
--help|-h Display this help message
Example: $scriptname -h
EOF
}
dump_instance() {
local name="$1"
kohaconfig="/etc/koha/sites/$name/koha-conf.xml"
date=$(date +"%m-%d-%Y-%Hh%M")
[ "$quiet" = "no" ] && echo "Dumping Koha site $name:"
# Dump database.
mysqlhost="$(sudo xmlstarlet sel -t -v 'yazgfs/config/hostname' "$kohaconfig")"
mysqldb="$(sudo xmlstarlet sel -t -v 'yazgfs/config/database' "$kohaconfig")"
mysqluser="$(sudo xmlstarlet sel -t -v 'yazgfs/config/user' "$kohaconfig")"
mysqlpass="$(sudo xmlstarlet sel -t -v 'yazgfs/config/pass' "$kohaconfig")"
backupdir=/tmp
[ -z "$backupdir" ] && backupdir="/var/spool/koha/$name"
dbdump="$backupdir/$name-$date.sql.gz"
dbflag="--databases"
[ "$without_db_name" = "yes" ] && dbflag=""
if [ "$schema_only" = "yes" ]; then
schemadump="$backupdir/$name-schema-$date.sql"
[ "$quiet" = "no" ] && echo "* schema to $schemadump"
# See:
# https://bugs.mysql.com/bug.php?id=109685#c530123
# https://repost.aws/knowledge-center/mysqldump-error-rds-mysql-mariadbhttps://snapshooter.com/docs/fixing-mysqldump-couldn-t-execute-flush-tables-access-denied
# mysqldump $dbflag --no-tablespaces -d --host="$mysqlhost" --single-transaction --user="$mysqluser" --password="$mysqlpass" "$mysqldb" | sed --expression='s/ AUTO_INCREMENT=[0-9]+//' >"$schemadump"
# Set --column-statistics=0 to fix mysqldump unknown table column_statistics error in RDS
# mysqldump $dbflag --no-tablespaces --set-gtid-purged=OFF --column-statistics=0 -d --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpass" "$mysqldb" | sed --expression='s/ AUTO_INCREMENT=[0-9]+//' >"$schemadump"
else
# [ "$quiet" = "no" ] && echo "* DB to $dbdump"
# See:
# https://stackoverflow.com/questions/52423595/mysqldump-couldnt-execute-unknown-table-column-statistics-in-information-sc
# https://bugs.mysql.com/bug.php?id=109685#c530123
# https://repost.aws/knowledge-center/mysqldump-error-rds-mysql-mariadb
# https://snapshooter.com/docs/fixing-mysqldump-couldn-t-execute-flush-tables-access-denied
# mysqldump $dbflag --no-tablespaces --host="$mysqlhost" --single-transaction --user="$mysqluser" --password="$mysqlpass" "$mysqldb" | gzip >"$dbdump"
# mysqldump $dbflag --no-tablespaces --set-gtid-purged=OFF --column-statistics=0 --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpass" "$mysqldb" | gzip >"$dbdump"
instancefile="$name.conf"
# Dump configs, logs, etc.
metadump="$backupdir/$name-$date.tar.gz"
[ "$quiet" = "no" ] && echo "* configs, logs to $metadump"
if [ "$exclude_indexes" = "yes" ]; then
excludes="--exclude=var/lib/koha/$name/biblios \
--exclude=var/lib/koha/$name/authorities"
fi
sudo tar -czf "$metadump" -C / --ignore-failed-read $excludes \
--exclude="var/log/koha/$name/plack.log" \
"etc/koha/sites/$name" \
"etc/apache2/sites-available/$instancefile" \
"etc/apache2/sites-enabled/$instancefile" \
"var/lib/koha/$name" \
"var/log/koha/$name"
# Make the dump files readable and writeable by the current user.
sudo chown $(whoami):$(whoami) "$metadump"
if [ -n "$s3_url" ]; then
[ "$quiet" = "no" ] && echo "* copying backups to S3 bucket: $s3_url"
# aws s3 cp "$dbdump" "$s3_url/$name/$name-$date.sql.gz"
aws s3 cp "$metadump" "$s3_url/$name/$name-$date.tar.gz"
# Clean up the database, configuration dumps
sudo rm "$metadump"
else
usage
exit 1
fi
[ "$quiet" = "no" ] && echo "Done."
fi
}
if [ $# -eq 0 ]; then
usage
exit 1
fi
while [ $# -gt 0 ]; do
case "$1" in
--schema-only)
schema_only="yes"
shift
;;
--exclude-indexes)
exclude_indexes="yes"
shift
;;
--without-db-name)
without_db_name="yes"
shift
;;
-i)
shift
while [ $# -gt 0 ] && [[ "$1" != "" ]] && ! [[ "$1" =~ ^- ]]; do
instances+=("$1")
shift
done
;;
-s)
shift
s3_url="$1"
if [ -z "$s3_url" ] || [[ -z "$1" ]]; then
usage
exit 1
fi
shift
;;
-h | --help)
usage
exit 0
;;
-q | --quiet)
quiet="yes"
shift
;;
-*)
echo "Error: invalid option switch ("$1")" >&2
usage
exit 1
;;
*)
# We expect the remaining stuff are the instance names
instances+=("$1")
shift
;;
esac
done
if [ "${#instances[@]}" -eq 0 ]; then
echo "Error: you must provide at least one instance name" >&2
usage
exit 1
fi
for name in "${instances[@]}"; do
if ! is_instance "$name"; then
echo "Error: Invalid instance name $name" >&2
exit 1
fi
dump_instance "$name"
done
exit 0