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
76 declare -i duration_t=-1
78 # Read in "stamp file"
81 if [ $start_t -gt 0 ] && [ $end_t -gt 0 ]; then
82 if [ "$(uname)" = "Linux" ]; then
83 start=$(date -d @"$start_t")
84 end=$(date -d @"$end_t")
86 start=$(date -r "$start_t")
87 end=$(date -r "$end_t")
89 duration_t=$end_t-$start_t
91 if [ "$(uname)" = "Linux" ]; then
92 end=`LC_ALL=C stat "$1" | grep "^Modify: " \
93 | cut -d':' -f2- | cut -d. -f1`
95 end=`LC_ALL=C stat -f "%Sc" "$1"`
98 [ -n "$start" ] && echo "$2 - Start date:" $start
99 [ -n "$end" ] && echo "$2 - End date:" $end
100 if [ $duration_t -gt -1 ]; then
101 declare -i s=$duration_t
102 if [ $s -ge 60 ]; then
103 declare -i m=$(($s/60))
104 declare -i s=$(($s%60))
105 if [ $m -ge 60 ]; then
106 declare -i h=$(($m/60))
107 declare -i m=$(($m%60))
108 if [ $h -ge 24 ]; then
109 declare -i d=$(($h/24))
110 declare -i h=$(($h%24))
111 duration="${d}d${h}h${m}m${s}s"
113 duration="${h}h${m}m${s}s"
116 duration="${m}m${s}s"
121 echo "$2 - Duration:" $duration
126 24) txt=", WARNING (some files vanished during backup)"; ;;
129 [ $code -ge 0 ] && echo "$2 - Result code: ${code}${txt}"
131 echo "$2 - No timestamp recorded! Backup currently running or aborted?"
136 echo " - Snapshot: $1"
138 Check_Stamp "$1/.stamp" " "
143 [ -r "$1" ] && source "$1"
144 [ -z "$code" ] && code=1
148 if [ "$1" == "-r" -o "$1" == "--running" ]; then
149 pid="$(cat "$PIDFILE" 2>/dev/null)"
150 if [ -n "$pid" ]; then
151 if kill -0 "$pid" >/dev/null 2>&1; then
152 echo "Backup job running with PID $pid."
154 pstree -ap "$pid" 2>/dev/null
157 echo "No backup running (invalid PID $pid in \"$PIDFILE\")."
161 echo "No backup running (no PID file \"$PIDFILE\" found)."
165 while [ $# -gt 0 ]; do
186 if [ $# -ge 1 ]; then
188 if [ ! -r "${conf_d}/$s" ]; then
189 echo "$NAME: Can' read \"${conf_d}/$s\"!"
192 sys="$sys ${conf_d}/$s"
199 [ -r "$f" -a -f "$f" ] || continue
203 "backup-script.conf"|*.sh)
208 # Set global defaults
210 target="$default_target"
211 generations="$default_generations"
213 # Read in system configuration file
216 target="$target/$(basename "$f")"
218 [ -d "$target" ] || continue
220 if [ "$ONLY_ERRORS" != "0" ]; then
221 [ $generations -gt 0 ] \
222 && result=$(Get_Result_Code "$target/latest/.stamp") \
223 || result=$(Get_Result_Code "$target/.stamp")
224 [ $result -eq 0 -o $result -eq 24 ] && continue
228 [ "$system" = "$fname" ] && echo "$fname" || echo "$fname [$system]"
230 # System target directory
231 echo "- Target: $target"
233 if [ $generations -gt 0 ]; then
234 if [ "$ONLY_LATEST" = "0" ]; then
235 for s in $target/[0-9]*-[0-9]* $target/current; do
236 [ -e "$s" ] || continue
238 snapshots=$snapshots+1
240 elif [ -e "$target/latest" ]; then
241 Snapshot_Info "$target/latest"
242 snapshots=$snapshots+1
245 # Timestamp and result code
247 Check_Stamp "$target/.stamp"
248 snapshots=$snapshots+1
255 if [ "$ONLY_ERRORS" != "0" ]; then
256 status="failed "; p0="."; pN="!"
258 status=""; p0="!"; pN="."
260 if [ $count -lt 1 ]; then
261 echo "No ${status}backups found${p0}"
264 [ $count -eq 1 ] && sc="" || sc="s"
265 [ $snapshots -eq 1 ] && ss="" || ss="s"
266 echo "$count ${status}system backup$sc found, $snapshots snapshot$ss${pN}"