-
Notifications
You must be signed in to change notification settings - Fork 0
/
koha-backup-db-only
executable file
·136 lines (117 loc) · 3.53 KB
/
koha-backup-db-only
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
#!/bin/bash
set -euo pipefail
# Script version
VERSION="0.1.0"
# Set the name of the script to variable
SCRIPT_NAME="$(basename "${0}")"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
# Functions
# Help function
function help_message() {
cat <<EOF
Usage: ${SCRIPT_NAME} -i <instance_id> [-d <dump_directory>] [-h] [-v]
Options:
-i: The name of the koha instance. Can be specified multiple times.
-d: The path of the dump directory. If not specified, a temporary directory will be used.
-h: Print help message.
-v: Print version.
Examples:
${SCRIPT_NAME} -i library
${SCRIPT_NAME} -i library1 -i library2 -d /path/to/dump/directory
EOF
}
# Check if required packages are installed
check_requirements() {
local packages=(xmlstarlet mysqldump)
for package in "${packages[@]}"; do
if ! command -v "$package" &> /dev/null; then
echo -e "${RED}Error: $package is not installed.${NC}" >&2
exit 1
fi
done
}
# Parse command-line options
instance_ids=()
while [[ $# -gt 0 ]]; do
case $1 in
-i)
if [[ -z ${2:-} ]]; then
echo -e "${RED}Error: instance id is missing.${NC}" >&2
help_message >&2
exit 1
fi
instance_ids+=("$2")
shift
;;
-d)
if [[ -z ${2:-} ]]; then
echo -e "${RED}Error: dump directory path is missing.${NC}" >&2
help_message >&2
exit 1
fi
DUMP_DIR="$2"
shift
;;
-v|--version)
echo "${SCRIPT_NAME} version ${VERSION}"
exit 0
;;
-h|--help)
help_message
exit 0
;;
*)
echo "Error: Unknown option: $1" >&2
help_message >&2
exit 1
;;
esac
shift
done
# Check if instance_ids are set
if [[ ${#instance_ids[@]} -eq 0 ]]; then
echo -e "${RED}Error: you must specify at least one instance id.${NC}" >&2
help_message >&2
exit 1
fi
# Check if required packages are installed
check_requirements
# Set up temporary directory for SQL dump if not specified
if [[ -z ${DUMP_DIR+x} ]]; then
DUMP_DIR=$(mktemp -d)
fi
# Function to dump database
dump_db() {
local instance_name=$1
# Set the path to koha_conf.xml
koha_conf="/etc/koha/sites/${instance_name}/koha-conf.xml"
# Retrieve the Koha user, password, and database hostname
kohadbpwd=$(sudo xmlstarlet sel -t -v 'yazgfs/config/pass' "$koha_conf")
kohadbuser="koha_${instance_name}"
kohadbhost=$(sudo xmlstarlet sel -t -v 'yazgfs/config/hostname' "$koha_conf")
date=$(date +"%m-%d-%Y-%Hh%M")
# Dump Koha database
# 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 -h "$kohadbhost" -u "$kohadbuser" -p"$kohadbpwd" --databases --no-tablespaces --set-gtid-purged=OFF --column-statistics=0 "koha_${instance_name}" > "${DUMP_DIR}/koha_${instance_name}-$date.sql"
}
# Lock file to prevent concurrent execution
LOCK_FILE="/tmp/${SCRIPT_NAME}.lock"
(
flock -n 200 || {
echo "Script is already running"
exit 1
}
# Dump Koha instances
for instance_name in "${instance_ids[@]}"; do
dump_db "$instance_name"
echo "The database for $instance_name has been saved to ${DUMP_DIR}/koha_${instance_name}-$date.sql"
done
) 200>"$LOCK_FILE"