Skip to main content

folder-backup configurable folder backup wrapper

pknw1logo-white.png

/usr/local/bin/folder-backup


Custom Backup Script

Custom script using a configurable persistent local config to archive and compress the folder contents while excluding any file/folder patterns

#!/bin/bash
TARGET='/dev/shm'
LOGPATH='/dev/shm'

if [[ $# -eq 0 ]]
then
  SOURCE=$(pwd)
else
  SOURCE="${1}"
fi


if ! [[ -f "${SOURCE}/.backup-options" ]]
then
   FULL_PATH=$(realpath "${SOURCE}")
   SOURCE_FOLDER=$(echo "${FULL_PATH}" | awk -F/ '{print $NF}')

   touch "${FULL_PATH}/.backup-options"
   cat <<EOF > "${FULL_PATH}/.backup-options"
BACKUP_NAME=${SOURCE_FOLDER}
BACKUP_ROOT=${FULL_PATH}
EXCLUDE=(.git .venv)
DESTINATION=${TARGET}
EOF

echo "created config file .backup-options - update exclusions"
cat "${FULL_PATH}/.backup-options"

else
  source "${SOURCE}/.backup-options"
  excludes=()    # start with an empty array
  for excl in  ${EXCLUDE[*]} ; do    # for each extra argument...
    excludes+=(--exclude "$excl")    # add an exclude to the array
  done

  if [[ -f ${DESTINATION}/${BACKUP_NAME}.tar ]]
  then
    BACKUP_OPTIONS="uzvf"
  else
    BACKUP_OPTIONS="czvf"
  fi

  echo $(pwd)
  cat << EOF >> ${LOGPATH}/${BACKUP_NAME}.log
  
  $(date) start backup
EOF

  tar -${BACKUP_OPTIONS} ${DESTINATION}/${BACKUP_NAME}.tgz ${excludes[@]} * 2>&1 >> ${LOGPATH}/${BACKUP_NAME}.log

  cat << EOF >> ${LOGPATH}/${BACKUP_NAME}.log
  
  $(date) end backup
EOF
fi

if [ -f  ${LOGPATH}/${BACKUP_NAME}.log ]
	then
	tail -n20  ${LOGPATH}/${BACKUP_NAME}.log
fi

execution

  • either change to the directory that is targetted for backup or pass the full path as a paramater
  • 1st run creates the local .backup-options file with no backup
BACKUP_NAME=sso
BACKUP_ROOT=/home/docker/config/sso
EXCLUDE=(.git .venv)
DESTINATION=/dev/shm
  • 2nd and subsequent executions read the backup parameters from the config file and tar/compress the backup


further info


Product Home

Link

Documentation

Link

Github

Link

DockerHub

Link

Misc

Link

more text more text