#!/bin/bash # -*- coding: utf-8 -*- # # Copyright by Scott Severance # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # file-insurance-cleanup # Deletes all but the most recent backup files created by file-insurance. prog="$(basename $0)" backupDir="$HOME/.file-insurance" escDir="$(echo "$backupDir" | sed 's/\//\\\//g')\/" counter="0" quiet= FILE_COLOR="\033[4;34m" ERROR_COLOR="\033[0;31m" MSG_COLOR="\033[0;32m" NO_COLOR="\033[0m" #Transparent - don't change TITLEBAR_START="\033]0;" TITLEBAR_END="\007" E_USAGE=65 E_NORMAL=0 E_CANCEL=66 function cleanDir { local dir="$1" local oldPwd="$(pwd)" local bakFile= if [[ "$dir" =~ "$backupDir" ]]; then local fullDir="$dir" else local fullDir="$oldPwd/$dir" fi counter="0" builtin cd "$dir" 2> /dev/null IFS="\n" ls -1 --reverse | while read bakFile; do cleanFiles "$bakFile" done builtin cd "$oldPwd" 2> /dev/null rmdir "$dir" 2> /dev/null } function cleanFiles { local bakFile="$1" if [ -d "$bakFile" ]; then cleanDir "$bakFile" return fi local fullFile="$(pwd)/$bakFile" local abbrFile="$(echo "$fullFile" | sed "s/${escDir}//g")" if [[ ! "$bakFile" =~ '^file-insurance\.[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2}\.bz2$' ]]; then echo -e "${ERROR_COLOR}WARNING: Skipping file with unexpected name: ${FILE_COLOR}$abbrFile${NO_COLOR}" 1>&2 return fi if [ "$counter" = "0" ]; then (( counter++ )) [ -z "$quiet" ] && echo -e "${MSG_COLOR}Preserving ${FILE_COLOR}$abbrFile${MSG_COLOR}...${NO_COLOR}" else [ -z "$quiet" ] && echo -e "Removing ${FILE_COLOR}$abbrFile${NO_COLOR}..." rm "$bakFile" (( counter++ )) fi } function usage { echo -e "${MSG_COLOR}Usage:" echo -e "$prog [--quiet|--help]${NO_COLOR}" echo -e "\n$prog cleans up after file-insurance by deleting all backup" echo -e "files it created except the most recent.\n" echo "With the --quiet option, $prog is silent except for errors." } function main { local i= case "$1" in "--help") usage exit $E_USAGE ;; "--quiet") quiet="true" FILE_COLOR= ERROR_COLOR= ;; esac if [ -z "$quiet" ]; then backupDirAbbr="$(echo "$backupDir" | sed "s/^$(echo "$HOME" | sed 's/[\/[{?.+*]/\\&/g')/~/g")" echo -n "Do you want to prune file-insurance's backup directory ($backupDirAbbr)? [y/N] " read answer if [[ ! "$answer" =~ '[Yy]' ]]; then echo "Exiting..." exit $E_CANCEL fi fi cleanDir "$backupDir" exit $E_NORMAL } main "$@"