-
-
Save alphapapa/d3235e69aec1293475d96dc366977f92 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
# -*- coding: UTF8 -*- | |
# Author: Guillaume Bouvier -- [email protected] | |
# 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 -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 'ServiceModel.cpcLicense.expires = ' $APPLOG | awk -F" = " '{print $2}')" | |
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 | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment