Skip to content

Instantly share code, notes, and snippets.

@bougui505
Last active September 23, 2020 02:08
Show Gist options
  • Save bougui505/ba9db84a2fc6f9330f3ccf32a352a98e to your computer and use it in GitHub Desktop.
Save bougui505/ba9db84a2fc6f9330f3ccf32a352a98e to your computer and use it in GitHub Desktop.
#!/usr/bin/env zsh
# -*- coding: UTF8 -*-
# Author: Guillaume Bouvier -- guillaume.bouvier@ens-cachan.org
# 2016-05-10 15:36:06 (UTC+0200)
format_duration()
{
diff=$1 # Duration in seconds
if test $diff -ge 86400; then
echo "$(($diff / 86400))d$((($diff / 3600) % 24))h$((($diff / 60) % 60))m$(($diff % 60))s"
elif test $diff -ge 3600; then
echo "$(($diff / 3600))h$((($diff / 60) % 60))m$(($diff % 60))s"
elif test $diff -ge 60; then
echo "$((($diff / 60) % 60))m$(($diff % 60))s"
else
echo "${diff}s"
fi
}
# Compress the old backup_files.log.1 file
if test -f "$HOME/crashplan/log/backup_files.log.1"; then
gzip -9 -S "_$(date +"%Y%m%d").gz" "$HOME/crashplan/log/backup_files.log.1"
fi
# sort the log files by date
LOGFILE=($(ls -rt $HOME/crashplan/log/backup_files.log.*))
if [[ "$@[1]" = "" || "$@[1]" = "-h" ]]; then
echo "Usage:"
echo " bstat filename1 [filename2] [...]"
echo " Check the backup status for the given filenames"
echo " bstat -v filename"
echo " Show all the backuped versions of the given file"
echo " bstat -d [path]"
echo " Check for deleted files or directories that can be restored"
echo " If a path is given, search in the given path,"
echo " else the current directory is used"
echo " bstat -a"
echo " Only print the time elapsed from last logged activity"
echo " bstat -i"
echo " Display license and version informations"
exit 0
fi
APPLOG="$LOGFILE[1]:h/app.log"
# Backup frequency in seconds:
BACKFREQ=$(grep watcherNoActivityInterval $APPLOG | awk -F"= " '{print $2}' | awk '{print $1}' | tr -d 'min' | awk '{print $0*60}')
if [[ $1 = "-i" ]]; then
echo "Version: $(grep 'CPVERSION = ' $APPLOG | awk -F" = " '{print $2}')"
echo "License expire date: $(grep -o "expireDate.*$" $APPLOG | awk -F"=" '{print $2}' | awk -F"," '{print $1}')"
exit 0
fi
# Decompress the log files in a tmp file:
TMPFILE=$(mktemp -p /dev/shm)
zcat -f $LOGFILE > $TMPFILE
# cleanup function
# see: http://goo.gl/2aUQc
trap "rm $TMPFILE; exit" SIGHUP SIGINT SIGTERM
NOW=$(date +%s)
HISTLOG="$LOGFILE[1]:h/history.log.0"
# List of servers where to backup
SERVERNAMES=($(grep 'Completed backup' $HISTLOG | awk -F'to | in |: No' '{gsub(/ /,"_",$2); print $2}' | sort | uniq))
for SERVERNAME in $SERVERNAMES; do
echo "Destination: $SERVERNAME"
# Match insensitive to space or underscore
match1=$SERVERNAME
match2=$(echo $SERVERNAME | tr "_" " ")
LASTACTION=$(date -d"$(grep "Completed backup" $HISTLOG | grep "$match1\|$match2" | tail -n1 | awk '{print $2, $3}')" +%s) 2> /dev/null || LASTACTION=-1
if test $LASTACTION -gt 0; then
# Time elapsed from last activity in seconds:
TIME_ELAPSED=$(($NOW-$LASTACTION))
# Time to the next backup in seconds:
NEXT_BACKUP=$(($BACKFREQ-$TIME_ELAPSED))
echo "Last completed backup: $(format_duration $TIME_ELAPSED) ago"
else
NEXT_BACKUP=-1
fi
if test $NEXT_BACKUP -gt 0; then
echo "Next backup in $(format_duration $NEXT_BACKUP)"
else
if grep "Starting backup\|Stopped backup\|Completed backup" $HISTLOG | tail -1 | grep "Starting backup to" | grep "$match1\|$match2">/dev/null; then
STARTTIME=$(date -d"$(grep "Starting backup to" $HISTLOG | grep "$match1\|$match2" | tail -1 | awk '{print $2, $3}')" +%s)
SIZE=$(grep "Starting backup to" $HISTLOG | grep "$match1\|$match2" | tail -1 | awk -F'[()]' '{print $2}') # SIZE of files to backup
UNIT="$SIZE[-2]"
SIZE="${SIZE: : -2}" # remove 2 last characters
SIZE=$(echo $(printf "%f" $SIZE)$UNIT | numfmt --from=si)
BACK_ELAPSED=$(($NOW-$STARTTIME))
echo "Backup running for: $(format_duration $BACK_ELAPSED)"
CURRENT_FILE_BACKUP=$(cat $TMPFILE | grep -v "Completed\|Starting" | tail -1 | awk '{print $7}')
echo "Last backup element: $CURRENT_FILE_BACKUP"
elif grep "$match1\|$match2" $HISTLOG | tail -1 | grep "not ready for backup" > /dev/null; then
echo "Destination unavailable."
else
echo "Backup should start ASAP..."
fi
fi
echo "---------------------"
done
if [[ $1 = "-a" ]]; then
rm $TMPFILE && exit 0
fi
echo "___________________________________________"
if [[ $1 = "-d" ]]; then
if test ${#@[*]} -eq 1; then # No argument after -d option
DIR=$(pwd) # Use the current directory
else
DIR=$@[2]:A # Use the given directory to search in
fi
DIR_ARRAY=(${(s:/:)DIR}) # split the DIR as an array eg: (home user data)
#(see: http://stackoverflow.com/a/29073092/1679629)
DIR_DEPTH=${#DIR_ARRAY[@]}
while read D ; do
D_ARRAY=(${(s:/:)D})
F="$D_ARRAY[$((DIR_DEPTH+1))]"
if test ! -e "$DIR/$F"; then
echo $F
fi
done < =(cat $TMPFILE | grep "$DIR" | grep "(deleted)$" | cut -d" " -f7- | sed 's/ (deleted)//' | sort | uniq) | sort | uniq
rm $TMPFILE && exit 0
fi
if [[ $1 = "-v" ]]; then
X=$2
MODIFDATE=$(stat -c %Y $X:A)
echo "Available backups for file '$X':"
#BDATES=$(cat $TMPFILE | grep '^I' | grep $X:A | awk '{print $2,$3}')
while read BDATE_; do
BACKDATE=$(date -d"$BDATE_" +"%s")
TODAY=$(date +"%Y%m%d")
# Format output
if test $((NOW-BACKDATE)) -lt 1800; then # last backup less than 30 min ago
BDATE="$(date -d "0 $NOW seconds - $BACKDATE seconds" +"%Mm") ago"
elif test $(date -d"$BDATE_" +"%Y%m%d") -eq $TODAY; then
BDATE="Today $(date -d"$BDATE_" +"%H:%M")"
else
BDATE=$(date -d"$BDATE_" +"%F %H:%M")
fi
if test $BACKDATE -ge $MODIFDATE; then
echo "$(tput bold)$BDATE" # Put actual file version in bold
else
echo $BDATE # Else normal font
fi
done < =(cat $TMPFILE | grep '^I' | grep $X:A | awk '{print $2,$3}')
rm $TMPFILE && exit 0
fi
OUT="Last backup date: | Status: | Filename:\n"
for X in "$@"; do
if test $(cat $TMPFILE | grep '^I' | grep -c $X:A) -gt 0; then
# Retrieve backup date
BDATE=$(cat $TMPFILE | grep '^I' | grep $X:A | tail -n1 | awk '{print $2,$3}')
BACKDATE=$(date -d"$BDATE" +"%s")
MODIFDATE=$(stat -c %Y $X:A)
TODAY=$(date +"%Y%m%d")
if test $BACKDATE -ge $MODIFDATE; then
STATUS="OK"
else
STATUS="NOT"
fi
# Format output
if test $((NOW-BACKDATE)) -lt 1800; then # last backup less than 30 min ago
BDATE="$(date -d "0 $NOW seconds - $BACKDATE seconds" +"%Mm") ago"
elif test $(date -d"$BDATE" +"%Y%m%d") -eq $TODAY; then
BDATE="Today $(date -d"$BDATE" +"%H:%M")"
else
BDATE=$(date -d"$BDATE" +"%F %H:%M")
fi
else
BDATE="NONE"
STATUS="NOT"
fi
OUT+="$BDATE | $STATUS | $X\n"
done
echo $OUT | column -s"|" -t
rm $TMPFILE
@victorypoint
Copy link

victorypoint commented Sep 23, 2020

Very well done. I ran your script on my Synology NAS running the Crashplan app. I had to edit the path lines as you suggested:

#!/bin/bash
LOGFILE=($(ls -rt /volume1/docker/crashplan/config/log/backup_files.log.))
APPLOG=($(ls -rt /volume1/docker/crashplan/config/log/app.log
))
HISTLOG=($(ls -rt /volume1/docker/crashplan/config/log/history.log.*))

I get a few errors when running the script as follows. I'm hoping you can help resolve the errors.

./bstat.sh -a
Destination: CrashPlan_PRO_Online
Last completed backup: 5h21m35s ago
numfmt: invalid suffix in input ‘0.0000000MB[-2]’: ‘B[-2]’
Backup running for: 3h50m35s
Last backup element: /volume1/test/testfile

./bstat.sh testfile
Destination: CrashPlan_PRO_Online
Last completed backup: 5h19m2s ago
numfmt: invalid suffix in input ‘0.0000000MB[-2]’: ‘B[-2]’
Backup running for: 3h48m2s
Last backup element: /volume1/test/testfile


/bstat.sh: line 128: syntax error near unexpected token (' ./bstat.sh: line 128: done < =(cat $TMPFILE | grep "$DIR" | grep "(deleted)$" | cut -d" " -f7- | sed 's/ (deleted)//' | sort | uniq) | sort | uniq'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment