Skip to content

Instantly share code, notes, and snippets.

@nhalstead
Last active August 29, 2022 18:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhalstead/2a2607043693ec140d8b07f71e7063e1 to your computer and use it in GitHub Desktop.
Save nhalstead/2a2607043693ec140d8b07f71e7063e1 to your computer and use it in GitHub Desktop.
This will exclude Node_modules, Vendor (Composer), and Build Folders from the Backup that is run by Deja Dup Backup
#!/bin/bash
# Update Deja Dup Backup Exclude Config for the node_modules folders.
# This will only auto exclude if a package.json file and a .git folder exist.
# This also will update the list for excluding backups of vendor <composer> folders.
if [[ ! -x "$(command -v yq)" ]]; then
echo "'yq' is required. Please install 'yq'. Try 'snap install yq'"
exit 1;
fi
if [[ ! -x "$(command -v sponge)" ]]; then
echo "'sponge' is required. Please install 'sponge'. Try 'sudo apt-get install moreutils'"
exit 1;
fi
tmp_dir=$(mktemp -d -t dejadup_exclude-XXXXXXXXXX)
# Export the current Deja Dup Config
dconf read /org/gnome/deja-dup/exclude-list > "${tmp_dir}/exclude-list.json"
sed "s/'/\"/g" "${tmp_dir}/exclude-list.json" | sponge "${tmp_dir}/exclude-list.json"
jq -r '.[]' "${tmp_dir}/exclude-list.json" > "${tmp_dir}/exclude-list-draft.txt"
touch "${tmp_dir}/exclude-list-draft.txt"
# Check that every directory listed in the current exclude list still exists.
touch "${tmp_dir}/exclude-list.txt"
while IFS='' read -r LINE || [ -n "${LINE}" ]; do
if [ -d "${LINE}" ]; then
# Project still exists, add this to the list.
echo "${LINE}" >> "${tmp_dir}/exclude-list.txt"
else
echo "1 Does not exist: ${LINE}"
fi
done < "${tmp_dir}/exclude-list-draft.txt"
#
# General Test Data folders for NPM node_modules folder
#
# Search the profile documents for the node_module(s) folders.
find ~/Documents -maxdepth 8 -type d -name 'node_modules' 2> /dev/null > "${tmp_dir}/node_modules_folders.txt"
# Check that every directory listed is a project with package.json file.
touch "${tmp_dir}/node_modules_exclude.txt"
while IFS='' read -r LINE || [ -n "${LINE}" ]; do
if [ -f $(readlink -f "${LINE}/../package.json") ] & [ -d $(readlink -f "${LINE}/../.git") ]; then
# Put this directory in the list, Its apart of a project with
# a package.json
echo "${LINE}" >> "${tmp_dir}/node_modules_exclude.txt"
else
echo "2 Does not exist: ${LINE}"
fi
done < "${tmp_dir}/node_modules_folders.txt"
#
# General Test Data folders for Composer Vendor folder
#
# Search the profile documents for the vendor(s) folders.
find ~/Documents -maxdepth 5 -type d -name 'vendor' 2> /dev/null > "${tmp_dir}/composer_packages_folders.txt"
# Check that every directory listed is a project with composer.json file.
touch "${tmp_dir}/composer_packages_exclude.txt"
while IFS='' read -r LINE || [ -n "${LINE}" ]; do
if [ -f $(readlink -f "${LINE}/../composer.json") ] & [ -d $(readlink -f "${LINE}/../.git") ]; then
# Put this directory in the list, Its apart of a project with
# a composer.json
echo "${LINE}" >> "${tmp_dir}/composer_packages_exclude.txt"
else
echo "3 Does not exist: ${LINE}"
fi
done < "${tmp_dir}/composer_packages_folders.txt"
#
# General Test Data folders for build outputs
#
# Search the profile documents for the build(s) folders.
find ~/Documents -maxdepth 5 -type d -name 'build' 2> /dev/null > "${tmp_dir}/build_folders.txt"
# Check that every directory listed is a git repo with a build directory.
touch "${tmp_dir}/build_exclude.txt"
while IFS='' read -r LINE || [ -n "${LINE}" ]; do
if [ -d "${LINE}/../.git" ]; then
# Put this directory in the list, Its apart of a git repo
echo "${LINE}" >> "${tmp_dir}/build_exclude.txt"
else
echo "4 Does not exist: ${LINE}"
fi
done < "${tmp_dir}/build_folders.txt"
#
# General Test Data folders for mongo db
#
# Search the profile documents for the mongodb(s) folders.
find ~/Documents -maxdepth 5 -type d -name 'mongo' 2> /dev/null > "${tmp_dir}/mongo_folders.txt"
# Check that every directory listed is a git repo with a build directory.
touch "${tmp_dir}/mongo_exclude.txt"
while IFS='' read -r LINE || [ -n "${LINE}" ]; do
if [ -d "${LINE}/../.git" ] & [ -d "${LINE}/data" ]; then
# Put this directory in the list, Its apart of a git repo
# and has a data folder.
echo "${LINE}" >> "${tmp_dir}/mongo_exclude.txt"
else
echo "5 Does not exist: ${LINE}"
fi
done < "${tmp_dir}/mongo_folders.txt"
#
# General Test Data folders for docker volumns (vols)
#
# Search the profile documents for the mongodb(s) folders.
find ~/Documents -maxdepth 5 -type d -name 'vols' 2> /dev/null > "${tmp_dir}/vols_folders.txt"
# Check that every directory listed is a git repo with a build directory.
touch "${tmp_dir}/vols_exclude.txt"
while IFS='' read -r LINE || [ -n "${LINE}" ]; do
# echo "${LINE}"
if [ -d "$(dirname $LINE)/.git" ] & [ -d "$(dirname $LINE)/vols" ]; then
# Put this directory in the list, Its apart of a git repo
# and has a data folder.
echo "${LINE}" >> "${tmp_dir}/vols_exclude.txt"
else
echo "6 Does not exist: ${LINE}"
fi
done < "${tmp_dir}/vols_folders.txt"
#
# Submodules
#
# Find all Git Repos and exclude all submodules
find ~/Documents -maxdepth 8 -type d -name '.git' 2> /dev/null > "${tmp_dir}/git_folders.txt"
# Check each of the repos to omit the git repos.
touch "${tmp_dir}/gitsub_exclude.txt"
while IFS='' read -r LINE || [ -n "${LINE}" ]; do
pushd "$LINE/.." > /dev/null
while IFS='' read -r LINESUB || [ -n "${LINESUB}" ]; do
# Push the Submodule to the temp file.
echo "${LINESUB}" >> "${tmp_dir}/gitsub_exclude.txt"
done < <(git config --file .gitmodules --get-regexp $PWD | awk '{ print $2 }')
popd > /dev/null
done < "${tmp_dir}/git_folders.txt"
#
# Check for node_modules folders in the .config folder
#
# Search the profile documents for the node_module(s) folders.
find ~/.config -maxdepth 6 -type d -name 'node_modules' 2> /dev/null > "${tmp_dir}/config_node_modules_folders.txt"
# Check that every directory listed is a project with package.json file.
touch "${tmp_dir}/config_node_modules_exclude.txt"
while IFS='' read -r LINE || [ -n "${LINE}" ]; do
# Ensure this is not a sub-node_modules
if [ $(readlink -f "${LINE}/../") != *"node_modules"* ]; then
# Put this directory in the list, Its apart of a project with
# a package.json
echo "${LINE}" >> "${tmp_dir}/config_node_modules_exclude.txt"
else
echo "7 Does not exist: ${LINE} $(readlink -f "${LINE}/../")"
fi
done < "${tmp_dir}/config_node_modules_folders.txt"
#
# Check for Cache folders in the .config folder
#
# Search the profile documents for the mongodb(s) folders.
find ~/.config -maxdepth 6 -type d -iname 'cache' 2> /dev/null > "${tmp_dir}/cache_folders.txt"
# Check that every directory listed is a git repo with a build directory.
touch "${tmp_dir}/cache_exclude.txt"
while IFS='' read -r LINE || [ -n "${LINE}" ]; do
# echo "${LINE}"
echo "${LINE}" >> "${tmp_dir}/cache_exclude.txt"
done < "${tmp_dir}/cache_folders.txt"
#
# Check for Terraform vendor folders in projects
#
# Search the profile documents for the build(s) folders.
find ~/Documents -maxdepth 5 -type d -name '.terraform' 2> /dev/null > "${tmp_dir}/terraform_folders.txt"
# Check that every directory listed is a git repo with a build directory.
touch "${tmp_dir}/terraform_exclude.txt"
while IFS='' read -r LINE || [ -n "${LINE}" ]; do
echo "${LINE}" >> "${tmp_dir}/terraform_exclude.txt"
done < "${tmp_dir}/terraform_folders.txt"
# Merge Node_Modules_Exclude and the current list Items into one file.
cat "${tmp_dir}/exclude-list.txt" \
"${tmp_dir}/node_modules_exclude.txt" \
"${tmp_dir}/composer_packages_exclude.txt" \
"${tmp_dir}/build_exclude.txt" \
"${tmp_dir}/mongo_exclude.txt" \
"${tmp_dir}/vols_exclude.txt" \
"${tmp_dir}/gitsub_exclude.txt" \
"${tmp_dir}/config_node_modules_exclude.txt" \
"${tmp_dir}/cache_exclude.txt" \
"${tmp_dir}/terraform_exclude.txt" \
> "${tmp_dir}/exclude-list-final.txt"
# Remove Duplicates (While also sorting)
sort -u "${tmp_dir}/exclude-list-final.txt" | sponge "${tmp_dir}/exclude-list-final.txt"
# Compile the output of the list to a JSON Array
jq -ncR '[inputs]' <<< "$(cat "${tmp_dir}/exclude-list-final.txt")" > "${tmp_dir}/exclude-list.json"
# Push the new list of ignore to DejaDup Config and Cleanup
# gsettings set org.gnome.DejaDup exclude-list "$(cat "${tmp_dir}/exclude-list.json")"
dconf write /org/gnome/deja-dup/exclude-list "$(cat "${tmp_dir}/exclude-list.json")"
cat "${tmp_dir}/exclude-list.json"
cp -r "${tmp_dir}/exclude-list-final.txt" "$HOME/dejadup-exclude.txt"
rm -r "${tmp_dir}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment