Skip to content

Instantly share code, notes, and snippets.

@thespacedoctor
Last active May 5, 2021 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thespacedoctor/2233e65186cfb5e9f4e7259bc0f77807 to your computer and use it in GitHub Desktop.
Save thespacedoctor/2233e65186cfb5e9f4e7259bc0f77807 to your computer and use it in GitHub Desktop.
[Whitelist Dropbox Folders to Sync] #dropbox #sync #whitelist

Dropbox Whitelister

I use this script on my linux machines to whitelist a single folder located in my ~/Dropbox/server-sync directory. Here I have a separate folder for each of my servers.

To run the script use:

dropbox_whitelist [options] <serverName>

    -k|--keep       keep all byproduct files (use for debugging)
    -h|--help       show this message
    -v|--version    show version number

    serverName      the name of the server folder to retain in server-sync folder

So, to only sync the folder ~/Dropbox/server-sync/soxs-eso-data I would run the command:

dropbox_whitelist soxs-eso-data

The script will need to be run frequently (e.g. via cron) to exclude new Dropbox files and folders generated on other machines.

#!/bin/bash
# SET PATH
PATH=:/usr/local/bin/:/usr/bin/:/Library/Frameworks/Python.framework/Versions/2.7/bin/:/bin/
VERSION=0.1
display_usage() {
echo "
This script will whitelist a folder with a given <serverName> name within the ~/Dropbox/server-sync folder and exclude all other files and folder from syncing to/from Dropbox. Hopefully saving a lot of space!
Usage:
${0##*/} [options] <serverName>
-k|--keep keep all byproduct files (use for debugging)
-h|--help show this message
-v|--version show version number
serverName the name of the server folder to retain in server-sync folder"
}
## DEFAULT OPTIONS & VARIABLES
keep=0 # KEEP ALL BYPRODUCT FILES (USE FOR DEBUGGING)
numberArguments=1 # MINIMUM NUMBER OF COMMAND-LINE ARGUMENTS REQUIRED
# COLLECT CL-ARGUMENTS & PARSE OPTIONS
args=( "$@" )
while [[ $# > 0 ]]
do
key="$1"
case $key in
-h|--help)
display_usage
exit 0
shift
;;
-v|--version)
echo "${0##*/} version $VERSION"
exit 0
;;
-k|--keep)
keep=1
delete=($key)
args=( ${args[@]/$delete/} )
shift
;;
*)
# unknown option
;;
esac
shift
done
# IF LESS THAN THE REQUIRED ARGUMENTS SUPPLIED, DISPLAY USAGE
if [ ${#args[@]} -lt $numberArguments ]
then
display_usage
exit 1
fi
serverName="${args[0]};Icon?"
# change into Dropbox folder
cd "$HOME/Dropbox"
# FIRST EXCLUDE ALL ROOT FOLDERS
WHITELIST="server-sync;Icon?" # files and folders you wish to whitelist
for f in *; do
# IFS = Internal Field Separator
OLDIFS="$IFS"
export IFS=";"
black="true"
for w in $WHITELIST; do
if [ "$f" == "$w" ]; then
black="false"
fi
done
if [ "$black" == "true" ]; then
dropbox exclude add "$f"
fi
export IFS="$OLDIFS"
done
# SECOND EXCLUDE ALL OTHER SERVER FOLDERS
cd "$HOME/Dropbox/server-sync"
for f in *; do
# IFS = Internal Field Separator
OLDIFS="$IFS"
export IFS=";"
black="true"
for w in $serverName; do
if [ "$f" == "$w" ]; then
black="false"
fi
done
if [ "$black" == "true" ]; then
dropbox exclude add "$f"
fi
export IFS="$OLDIFS"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment