3 # Backup persistent CTDB TDBs into the given directory.
5 # Copyright: DataDirect Networks, 2024
6 # Authors: Vinit Agnihotri <vagnihotri@ddn.com>
7 # Martin Schwenke <mschwenke@ddn.com>
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, see <http://www.gnu.org/licenses/>.
24 # -l: Only do the backup if this node is the leader node, otherwise
27 # -L <rc>: Only do the backup if this node is the leader node, otherwise
31 # Option/argument handling
42 die
"usage: $0 [-l | -L <rc> ] <dir>"
49 while getopts "L:lh?" opt
; do
53 leader_only_rc
="$OPTARG"
71 if [ ! -d "$dir" ]; then
72 die
"No such directory ${dir}"
77 leader_node
=$
(ctdb leader
)
78 if [ "$this_node" != "$leader_node" ]; then
79 exit "$leader_only_rc"
84 # Backups TDBs in timestamped subdirectory
87 dt
=$
(date "+%Y%m%d%H%M%S")
88 prefix
="ctdb-persistent-db-backup-${dt}"
89 outdir
="${dir}/${prefix}"
91 # Clean up temporary directory on failure"
92 trap 'rm -rf ${outdir}' 0
96 if ! db_map
=$
(ctdb getdbmap
-X); then
97 die
"Failed to list databases"
99 db_list
=$
(echo "$db_map" |
awk -F '|' '$5 == "1" { print $3 }')
101 cd "$outdir" || die
"Failed to change directory to ${dir}"
103 for db
in $db_list; do
104 if ! ctdb backupdb
"$db" "${db}.backup"; then
105 die
"Failed to backup ${db}"
113 cd "$dir" || die
"Failed to change directory to ${dir}"
115 tarball
="${prefix}.tgz"
117 if ! tar -c -z -f "$tarball" "$prefix"; then
118 die
"Failed to create tarball"
121 echo "Created backup tarball ${dir}/${tarball}"