Skip to content

Instantly share code, notes, and snippets.

@felipealfonsog
Last active May 16, 2026 13:19
Show Gist options
  • Select an option

  • Save felipealfonsog/88b90857ebf79c8599cff360b3227ee2 to your computer and use it in GitHub Desktop.

Select an option

Save felipealfonsog/88b90857ebf79c8599cff360b3227ee2 to your computer and use it in GitHub Desktop.
Install Dropbox as a service
#!/usr/bin/env bash
set -euo pipefail
WRAPPER="$HOME/.local/bin/dropbox"
DROPBOXD="$HOME/.dropbox-dist/dropboxd"
DROPBOX_CLI="$HOME/.local/lib/dropbox/dropbox.py"
SERVICE_DIR="$HOME/.config/systemd/user"
SERVICE="$SERVICE_DIR/dropbox.service"
install_deps() {
if command -v pacman >/dev/null 2>&1; then
sudo pacman -S --needed curl tar python xdg-utils
elif command -v apt >/dev/null 2>&1; then
sudo apt update
sudo apt install -y curl tar python3 xdg-utils
elif command -v dnf >/dev/null 2>&1; then
sudo dnf install -y curl tar python3 xdg-utils
else
echo "Install manually: curl tar python3 xdg-utils"
fi
}
install_daemon() {
if [[ -x "$DROPBOXD" ]]; then
echo "Dropbox daemon already installed."
return
fi
cd "$HOME"
case "$(uname -m)" in
x86_64) PLAT="lnx.x86_64" ;;
i386|i686) PLAT="lnx.x86" ;;
*) echo "Unsupported architecture: $(uname -m)"; exit 1 ;;
esac
curl -L "https://www.dropbox.com/download?plat=${PLAT}" | tar xzf -
}
install_cli() {
mkdir -p "$(dirname "$DROPBOX_CLI")"
curl -L -o "$DROPBOX_CLI" "https://www.dropbox.com/download?dl=packages/dropbox.py"
chmod +x "$DROPBOX_CLI"
}
install_service() {
mkdir -p "$SERVICE_DIR"
cat > "$SERVICE" <<EOF
[Unit]
Description=Dropbox
After=network-online.target
[Service]
Type=simple
ExecStart=$DROPBOXD
Restart=on-failure
RestartSec=10
[Install]
WantedBy=default.target
EOF
systemctl --user daemon-reload
systemctl --user enable dropbox.service
}
install_wrapper() {
mkdir -p "$HOME/.local/bin"
cat > "$WRAPPER" <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
DROPBOXD="$HOME/.dropbox-dist/dropboxd"
DROPBOX_CLI="$HOME/.local/lib/dropbox/dropbox.py"
help() {
cat <<HELP
Dropbox Linux Manager
Usage:
dropbox --help
dropbox --login
dropbox --start
dropbox --stop
dropbox --enable
dropbox --disable
dropbox --restart
dropbox --status
dropbox --logs
dropbox --open
dropbox --exclude-list
dropbox --exclude-add <folder>
dropbox --exclude-remove <folder>
dropbox --service-status
dropbox --uninstall
Flow:
1. dropbox --login
2. Browser opens Dropbox authorization page
3. Log in / approve Dropbox permissions
4. Dropbox starts syncing into ~/Dropbox
HELP
}
cli() {
python3 "$DROPBOX_CLI" "$@"
}
wait_for_dropbox() {
LAST_STATUS=""
for _ in $(seq 1 120); do
STATUS="$(cli status 2>/dev/null || true)"
if [[ -z "$STATUS" ]]; then
sleep 1
continue
fi
if [[ "$STATUS" == "$LAST_STATUS" ]]; then
sleep 1
continue
fi
LAST_STATUS="$STATUS"
case "$STATUS" in
*"Dropbox isn't running"*)
;;
*"Connecting..."*)
echo "Connecting to Dropbox..."
;;
*"Starting..."*)
echo "Starting Dropbox..."
;;
*"Indexing"*|*"Syncing"*|*"Downloading"*|*"Uploading"*)
echo "$STATUS"
;;
*"Up to date"*)
echo
echo "Dropbox connected successfully."
echo "Status: Up to date"
return 0
;;
*)
echo
echo "Dropbox connected successfully."
echo "Status: $STATUS"
return 0
;;
esac
sleep 1
done
return 1
}
login_flow() {
systemctl --user stop dropbox.service 2>/dev/null || true
echo "Starting Dropbox authorization flow..."
echo "A browser window should open."
echo "Log in to Dropbox there and approve this computer."
echo
TMP_LOG="$(mktemp)"
"$DROPBOXD" > "$TMP_LOG" 2>&1 &
PID="$!"
URL_OPENED=0
for _ in $(seq 1 180); do
if grep -qE 'https://www\.dropbox\.com/cli_link_nonce[^[:space:]]+' "$TMP_LOG"; then
URL="$(
grep -oE \
'https://www\.dropbox\.com/cli_link_nonce[^[:space:]]+' \
"$TMP_LOG" | tail -n1
)"
if [[ "$URL_OPENED" -eq 0 ]]; then
echo "Opening Dropbox authorization page:"
echo "$URL"
xdg-open "$URL" >/dev/null 2>&1 || true
URL_OPENED=1
fi
fi
if [[ -f "$HOME/.dropbox/host.db" ]]; then
echo
echo "Dropbox linked successfully."
kill "$PID" 2>/dev/null || true
wait "$PID" 2>/dev/null || true
rm -f "$TMP_LOG"
systemctl --user restart dropbox.service
echo
echo "Dropbox service started."
echo "Sync folder:"
echo " $HOME/Dropbox"
echo
echo "Waiting for Dropbox daemon..."
if ! wait_for_dropbox; then
echo
echo "Dropbox service is still initializing."
echo
echo "Check:"
echo " dropbox --status"
echo " dropbox --logs"
fi
exit 0
fi
sleep 1
done
echo
echo "Dropbox login did not complete."
echo
echo "Last Dropbox output:"
tail -n 80 "$TMP_LOG" || true
kill "$PID" 2>/dev/null || true
wait "$PID" 2>/dev/null || true
rm -f "$TMP_LOG"
exit 1
}
case "${1:-"--help"}" in
--help|-h|help)
help
;;
--login|login)
login_flow
;;
--start|start)
systemctl --user start dropbox.service
echo "Dropbox service started."
;;
--stop|stop)
systemctl --user stop dropbox.service
echo "Dropbox service stopped."
;;
--enable|enable)
systemctl --user enable --now dropbox.service
echo "Dropbox autostart enabled."
echo "Dropbox service started."
;;
--disable|disable)
systemctl --user disable --now dropbox.service
echo "Dropbox autostart disabled."
echo "Dropbox service stopped."
;;
--restart|restart)
systemctl --user restart dropbox.service
echo "Dropbox service restarted."
;;
--status|status)
cli status
;;
--logs|logs)
journalctl --user -fu dropbox.service
;;
--open|open)
xdg-open "$HOME/Dropbox"
;;
--exclude-list|exclude-list)
cli exclude list
;;
--exclude-add|exclude-add)
shift
cli exclude add "$@"
;;
--exclude-remove|exclude-remove)
shift
cli exclude remove "$@"
;;
--service-status|service-status)
systemctl --user status dropbox.service --no-pager
;;
--uninstall|uninstall)
systemctl --user stop dropbox.service 2>/dev/null || true
systemctl --user disable dropbox.service 2>/dev/null || true
rm -f "$HOME/.config/systemd/user/dropbox.service"
systemctl --user daemon-reload 2>/dev/null || true
rm -f "$HOME/.local/bin/dropbox"
rm -rf "$HOME/.local/lib/dropbox"
echo "Removed wrapper, CLI and systemd service."
echo
echo "Left untouched:"
echo " ~/.dropbox"
echo " ~/.dropbox-dist"
echo " ~/Dropbox"
;;
*)
echo "Unknown command: $1"
echo
help
exit 1
;;
esac
EOF
chmod +x "$WRAPPER"
}
ensure_path() {
mkdir -p "$HOME/.local/bin"
if [[ ":$PATH:" != *":$HOME/.local/bin:"* ]]; then
grep -qF 'export PATH="$HOME/.local/bin:$PATH"' "$HOME/.bashrc" 2>/dev/null || \
echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$HOME/.bashrc"
export PATH="$HOME/.local/bin:$PATH"
fi
if ! command -v dropbox >/dev/null 2>&1; then
sudo ln -sf "$WRAPPER" /usr/local/bin/dropbox
fi
}
main() {
install_deps
install_daemon
install_cli
install_service
install_wrapper
ensure_path
echo
echo "Dropbox installed successfully."
echo
echo "Now run:"
echo " dropbox --login"
echo
echo "Then:"
echo " dropbox --status"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment