Skip to content

Instantly share code, notes, and snippets.

@DennisLfromGA
Last active May 8, 2021 23:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DennisLfromGA/450e3b7586ad122050646e786e33d9c1 to your computer and use it in GitHub Desktop.
Save DennisLfromGA/450e3b7586ad122050646e786e33d9c1 to your computer and use it in GitHub Desktop.
Perfroms a chroot backup ensuring enough space is available.
#!/bin/sh
ANSWER=''
APPLICATION="${0##*/}"
AVAIL=''
AVAILGB=''
BACKTAG=''
BACKUP=''
CHROOT=''
CHROOTS=''
CHROOTSIZE=''
CHROOTSIZEGB=''
DATE="$(date '+%Y%m%d-%H%M')"
DESTDIR=''
MOUNTPT=''
SIZE=''
SIZEGB=''
USED=''
USEDGB=''
echo_e='/bin/echo -e'
###
### If we're not running as root, restart as root.
if [ ${UID:-$(id -u)} -ne 0 ]; then
${echo_e} "...elevating ${USER} to superuser..."
exec sudo /bin/sh -e "$0" "$@"
fi
###
### Set critical vars
CHROOT="${1:-list}"
DESTDIR="${2:-$(pwd)}"
BACKTAG="${3}"
if [ -n "$BACKTAG" ]; then BACKTAG="_${BACKTAG}"; fi
MOUNTPT="$(dirname ${DESTDIR})"
###
### Find CHROOTS
if [ -d /var/crouton/chroots ]
then CHROOTS="/var/crouton/chroots"
else CHROOTS="/usr/local/chroots"
fi
###
### Check for args
if [ -z "${1}" ];then
${echo_e} "USAGE: ${APPLICATION} [chroot_name | list] [backup_location] [backup_tag]\n"
elif [ "${1}" = '-h' ];then
${echo_e} "USAGE: ${APPLICATION} [chroot_name | list] [backup_location] [backup_tag]\n"
exit
fi
###
### Get mountpoint
while :
do
if grep -q " ${MOUNTPT} " /proc/mounts;
then break
else MOUNTPT="$(dirname ${MOUNTPT})"; continue
fi
done
###
### Set more vars
${echo_e} -n "Gathering size requirements, etc. ..."
DATE=$(date '+%Y%m%d-%H%M')
BACKUP="${CHROOT}-${DATE}${BACKTAG}.tar.gz"
CHROOTSIZE="$(sudo du --max-depth=0 ${CHROOTS}/${CHROOT} 2>/dev/null | awk '{print $1}')"
CHROOTSIZEGB="$(sudo du -h --max-depth=0 ${CHROOTS}/${CHROOT} 2>/dev/null | awk '{print $1}')"
SIZE=$(df ${MOUNTPT} 2>/dev/null | grep -v ^File | awk '{print $2}')
USED=$(df ${MOUNTPT} 2>/dev/null | grep -v ^File | awk '{print $3}')
AVAIL=$(df ${MOUNTPT} 2>/dev/null | grep -v ^File | awk '{print $4}')
SIZEGB=$(df -h ${MOUNTPT} 2>/dev/null | grep -v ^File|awk '{print $2}')
USEDGB=$(df -h ${MOUNTPT} 2>/dev/null | grep -v ^File|awk '{print $3}')
AVAILGB=$(df -h ${MOUNTPT} 2>/dev/null | grep -v ^File|awk '{print $4}')
${echo_e}
###
### Check for valid chroot(s)
if ! [ -d ${CHROOTS}/${CHROOT} ]; then
if [ "${CHROOT}" = "list" ]; then
# ${echo_e} "Your chroots: $(ls -m "$CHROOTS")\n"
${echo_e} "\nValid CHROOT(S) and uncompressed size(s) are:\n"
for each in `ls -1 $CHROOTS`; do
CTR=$(($CTR + 1))
CHROOTSIZEGB="$(sudo du -h --max-depth=0 ${CHROOTS}/${each} 2>/dev/null | awk '{print $1}')"
${echo_e} " $CTR) $each: $CHROOTSIZEGB"
CRSIZE="$(echo $CHROOTSIZEGB|sed 's/G$//')"
STOTAL="$(echo $STOTAL + $CRSIZE)"
done
TOTAL="$(echo "$STOTAL" | sed 's/^+ //' | bc)"
echo
${echo_e} " TOTAL CHROOTS: ${TOTAL}G"
echo
exit 0
fi
${echo_e} "\nSorry no CHROOT named '${CHROOT}' was found in ${CHROOTS} - aborting...\n"
${echo_e} "Valid CHROOT(S) are: $(ls -m ${CHROOTS})\n"
exit 1
fi
###
### Show destination size
${echo_e} "\n${APPLICATION}: Back up of crouton chroot: '${CHROOT}'\n"
${echo_e} "Your disk size in '${DESTDIR}' is below:\n"
df -h ${MOUNTPT}
${echo_e} "\nThe size of '${CHROOT}' is: ${CHROOTSIZEGB} (uncompressed)\n"
###
### Notify of space available
if ! [ "${AVAIL}" -gt "${CHROOTSIZE}" ]; then
${echo_e} "\nYou don't have enough free space left in '${DESTDIR}' to backup your chroot, boo!\n"
${echo_e} "\nFree-up more space or save your backup to external media.\n"
exit 1
else
${echo_e} "You have sufficient free space for a backup, yay!\n"
fi
###
### Request backup operation
${echo_e} "Backup filename will be: ${DESTDIR}/${BACKUP}\n"
echo -n "Perform backup of '${CHROOTS}/${CHROOT}' (Y/n/q) ? " 1>&2
read ANSWER; if [ -z "${ANSWER}" ]; then ANSWER='y'; fi
case "${ANSWER}" in
[yY]*) echo ;;
*) ${echo_e} "\nQuitting...\n"; exit 0;;
esac
###
### Perform backup
tar cf - ${CHROOTS}/${CHROOT}/ -P 2>/dev/null |\
pv -i 5 -w 80 -berps $(du -sb ${CHROOTS}/${CHROOT} |\
awk '{print $1}') |\
gzip > ${DESTDIR}/${BACKUP}
###
### Show compressed backup file
chown ${SUDO_USER}:${SUDO_USER} ${DESTDIR}/${BACKUP}
${echo_e} "\nBackup of '${CHROOTS}/${CHROOT}' done.\n"
${echo_e} "The file is stored in '${DESTDIR}\n"
ls -hlF "${DESTDIR}/${BACKUP}"
echo
@DennisLfromGA
Copy link
Author

Cleaned up and fixed mount point and destination directory.

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