#! /bin/bash set -eu -o pipefail burpconf=/etc/burp/burp.conf numFiles=2 restoreFolder=/tmp/test_restauration IFS=$(echo -ne '\b\n') if [ $# -gt 0 ] ; then cmdLine="-C $1" fi function _getRandomBurpFolders { folderList=$(awk -F " = " '/^include/{ print $2}' $burpconf) echo $folderList | sort --random-sort | tail -n 1 } function _getRandomBackup { burp -a l | awk '{ print $2}' | sort --random-sort | tail -n 1 } function _cacheStale { # 1 if given file older than 1 week # 0 otherwhise # file age in seconds = current_time - file_modification_time. test -f $1 || return 2 fileage=$(($(date +%s) - $(stat -c '%Y' "$1"))) limit=$((7*24*3600)) diff=$((fileage-$limit )) if [ $diff -lt 0 ] ; then return 0 ; else return 1 ; fi } function _getRandomFile { f=$(_getRandomBurpFolders ) #f=/home/tom/Documents/Opendoor/Developpement/Scripts/Backups/BurpCheck/Test test -d "${f}" || { echo "error opening $f" ; exit 1 ; } cacheFile=/tmp/burp_cache_$(echo $f | md5sum | cut -f1 -d ' ') if ( $(_cacheStale "$cacheFile" )) ; then readarray -d '' files < <(shuf -z $cacheFile) else readarray -d '' files < <(find $f -type f -print0 | shuf -z | tee $cacheFile) fi } backup=$(_getRandomBackup) _getRandomFile c=0 for file in "$files" ; do let "c+=1" echo "$file" | grep -q .git && break echo "Trying to restore '${file}' from backup #${backup}" burp -a r -b ${backup} -r "${file}" -d ${restoreFolder} test $c -eq $numFiles || continue done exit $?