3 # Event script for ctdb-specific setup and other things that don't fit
7 CTDB_BASE
=$
(d
=$
(dirname "$0") && cd -P "$d" && dirname "$PWD")
9 .
"${CTDB_BASE}/functions"
13 ############################################################
15 # type is commonly supported and more portable than which(1)
16 # shellcheck disable=SC2039
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
28 WARNING: The installed 'tdbtool' does not offer the 'check' subcommand.
29 Using 'tdbdump' for database checks.
30 Consider updating 'tdbtool' for better checks!
32 elif $found_tdbdump; then
34 WARNING: 'tdbtool' is not available.
35 Using 'tdbdump' to check the databases.
36 Consider installing a recent 'tdbtool' for better checks!
40 WARNING: Cannot check databases since neither
41 'tdbdump' nor 'tdbtool check' is available.
42 Consider installing tdbtool or at least tdbdump!
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
61 timeout
10 tdbdump
"$_db" >/dev
/null
2>/dev
/null
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
74 die
"Persistent database $_db is corrupted! CTDB will not start."
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
86 _backup
="${_db}.$(date +'%Y%m%d.%H%M%S').corrupt"
88 WARNING: database ${_db} is corrupted.
89 Moving to backup ${_backup} for later analysis.
93 # Now remove excess backups
94 _max
="${CTDB_MAX_CORRUPT_DB_BACKUPS:-10}"
95 _bdb
="${_db##*/}" # basename
96 find "$_dir" -name "${_bdb}.*.corrupt" |
98 tail -n +$
((_max
+ 1)) |
104 maybe_backup_persistent_tdbs
()
106 _dir
="${CTDB_PERSISTENT_DB_BACKUP_DIR:-}"
107 if [ -z "$_dir" ]; then
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
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 ############################################################
142 # Load/cache database options from configuration file
145 if select_tdb_checker
; then
146 check_persistent_databases ||
exit $?
147 check_non_persistent_databases
151 maybe_backup_persistent_tdbs