#!/bin/bash # # backup-script system for cloning systems using rsync # Copyright (c)2008-2016 Alexander Barton, alex@barton.de # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # Please read the file COPYING, README and AUTHORS for more information. # NAME=$(basename "$0") PIDFILE="/var/run/backup-script.pid" QUICK=0 ONLY_ERRORS=0 ONLY_LATEST=0 export LC_ALL=C declare -i count=0 declare -i snapshots=0 # Default settings, can be overwritten in backup-script.conf: [ -d "/usr/local/etc/backup-script.d" ] \ && conf_d="/usr/local/etc/backup-script.d" \ || conf_d="/etc/backup-script.d" default_backup_type="rsync" default_generations=0 default_target="/var/backups" # Search configuration file (last one is used as default!) for conf in \ "/usr/local/etc/backup-script.conf" \ "/etc/backup-script.conf" \ "${conf_d}/backup-script.conf" \ "/usr/local/etc/backup-script.conf" \ ; do if [ -r "$conf" ]; then # shellcheck source=/dev/null source "$conf" break fi done Usage() { echo "Usage: $NAME [--errors|--latest] [--quick] [ [ [...]]]" echo " $NAME --running" echo echo " -e, --errors Only show current backups with errors (implies \"--latest\")." echo " -l, --latest Only show latest backup generations." echo " -q, --quick Don't calculate backup sizes." echo " -r, --running Check if an \"backup-script\" task is currently running." echo echo "When no is given, all defined jobs are listed." echo exit 2 } Check_Size() { # $1: directory # $2: padding if [ "$QUICK" = "0" ]; then size=$(du -Hhs "$1" | cut -f1) # shellcheck disable=SC2086 echo "$2 - Size:" $size fi } Check_Stamp() { # $1: stamp file # $2: padding if [ -f "$1" ]; then declare -i code=-1 declare -i start_t=-1 start="" declare -i end_t=-1 end="" declare -i duration_t=-1 # Read in "stamp file" # shellcheck source=/dev/null source "$1" if [ $start_t -gt 0 ] && [ $end_t -gt 0 ]; then if [ "$(uname)" = "Linux" ]; then start=$(date -d @"$start_t") end=$(date -d @"$end_t") else start=$(date -r "$start_t") end=$(date -r "$end_t") fi duration_t=$end_t-$start_t else if [ "$(uname)" = "Linux" ]; then end=$(LC_ALL=C stat "$1" | grep "^Modify: " \ | cut -d':' -f2- | cut -d. -f1) else end=$(LC_ALL=C stat -f "%Sc" "$1") fi fi # shellcheck disable=SC2086 [ -n "$start" ] && echo "$2 - Start date:" $start # shellcheck disable=SC2086 [ -n "$end" ] && echo "$2 - End date:" $end if [ $duration_t -gt -1 ]; then declare -i s=$duration_t if [ $s -ge 60 ]; then declare -i m=$((s / 60)) declare -i s=$((s % 60)) if [ $m -ge 60 ]; then declare -i h=$((m / 60)) declare -i m=$((m % 60)) if [ $h -ge 24 ]; then declare -i d=$((h / 24)) declare -i h=$((h % 24)) duration="${d}d${h}h${m}m${s}s" else duration="${h}h${m}m${s}s" fi else duration="${m}m${s}s" fi else duration="${s}s" fi echo "$2 - Duration:" $duration fi case "$code" in 0) txt=", OK"; ;; 24) txt=", WARNING (some files vanished during backup)"; ;; *) txt=", ERROR" esac [ $code -ge 0 ] && echo "$2 - Result code: ${code}${txt}" else echo "$2 - No timestamp recorded! Backup currently running or aborted?" fi } Snapshot_Info() { echo " - Snapshot: $1" Check_Size "$1" " " Check_Stamp "$1/.stamp" " " } Get_Result_Code() { code=1 # shellcheck source=/dev/null [ -r "$1" ] && source "$1" [ -z "$code" ] && code=1 echo $code } if [[ "$1" == "-r" || "$1" == "--running" ]]; then pid="$(cat "$PIDFILE" 2>/dev/null)" if [ -n "$pid" ]; then if kill -0 "$pid" >/dev/null 2>&1; then echo "Backup job running with PID $pid." echo pstree -ap "$pid" 2>/dev/null exit 0 else echo "No backup running (invalid PID $pid in \"$PIDFILE\")." exit 1 fi fi echo "No backup running (no PID file \"$PIDFILE\" found)." exit 1 fi while [ $# -gt 0 ]; do case "$1" in "--errors"|"-e") ONLY_ERRORS=1 ONLY_LATEST=1 ;; "--latest"|"-l") ONLY_LATEST=1 ;; "--quick"|"-q") QUICK=1 ;; "-"*) Usage ;; *) break esac shift done if [ $# -ge 1 ]; then for s in "$@"; do if [ ! -r "${conf_d}/$s" ]; then echo "$NAME: Can' read \"${conf_d}/$s\"!" exit 1 fi sys+=("${conf_d}/$s") done else sys=("${conf_d}/"*) fi for f in "${sys[@]}"; do [[ -r "$f" && -f "$f" ]] || continue fname=$(basename "$f") case "$fname" in "backup-script.conf"|*.sh) continue ;; esac # Set global defaults system="$fname" target="$default_target" generations="$default_generations" backup_type="$default_backup_type" # Read in system configuration file # shellcheck source=/dev/null source "$f" target="$target/$(basename "$f")" [ -d "$target" ] || continue if [ "$ONLY_ERRORS" != "0" ]; then [[ "$backup_type" = "disabled" ]] && continue [ $generations -gt 0 ] \ && result=$(Get_Result_Code "$target/latest/.stamp") \ || result=$(Get_Result_Code "$target/.stamp") [[ $result -eq 0 || $result -eq 24 ]] && continue fi # System name [ "$system" = "$fname" ] && echo "$fname" || echo "$fname [$system]" # System target directory echo "- Target: $target" if [ $generations -gt 0 ]; then if [ "$ONLY_LATEST" = "0" ]; then for s in $target/[0-9]*-[0-9]* $target/current; do [ -e "$s" ] || continue Snapshot_Info "$s" snapshots=$snapshots+1 done elif [ -e "$target/latest" ]; then Snapshot_Info "$target/latest" snapshots=$snapshots+1 fi else # Timestamp and result code Check_Size "$target" Check_Stamp "$target/.stamp" snapshots=$snapshots+1 fi count=$count+1 echo done if [ "$ONLY_ERRORS" != "0" ]; then status="failed "; p0="."; pN="!" else status=""; p0="!"; pN="." fi if [ $count -lt 1 ]; then echo "No ${status}backups found${p0}" exit 1 fi [ $count -eq 1 ] && sc="" || sc="s" [ $snapshots -eq 1 ] && ss="" || ss="s" echo "$count ${status}system backup$sc found, $snapshots snapshot$ss${pN}" # -eof-