-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase-snapshot
More file actions
executable file
·154 lines (123 loc) · 4.15 KB
/
database-snapshot
File metadata and controls
executable file
·154 lines (123 loc) · 4.15 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/bash
############################################################
##################### CONSTANTS ############################
############################################################
Timefile=".snapshot-timestamp"
indent() { sed 's/^/ /'; }
die() { echo "Error: $1" >&2 ; exit $2; }
warn() { echo "Warning: $1" >&2 ; }
############################################################
set -o pipefail -o nounset
Self=$(basename $0)
# long options to short ones
for arg in "$@"
do
shift
case "$arg" in
"--dry-run") set -- "$@" "-n" ;;
"--verbose") set -- "$@" "-v" ;;
*) set -- "$@" "$arg"
esac
done
# defaults
Verbose=n
DryRun=n
# parse options
OPTIND=1
while getopts "nv" opt
do
case "$opt" in
"n") DryRun=y ;;
"v") Verbose=y ;;
"?") die "unknown option" 10 ;;
esac
done
shift $(expr $OPTIND - 1) # remove options from positional parameters
# handle non-option arguments
[[ $# -gt 1 ]] && die "$Self: only one single configuration file can be provided." 12
############################################################
if [ $# -eq 1 ]
then
[ -f "$1" -a -r "$1" ] || die "$Self: configuration file $1 not found or not readable." 20
# source the job config
source "$1"
if [ -z ${Job+x} ]; then
Job=$(basename -s .conf $1)
fi
Self="${Self}@${Job}"
fi
# check whether all mandatory variables were set in config file or environment
[ -z ${Job+x} ] && die "Job name (Job) is required." 21
[ -z ${DATABASES+x} ] && die "List of databases (DATABASES) is required." 21
[ -z ${DB_DUMP+x} ] && die "Dump utility (DB_DUMP) is required." 21
[ -z ${REMOTE_BASE+x} ] && die "Remote path (REMOTE_BASE) is required." 21
[ -z ${TAG+x} ] && die "Snapshot tag format (TAG) is required." 21
############################################################
Outdir="$(mktemp -d)" || die "Could not create temporary directory." 30
touch "${Outdir}/${Timefile}" || die "Cannot touch(1) timestamp file '${Outdir}/${Timefile}'" 31
chmod 0444 "${Outdir}/${Timefile}" || warn "Could not make timestamp file '${Outdir}/${Timefile}' read-only."
Timestamp="$(date ${TAG_OPTIONS:-} -r "${Outdir}/${Timefile}" +"${TAG}")"
[ -z "${Timestamp+x}" ] && die "Problem generating new snapshot tag." 32
# set defaults for optional variables
[ -z ${WORK_DIR+x} ] && WORK_DIR=${Outdir}
[ -z ${DB_ARGS+x} ] && DB_ARGS=""
echo "- Creating snapshots in ${Outdir}:"
[ -n "${DB_USER:+x}" ] && SUDO_CMD="sudo -u ${DB_USER:-}"
is_array() {
[[ $# -ne 1 ]] && echo "is_array(): supply exactly one variable name as an argument" >&2 && return 2
local var=$1
[[ "$(declare -p "$var" 2> /dev/null)" =~ "declare -a" ]] && return 0 || return 1
}
for (( i=0; i<${#DATABASES[@]}; i++ ))
do
Db=${DATABASES[$i]}
# cd to working directory
echo -n " * ${Db}: cd"
if is_array WORK_DIR
then
cd ${WORK_DIR[$i]} || die " -- Failed to change directory to: ${WORK_DIR[$i]}" 41
else
cd ${WORK_DIR} || die " -- Failed to change directory to: ${WORK_DIR}" 42
fi
# run dump
if is_array DB_DUMP
then
DUMP_CMD=${DB_DUMP[$i]}
else
DUMP_CMD=${DB_DUMP}
fi
if is_array DB_ARGS
then
DUMP_ARGS=${DB_ARGS[$i]}
else
DUMP_ARGS=${DB_ARGS:-}
fi
echo -n " dump"
${SUDO_CMD:-} ${DUMP_CMD} ${DUMP_ARGS} "${Db}" > "${Outdir}/${Db}.sql" && echo -n " -- OK" || die " -- Database dump failed." 40
echo
done
echo ; echo -n "- Copying to ${REMOTE_BASE}${Timestamp}:"
if [ "$DryRun" = "n" ]
then
rsync -a "${REMOTE_AUTH[@]:---}" "${Outdir}/" "${REMOTE_BASE}${Timestamp}"
else
echo
rsync -avn "${REMOTE_AUTH[@]:---}" "${Outdir}/" "${REMOTE_BASE}${Timestamp}"
fi
if [ $? -ne 0 ]
then
echo " Error code: $?"
die "rsync failed, retaining local files in ${Outdir}." 50
else
echo " Done."
echo -n "- Deleting temporary files:"
cd "${Outdir}"
for Db in ${DATABASES[@]}
do
rm -f "${Db}.sql" && echo -n " ${Db}.sql" || die "Could not delete ${Db}.sql." 60
done
rm -f "${Timefile}" && echo -n " ${Timefile}" || die "Could not delete ${Timefile}." 61
cd /
rmdir "${Outdir}" && echo -n " ${Outdir}" || die "Could not remove ${Outdir}." 62
echo " Done."
fi