Created
September 7, 2021 21:59
-
-
Save matu3ba/f7c06f1d5208e034958d1102ef9f35fa to your computer and use it in GitHub Desktop.
advanced backup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
#quick backup script to mess with nvim config | |
## backup all files except those defined in ExcludeArray | |
## files in ExcludeArray will not be touched | |
## all other files in nvim config are deleted before restoring | |
## USAGE: 1. in $NVIM_PATH: bash backup.sh | |
## 2. in $NVIM_PATH/$BACKUP_FOLDER: bash restore.sh | |
#safer defaults https://bertvv.github.io/cheat-sheets/Bash.html | |
set -o errexit # abort on nonzero exitstatus | |
set -o nounset # abort on unbound variable | |
set -o pipefail # don't hide errors within pipes | |
NVIM_PATH="${HOME}/.config/nvim" | |
BACKUP_FOLDER="backup" | |
RESTORE="restore.sh" | |
declare -a ExcludeArray=("backup.sh" "beta_software" "install" "list") | |
ExcludeArray+=(${BACKUP_FOLDER}) | |
echo "excluding ${ExcludeArray[@]}" | |
if [ -f "${NVIM_PATH}/${BACKUP_FOLDER}" ]; then | |
echo "backup folder can not be created, please remove \"${BACKUP_FOLDER}\"" | |
echo "exiting.." | |
exit 0 | |
fi | |
mkdir ${BACKUP_FOLDER} | |
cd "${NVIM_PATH}" | |
# get all files at top level, that are not in ExcludeArray | |
for i in *; do | |
# how hard can it be to check in one line, if an element is in an array... | |
# https://www.mybluelinux.com/bash-guide-to-bash-arrays/#bash_array_contains_a_value_exact_match | |
if [[ ${ExcludeArray[@]} =~ (^|[[:space:]])"${i}"($|[[:space:]]) ]]; then | |
echo "dont copy: ${i}" | |
else | |
echo "copy recu: ${i}" | |
cp -r "${i}" "${NVIM_PATH}/${BACKUP_FOLDER}" | |
fi | |
done | |
echo "generate restore commands.." | |
cd "${NVIM_PATH}/${BACKUP_FOLDER}" | |
cat > "${RESTORE}" <<EOL | |
#!/bin/bash | |
# execute this script with | |
EOL | |
printf "# bash " >> "${RESTORE}" | |
printf "${RESTORE}\n" >> "${RESTORE}" | |
cat >> "${RESTORE}" <<EOL | |
# if rm fails: stop nvim and delete nvim config with `rm -fr` | |
set -o errexit # abort on nonzero exitstatus | |
set -o nounset # abort on unbound variable | |
set -o pipefail # don't hide errors within pipes | |
NVIM_PATH="\${HOME}/.config/nvim" | |
BACKUP_FOLDER="${BACKUP_FOLDER}" | |
RESTORE="${RESTORE}" | |
EOL | |
# metaprogramming in bash sucks due to missing reflection | |
printf "declare -a ExcludeArray=(" >> "${RESTORE}" | |
for i in "${ExcludeArray[@]}"; do | |
printf "${i} " >> "${RESTORE}" | |
done | |
printf ")\n" >> "${RESTORE}" | |
cat >> "${RESTORE}" <<EOL | |
cd "\${NVIM_PATH}/" | |
# clean nvim config | |
for i in *; do | |
if [[ \${ExcludeArray[@]} =~ (^|[[:space:]])"\${i}"(\$|[[:space:]]) ]]; then | |
echo "dont remove: \${i}" | |
else | |
echo "remove recu: \${i}" | |
rm -r "\${i}" | |
fi | |
done | |
cd \${NVIM_PATH}/\${BACKUP_FOLDER}/ | |
for i in *; do | |
if [[ "\${i}" == "\${RESTORE}" ]]; then | |
echo "dont copy \${i}" | |
continue | |
fi | |
if [[ \${ExcludeArray[@]} =~ (^|[[:space:]])"\${i}"(\$|[[:space:]]) ]]; then | |
echo "dont copy: \${i}" | |
else | |
echo "copy recu: \${i}" | |
cp -r "\${NVIM_PATH}/\${BACKUP_FOLDER}/\${i}" "\${NVIM_PATH}/" | |
fi | |
done | |
EOL | |
echo "done. have fun configuring nvim!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment