ctdb-scripts: Add support for backing up persistent TDBs
[samba4-gss.git] / ctdb / config / ctdb-backup-persistent-tdbs.sh
blob9610eefeb3861c151b7893750614a79887f6765b
1 #!/bin/sh
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/>.
22 # Options:
24 # -l: Only do the backup if this node is the leader node, otherwise
25 # exit with 0.
27 # -L <rc>: Only do the backup if this node is the leader node, otherwise
28 # exit with <rc>.
31 # Option/argument handling
34 die()
36 echo "ERROR: $1"
37 exit 1
40 usage()
42 die "usage: $0 [-l | -L <rc> ] <dir>"
45 leader_only=false
46 leader_only_rc=0
47 dir=""
49 while getopts "L:lh?" opt; do
50 case "$opt" in
52 leader_only=true
53 leader_only_rc="$OPTARG"
56 leader_only=true
58 \? | h)
59 usage
61 esac
62 done
63 shift $((OPTIND - 1))
65 if [ $# -ne 1 ]; then
66 usage
69 dir="$1"
71 if [ ! -d "$dir" ]; then
72 die "No such directory ${dir}"
75 if $leader_only; then
76 this_node=$(ctdb pnn)
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
94 mkdir -p "$outdir"
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}"
107 done
110 # Create tarball
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}"
123 exit 0