3 # backup-script system for cloning systems using rsync
4 # Copyright (c)2008-2016 Alexander Barton, alex@barton.de
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 # Please read the file COPYING, README and AUTHORS for more information.
14 PIDFILE="/var/run/backup-script.pid"
22 declare -i snapshots=0
24 # Default settings, can be overwritten in backup-script.conf:
25 [ -d "/usr/local/etc/backup-script.d" ] \
26 && conf_d="/usr/local/etc/backup-script.d" \
27 || conf_d="/etc/backup-script.d"
28 default_target="/var/backups"
31 # Search configuration file (last one is used as default!)
33 "/usr/local/etc/backup-script.conf" \
34 "/etc/backup-script.conf" \
35 "${conf_d}/backup-script.conf" \
36 "/usr/local/etc/backup-script.conf" \
38 if [ -r "$conf" ]; then
39 # shellcheck source=/dev/null
46 echo "Usage: $NAME [--errors|--latest] [--quick] [<job> [<job> [...]]]"
47 echo " $NAME --running"
49 echo " -e, --errors Only show current backups with errors (implies \"--latest\")."
50 echo " -l, --latest Only show latest backup generations."
51 echo " -q, --quick Don't calculate backup sizes."
52 echo " -r, --running Check if an \"backup-script\" task is currently running."
54 echo "When no <job> is given, all defined jobs are listed."
63 if [ "$QUICK" = "0" ]; then
64 size=$(du -Hhs "$1" | cut -f1)
65 # shellcheck disable=SC2086
66 echo "$2 - Size:" $size
80 declare -i duration_t=-1
82 # Read in "stamp file"
83 # shellcheck source=/dev/null
86 if [ $start_t -gt 0 ] && [ $end_t -gt 0 ]; then
87 if [ "$(uname)" = "Linux" ]; then
88 start=$(date -d @"$start_t")
89 end=$(date -d @"$end_t")
91 start=$(date -r "$start_t")
92 end=$(date -r "$end_t")
94 duration_t=$end_t-$start_t
96 if [ "$(uname)" = "Linux" ]; then
97 end=$(LC_ALL=C stat "$1" | grep "^Modify: " \
98 | cut -d':' -f2- | cut -d. -f1)
100 end=$(LC_ALL=C stat -f "%Sc" "$1")
103 # shellcheck disable=SC2086
104 [ -n "$start" ] && echo "$2 - Start date:" $start
105 # shellcheck disable=SC2086
106 [ -n "$end" ] && echo "$2 - End date:" $end
107 if [ $duration_t -gt -1 ]; then
108 declare -i s=$duration_t
109 if [ $s -ge 60 ]; then
110 declare -i m=$((s / 60))
111 declare -i s=$((s % 60))
112 if [ $m -ge 60 ]; then
113 declare -i h=$((m / 60))
114 declare -i m=$((m % 60))
115 if [ $h -ge 24 ]; then
116 declare -i d=$((h / 24))
117 declare -i h=$((h % 24))
118 duration="${d}d${h}h${m}m${s}s"
120 duration="${h}h${m}m${s}s"
123 duration="${m}m${s}s"
128 echo "$2 - Duration:" $duration
133 24) txt=", WARNING (some files vanished during backup)"; ;;
136 [ $code -ge 0 ] && echo "$2 - Result code: ${code}${txt}"
138 echo "$2 - No timestamp recorded! Backup currently running or aborted?"
143 echo " - Snapshot: $1"
145 Check_Stamp "$1/.stamp" " "
150 # shellcheck source=/dev/null
151 [ -r "$1" ] && source "$1"
152 [ -z "$code" ] && code=1
156 if [[ "$1" == "-r" || "$1" == "--running" ]]; then
157 pid="$(cat "$PIDFILE" 2>/dev/null)"
158 if [ -n "$pid" ]; then
159 if kill -0 "$pid" >/dev/null 2>&1; then
160 echo "Backup job running with PID $pid."
162 pstree -ap "$pid" 2>/dev/null
165 echo "No backup running (invalid PID $pid in \"$PIDFILE\")."
169 echo "No backup running (no PID file \"$PIDFILE\" found)."
173 while [ $# -gt 0 ]; do
194 if [ $# -ge 1 ]; then
196 if [ ! -r "${conf_d}/$s" ]; then
197 echo "$NAME: Can' read \"${conf_d}/$s\"!"
200 sys+=("${conf_d}/$s")
206 for f in "${sys[@]}"; do
207 [[ -r "$f" && -f "$f" ]] || continue
209 fname=$(basename "$f")
211 "backup-script.conf"|*.sh)
216 # Set global defaults
218 target="$default_target"
219 generations="$default_generations"
221 # Read in system configuration file
222 # shellcheck source=/dev/null
225 target="$target/$(basename "$f")"
227 [ -d "$target" ] || continue
229 if [ "$ONLY_ERRORS" != "0" ]; then
230 [ $generations -gt 0 ] \
231 && result=$(Get_Result_Code "$target/latest/.stamp") \
232 || result=$(Get_Result_Code "$target/.stamp")
233 [[ $result -eq 0 || $result -eq 24 ]] && continue
237 [ "$system" = "$fname" ] && echo "$fname" || echo "$fname [$system]"
239 # System target directory
240 echo "- Target: $target"
242 if [ $generations -gt 0 ]; then
243 if [ "$ONLY_LATEST" = "0" ]; then
244 for s in $target/[0-9]*-[0-9]* $target/current; do
245 [ -e "$s" ] || continue
247 snapshots=$snapshots+1
249 elif [ -e "$target/latest" ]; then
250 Snapshot_Info "$target/latest"
251 snapshots=$snapshots+1
254 # Timestamp and result code
256 Check_Stamp "$target/.stamp"
257 snapshots=$snapshots+1
264 if [ "$ONLY_ERRORS" != "0" ]; then
265 status="failed "; p0="."; pN="!"
267 status=""; p0="!"; pN="."
269 if [ $count -lt 1 ]; then
270 echo "No ${status}backups found${p0}"
273 [ $count -eq 1 ] && sc="" || sc="s"
274 [ $snapshots -eq 1 ] && ss="" || ss="s"
275 echo "$count ${status}system backup$sc found, $snapshots snapshot$ss${pN}"