# folder-backup configurable folder backup wrapper

[![pknw1logo-white.png](https://bookstack.notflix.pknw1.co.uk/uploads/images/gallery/2025-01/scaled-1680-/HLPpknw1logo-white-png.png)](https://bookstack.notflix.pknw1.co.uk/uploads/images/gallery/2025-01/scaled-1680-/HLPpknw1logo-white-png.png)

##### `<span class="editor-theme-code">/usr/local/bin/folder-backup</span>`

<u>****Custom Backup Script****</u>

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

<details id="bkmrk-script-infocheck-mar">- [ ] either change to the directory that is targetted for backup or pass the full path as a paramater
- [ ] <span style="white-space: pre-wrap;">1st run creates the local </span>`<span class="editor-theme-code">.backup-options</span>`<span style="white-space: pre-wrap;"> file with </span>****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

</details><u>****further info****</u>

<details id="bkmrk-product-homelinkdocu"><table><colgroup><col style="width: 240px;"></col><col style="width: 240px;"></col></colgroup><tbody><tr><td><u>****Product Home****</u>

</td><td>[Link](https://www.pknw1.co.uk)

</td></tr><tr><td><u>****Documentation****</u>

</td><td>[Link](https://www.pknw1.co.uk)

</td></tr><tr><td><u>****Github****</u>

</td><td>[Link](https://www.pknw1.co.uk)

</td></tr><tr><td><u>****DockerHub****</u>

</td><td>[Link](https://www.pknw1.co.uk)

</td></tr><tr><td><u>****Misc****</u>

</td><td>[Link](https://www.pknw1.co.uk)

</td></tr></tbody></table>

</details>more text more text