-
Notifications
You must be signed in to change notification settings - Fork 0
/
koha-backup-local
executable file
·178 lines (146 loc) · 5.28 KB
/
koha-backup-local
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
#!/bin/bash
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root or by a user with sudo privileges" 1>&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
# Make sure the files we create are not accessible by anyone else.
# umask 0077
# Default values
quiet="no"
exclude_indexes="no"
without_db_name="no"
schema_only="no"
instances=()
backupdir="/tmp" # Set default backup directory here
excludes=""
usage() {
local scriptname=$(basename "$0")
cat <<EOF
$scriptname
This script dumps your Koha instance data to a local directory for backup or migration.
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] [--without-db-name][--schema-only] [--backup-dir|-b backup_path] [-i instance1 [instance2...]]
$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
--backup-dir|-b Specify the path to save the backup files
Example: $scriptname --backup-dir /path/to/backup -i instance1
-i One or more Koha instance names to dump (separated by spaces)
Example: $scriptname -i instance1 instance2
--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" )"
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"
mysqldump $dbflag --no-tablespaces -d --host="$mysqlhost" --single-transaction --user="$mysqluser" --password="$mysqlpass" "$mysqldb" | sed --expression='s/ AUTO_INCREMENT=[0-9]+//' > "$schemadump"
else
[ "$quiet" = "no" ] && echo "* DB to $dbdump"
mysqldump $dbflag --no-tablespaces --host="$mysqlhost" --single-transaction --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 / $excludes \
"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) "$dbdump" "$metadump"
[ "$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 ;;
--backup-dir|-b)
if [ -z "${2:-}" ]; then
echo "Error: Backup directory path cannot be empty" >&2
usage
exit 1
else
backupdir="$2"
shift 2
fi ;;
-i)
shift
while [ $# -gt 0 ] && [[ "$1" != "" ]] && ! [[ "$1" =~ ^- ]]; do
instances+=("$1")
shift
done ;;
-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"
echo "The database and config files for $name have been saved to $dbdump and $metadump respectively."
done
exit 0