ctdb-scripts: Add support for backing up persistent TDBs
[samba4-gss.git] / ctdb / config / events / legacy / 95.database.script
blobe2627c6c1d04ff49ba91da5659463d67aa555d75
1 #!/bin/sh
3 # Event script for ctdb-specific setup and other things that don't fit
4 # elsewhere.
6 [ -n "$CTDB_BASE" ] ||
7 CTDB_BASE=$(d=$(dirname "$0") && cd -P "$d" && dirname "$PWD")
9 . "${CTDB_BASE}/functions"
11 load_script_options
13 ############################################################
15 # type is commonly supported and more portable than which(1)
16 # shellcheck disable=SC2039
17 select_tdb_checker()
19 # Find the best TDB consistency check available.
20 use_tdb_tool_check=false
21 type tdbtool >/dev/null 2>&1 && found_tdbtool=true
22 type tdbdump >/dev/null 2>&1 && found_tdbdump=true
24 if $found_tdbtool && echo "help" | tdbtool | grep -q check; then
25 use_tdb_tool_check=true
26 elif $found_tdbtool && $found_tdbdump; then
27 cat <<EOF
28 WARNING: The installed 'tdbtool' does not offer the 'check' subcommand.
29 Using 'tdbdump' for database checks.
30 Consider updating 'tdbtool' for better checks!
31 EOF
32 elif $found_tdbdump; then
33 cat <<EOF
34 WARNING: 'tdbtool' is not available.
35 Using 'tdbdump' to check the databases.
36 Consider installing a recent 'tdbtool' for better checks!
37 EOF
38 else
39 cat <<EOF
40 WARNING: Cannot check databases since neither
41 'tdbdump' nor 'tdbtool check' is available.
42 Consider installing tdbtool or at least tdbdump!
43 EOF
44 return 1
48 check_tdb()
50 _db="$1"
52 if $use_tdb_tool_check; then
53 # tdbtool always exits with 0 :-(
54 if timeout 10 tdbtool "$_db" check 2>/dev/null |
55 grep -q "Database integrity is OK"; then
56 return 0
57 else
58 return 1
60 else
61 timeout 10 tdbdump "$_db" >/dev/null 2>/dev/null
62 return $?
66 check_persistent_databases()
68 _dir="${CTDB_DBDIR_PERSISTENT:-${CTDB_VARDIR}/persistent}"
69 [ -d "$_dir" ] || return 0
71 for _db in "$_dir/"*.tdb.*[0-9]; do
72 [ -r "$_db" ] || continue
73 check_tdb "$_db" ||
74 die "Persistent database $_db is corrupted! CTDB will not start."
75 done
78 check_non_persistent_databases()
80 _dir="${CTDB_DBDIR:-${CTDB_VARDIR}}"
81 [ -d "$_dir" ] || return 0
83 for _db in "${_dir}/"*.tdb.*[0-9]; do
84 [ -r "$_db" ] || continue
85 check_tdb "$_db" || {
86 _backup="${_db}.$(date +'%Y%m%d.%H%M%S').corrupt"
87 cat <<EOF
88 WARNING: database ${_db} is corrupted.
89 Moving to backup ${_backup} for later analysis.
90 EOF
91 mv "$_db" "$_backup"
93 # Now remove excess backups
94 _max="${CTDB_MAX_CORRUPT_DB_BACKUPS:-10}"
95 _bdb="${_db##*/}" # basename
96 find "$_dir" -name "${_bdb}.*.corrupt" |
97 sort -r |
98 tail -n +$((_max + 1)) |
99 xargs rm -f
101 done
104 maybe_backup_persistent_tdbs()
106 _dir="${CTDB_PERSISTENT_DB_BACKUP_DIR:-}"
107 if [ -z "$_dir" ]; then
108 return 0
111 if [ ! -d "$_dir" ]; then
112 echo "Creating CTDB_PERSISTENT_DB_BACKUP_DIR=${_dir}"
113 if ! mkdir -p "$_dir"; then
114 die "ERROR: unable to create ${_dir}"
118 # Don't backup if there are backup files from within the past day
119 _out=$(find "$_dir" -type f -mtime -1)
120 if [ -n "$_out" ]; then
121 return 0
124 # Script will ignore if this isn't leader node, so don't
125 # double-check that here...
126 "${CTDB_BASE}/ctdb-backup-persistent-tdbs.sh" -l "$_dir"
128 # Remove backups beyond the limit (default 14)
129 _limit="${CTDB_PERSISTENT_DB_BACKUP_LIMIT:-14}"
130 _offset=$((_limit + 1))
131 # Can't sort by time using find instead of ls
132 # shellcheck disable=SC2012
133 ls -t "$_dir"/* 2>/dev/null | tail -n "+${_offset}" | xargs rm -f
136 ############################################################
138 ctdb_check_args "$@"
140 case "$1" in
141 init)
142 # Load/cache database options from configuration file
143 ctdb_get_db_options
145 if select_tdb_checker; then
146 check_persistent_databases || exit $?
147 check_non_persistent_databases
150 monitor)
151 maybe_backup_persistent_tdbs
153 esac
155 # all OK
156 exit 0