3 # backup-script system for cloning systems using rsync
4 # Copyright (c)2008-2015 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
45 echo "Usage: $NAME [--errors|--latest] [--quick] [<system> [<system> [...]]]"
46 echo " $NAME --running"
48 echo " --errors, -e Only show current backups with errors (implies \"--latest\")."
49 echo " --latest, -l Only show latest backup generations."
50 echo " --quick, -q Don't calculate backup sizes."
51 echo " --running, -r Check if an \"backup-script\" task is currently running."
60 if [ "$QUICK" = "0" ]; then
61 size=`du -Hhs "$1" | cut -f1`
62 echo "$2 - Size:" $size
71 if [ "$(uname)" = "Linux" ]; then
72 last=`LC_ALL=C stat "$1" | grep "^Modify: " \
73 | cut -d':' -f2- | cut -d. -f1`
75 last=`LC_ALL=C stat -f "%Sc" "$1"`
77 [ -n "$last" ] && echo "$2 - Date:" $last
82 24) txt=", WARNING (some files vanished during backup)"; ;;
85 [ -n "$code" ] && echo "$2 - Result code: $code$txt"
87 echo "$2 - No timestamp recorded! Backup currently running or aborted?"
92 echo " - Snapshot: $1"
94 Check_Stamp "$1/.stamp" " "
99 [ -r "$1" ] && source "$1"
100 [ -z "$code" ] && code=1
104 if [ "$1" == "-r" -o "$1" == "--running" ]; then
105 pid="$(cat "$PIDFILE" 2>/dev/null)"
106 if [ -n "$pid" ]; then
107 if kill -0 "$pid" >/dev/null 2>&1; then
108 echo "Backup job running with PID $pid."
110 pstree -ap "$pid" 2>/dev/null
113 echo "No backup running (invalid PID $pid in \"$PIDFILE\")."
117 echo "No backup running (no PID file \"$PIDFILE\" found)."
121 while [ $# -gt 0 ]; do
142 if [ $# -ge 1 ]; then
144 if [ ! -r "${conf_d}/$s" ]; then
145 echo "$NAME: Can' read \"${conf_d}/$s\"!"
148 sys="$sys ${conf_d}/$s"
155 [ -r "$f" -a -f "$f" ] || continue
159 "backup-script.conf"|*.sh)
164 # Set global defaults
166 target="$default_target"
167 generations="$default_generations"
169 # Read in system configuration file
172 target="$target/$(basename "$f")"
174 [ -d "$target" ] || continue
176 if [ "$ONLY_ERRORS" != "0" ]; then
177 [ $generations -gt 0 ] \
178 && result=$(Get_Result_Code "$target/latest/.stamp") \
179 || result=$(Get_Result_Code "$target/.stamp")
180 [ $result -eq 0 -o $result -eq 24 ] && continue
184 [ "$system" = "$fname" ] && echo "$fname" || echo "$fname [$system]"
186 # System target directory
187 echo "- Target: $target"
189 if [ $generations -gt 0 ]; then
190 if [ "$ONLY_LATEST" = "0" ]; then
191 for s in $target/[0-9]*-[0-9]* $target/current; do
192 [ -e "$s" ] || continue
194 snapshots=$snapshots+1
196 elif [ -e "$target/latest" ]; then
197 Snapshot_Info "$target/latest"
198 snapshots=$snapshots+1
201 # Timestamp and result code
203 Check_Stamp "$target/.stamp"
204 snapshots=$snapshots+1
211 if [ $count -lt 1 ]; then
212 echo "No backups found!"
215 [ $count -eq 1 ] && sc="" || sc="s"
216 [ $snapshots -eq 1 ] && ss="" || ss="s"
217 echo "$count system backup$sc found, $snapshots snapshot$ss."