Skip to content

Instantly share code, notes, and snippets.

@DennisLfromGA
Last active September 5, 2021 15:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DennisLfromGA/81b9275b46e1f6072b0e46f33ca82536 to your computer and use it in GitHub Desktop.
Save DennisLfromGA/81b9275b46e1f6072b0e46f33ca82536 to your computer and use it in GitHub Desktop.
A script to list & backup brioche containers
#!/bin/bash
# brio-backup - A script to list & backup brioche containers
##
#######################
### Setup variables ###
#######################
##
APPL="${0##*/}"
BACKEDUP=''
BACKUPS=''
BEGIN=''
## was: DATE="$(date '+%Y%m%d-%H%M')"
DATE="$(date '+%F-%I%M%P')"
DOWNLOADS="${DOWNLOADS:-${HOME}/Downloads}"
FAILED=''
OPT=''
USAGE="
====================================================================
= PLEASE NOTE: This script is deprecated and replaced by 'brio-br' =
====================================================================
## brio-backup - A script to list & backup brioche containers
USAGE: '${APPL}' backs up brioche containers found under '~/brioche/'
to '${DOWNLOADS}' (default: ${HOME}/Downloads).
The script will prompt for each container before proceeding.
Optionally displays the script version, version history & help/usage (this blurb).
Options:
-c Check for & list brioche containers found in ~/brioche
-h Displays this help/usage message and exits
-v Displays the current version of '${APPL}' and exits
-V Displays version number plus version history & exits
====================================================================
= Please install 'brio-br' using 'install.brio-br' =
====================================================================
"
# VERSION format: "n.n.$(date +%Y%m%d%H%M)"
VERSION='3.2.202109051056'
VERHIST="\
${APPL}-3.2.202109051056 : This script is deprecated and replaced by 'brio-br'
${APPL}-3.1.202108241404 : Removed routine to check & relaunch in bash if needed.
${APPL}-3.0.202108241216 : Renamed script from brioche-backup to brio-backup
${APPL}-3.0.202108241216 + and backup file name from brioche-... to end with .brio
${APPL}-2.3.202108232359 : cleaned up b_do_backup function
${APPL}-2.2.202108231737 : Add command-line options [-v,-V,-h]
${APPL}-2.1.202108231715 : Use 'pv' when installed instead of 'spinner'
${APPL}-2.0.202108231226 : Converted conditionals, etc. to functions
${APPL}-1.1.202108221629 : Added version and version history
${APPL}-1.0.202108111731 : Created $APPL and made 12 revisions"
##
#######################
### Setup functions ###
#######################
##
### functions with b/r prefix are for backup/restore respectively
##
### Following two functions, error & spinner, borrowed from the 'crouton' authors
##
## exit with exit code $1, spitting out message $@ to stderr
echo_e='/bin/echo -e'
error() {
local ecode="${1}"
shift
${echo_e} "${*}" 1>&2
exit "${ecode}"
}
##
## Prints out a fancy spinner that updates every time a line is fed in, unless
## the output is not to a tty, in which case it just prints a new line.
# $1: number of lines between each update of the spinner
# $2...: Command to be run
# Erases the line each time, so it will always be at position 0.
# Either expect this and put text later in the line, or give this its own line.
spinner() {
local spin="$1"
shift
if [ -t 2 ]; then
# Keep track of the exit status of the piped command
local ret="`(("$@" || echo "$?" 1>&3) | mawk -Winteractive '
BEGIN {
printf "\r"
}
{
y = (y+1) % '"$spin"'
if (y == 0) {
x = (x+1) % 4
printf substr("\|/-", x+1, 1) "\r"
}
}' 1>&2) 3>&1`"
if [ -n "$ret" ]; then
return "$ret"
fi
else
echo 1>&2
"$@" 1>/dev/null
fi
}
##
check_for_opts() {
## Get command line parameters
local OPTIND
while getopts chvV OPT; do
case ${OPT} in
c) b_check_for_brioche
error 0 "${USAGE}";;
h) error 0 "$USAGE";;
v) echo "$VERHIST" | grep $VERSION 1>&2; exit 0;;
V) error 0 "VERSION:${VERSION}\n${VERHIST}" ;;
\?) error 2 "$USAGE";;
esac
done
shift "$((OPTIND-1))"
}
##
b_check_for_brioche() {
if [ ! -d ${HOME}/brioche ]; then
echo "Sorry, no '~/brioche/' directory exists."
error 1 "If they are under /root/brioche please move them under ~/brioche/."
elif ! ls ${HOME}/brioche/* 2>/dev/null 1>&2; then
error 1 "Sorry, no brioche containers found under ~/brioche/."
else
sudo chown chronos:root ${HOME}/brioche 2>/dev/null
cd ${HOME}/brioche/
echo "Found the followoing brioche container candidates for backup: "
echo; ls | sed 's/^/ /' 2>/dev/null
fi
}
##
b_ask_for_backup() {
local CONT OPER
cd ${HOME}/brioche/ 1>/dev/null
for CONT in $(ls); do
sudo chown chronos:root ${HOME}/brioche/${CONT} 2>/dev/null
cd ${HOME}/brioche/${CONT} 1>/dev/null
echo
echo "Backup brioche container '${CONT}' to ${DOWNLOADS} [Ysra] ? "
read -n 1 -p "PRESS 'y' to backup, 's' to SKIP, 'r' to RESTART or 'a' to ABORT! " OPER
if [ -n "${OPER}" ]; then
case ${OPER} in
[yY])
echo; echo "Got '${OPER}', '${CONT}' added to backups ..."
BACKUPS="${BACKUPS} ${CONT}"
;;
[sS]|[nN])
echo; echo "Got '${OPER}', so Skipping '${CONT}' ..."
continue
;;
[rR])
echo; echo "Got '${OPER}', so Restarting ..."
BACKUPS=''
b_ask_for_backup
;;
[aA]|[xX])
echo; echo -n "Got '${OPER}'"
error 0 "Okay, ABORTING ..."
;;
*)
echo; echo "Got '${OPER}', no such option '${OPER}' ..."
echo "Only 'y' to backup, 's' to SKIP, 'r' to RESTART or 'a' to ABORT are accepted."
error 1 "${USAGE}"
;;
esac
else
echo; echo "Got 'ENTER', '${CONT}' added to backups ..."
BACKUPS="${BACKUPS} ${CONT}"
fi
done
BACKUPS="$(echo ${BACKUPS}|sed 's/^ //')"
echo
echo -n "Ready to backup container(s) '${BACKUPS}', press 'ENTER' to begin or 'Ctrl-C' to ABORT ..."
read BEGIN
}
##
b_do_backup() {
local CONT
for CONT in ${BACKUPS}; do
echo
echo "Stopping '${CONT}' - just in case ..."
brioche ${CONT} stop 2>/dev/null
echo "Backing up '${CONT}' to '${DOWNLOADS}/${CONT}.${DATE}.brio' ..."
sudo rm -f "${DOWNLOADS}/${CONT}.${DATE}.brio" 2>/dev/null
trap "rm -f '${DOWNLOADS}/${CONT}.${DATE}.brio'; \
error 2 \"Back up of '${CONT}' aborted, file deleted!\"" INT HUP TERM 0
if command -v pv 1>/dev/null; then
# "'pv' is installed."
cd ${HOME}/brioche/${CONT} 1>/dev/null
sudo tar -cf - . | pv -Ws $(sudo du -sb . 2>/dev/null | awk '{print $1}') | gzip > \
${DOWNLOADS}/${CONT}.${DATE}.brio || FAILED="${FAILED} ${CONT}"
if [ -n "${FAILED}" ]; then
sudo rm -f "${DOWNLOADS}/${CONT}.${DATE}.brio"; \
echo "Back up of '${CONT}' failed!"
fi
else
# "'pv' is NOT installed."
spinner 10 sudo tar --checkpoint=10 --checkpoint-action=exec=echo --numeric-owner \
-czf ${DOWNLOADS}/${CONT}.${DATE}.brio . 1>&2 || FAILED="${FAILED} ${CONT}"
if [ -n "${FAILED}" ]; then
sudo rm -f "${DOWNLOADS}/${CONT}.${DATE}.brio"; \
echo "Back up of '${CONT}' failed!"
fi
fi
trap - INT HUP TERM 0
sudo chown chronos:root ${DOWNLOADS}/${CONT}.${DATE}.brio 2>/dev/null
echo "Back up of '${CONT}' finished ..."
BACKEDUP="${BACKEDUP} ${CONT}"
done
BACKEDUP="$(echo ${BACKEDUP}|sed 's/^ //')"
FAILED="$(echo ${FAILED}|sed 's/^ //')"
}
##
b_report_results() {
echo
if [ -n "${FAILED}" ]; then
echo "Backups FAILED for brioche container(s) ${FAILED} !"
fi
if [ -n "${BACKEDUP}" ]; then
echo "Backups complete for brioche container(s) ${BACKEDUP} - see below:"
ls -l ${DOWNLOADS}/*.${DATE}.brio 2>/dev/null
else
echo "No brioche container backups created ..."
fi
echo
}
##
####################
### Main section ###
####################
##
check_for_opts "$@"
error 0 "${USAGE}"
##
if [ ${UID:-$(id -u)} -eq 0 ]; then
echo
echo "ERROR: ${APPL} must NOT be run as root!"
error 1 "${USAGE}"
fi
check_for_opts "$@"
b_check_for_brioche
b_ask_for_backup
b_do_backup
b_report_results
exit 0
@DennisLfromGA
Copy link
Author

PLEASE NOTE: This script is deprecated and replaced by 'brio-br'

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