feat(sync): refactor Nextcloud sync script with retries and argument handling
feat(sync): refactor Nextcloud sync script with retries and argument handling - Add centralized sync_directory() function - Add retry logic with configurable attempts and delay - Improve argument parsing and help output - Add validation for selected sync targets - Add support for sync marker removal (-del) - Add logging abstraction with silent/live output modes - Add dependency and credential file checks - Prevent concurrent nextcloudcmd executions - Improve error handling and script structure - Add version flag support - Standardize synchronization workflow for all folders asd
This commit is contained in:
1 parent
e5629b73ae
commit
3168e3928b
2 files changed
+278
-163
No files matched your search
Executable
+278
@@ -0,0 +1,278 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -o pipefail
|
||||
set -u
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Nextcloud synchronization script
|
||||
#
|
||||
# Usage:
|
||||
# sync.sh [OPTIONS]
|
||||
#
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
SERVER="https://cloud.someoneslinux.selfhost.eu"
|
||||
SERVER="https://localhost"
|
||||
NETRC="/home/lucas/.netrc"
|
||||
SYNC_LOG="${HOME}/sync.log"
|
||||
SYNC_MARKER="/tmp/nextcloud_sync"
|
||||
|
||||
MAX_RETRIES=3
|
||||
RETRY_DELAY=5
|
||||
|
||||
SILENT=true
|
||||
SYNC_MEMES=1
|
||||
SYNC_AUDIOBOOKS=1
|
||||
SYNC_RECIPIES=1
|
||||
SYNC_ALINA=1
|
||||
SYNC_ALINA_DROP=1
|
||||
SYNC_WALLPAPER=1
|
||||
SYNC_CAMERA=1
|
||||
|
||||
usage() {
|
||||
cat <<EOF
|
||||
Usage: ${0##*/} [OPTIONS]
|
||||
|
||||
Synchronize configured directories with Nextcloud.
|
||||
|
||||
Options:
|
||||
-h, --help Show this help message and exit
|
||||
-l Show synchronization output
|
||||
-d Enable Alina Share synchronization
|
||||
-c Enable Camera synchronization
|
||||
-w Enable Wallpaper synchronization
|
||||
-m Enable Memes synchronization
|
||||
-ab Enable Audiobook synchronization
|
||||
-al Enable Alina synchronization
|
||||
-all Enable all synchronization operations
|
||||
-del Remove the Nextcloud sync marker
|
||||
--version Show script version
|
||||
|
||||
Examples:
|
||||
${0##*/} Run the default synchronization
|
||||
${0##*/} -l Run with live output
|
||||
${0##*/} -c -ab Sync everything except camera and audiobooks
|
||||
${0##*/} -h Show this help
|
||||
EOF
|
||||
}
|
||||
|
||||
die() {
|
||||
echo "Error: $*" >&2
|
||||
echo
|
||||
usage >&2
|
||||
exit 2
|
||||
}
|
||||
|
||||
run_cmd() {
|
||||
if [[ "$SILENT" == "true" ]]; then
|
||||
"$@" >"$SYNC_LOG" 2>&1
|
||||
else
|
||||
"$@" 2>&1 | tee "$SYNC_LOG"
|
||||
fi
|
||||
}
|
||||
|
||||
sync_directory() {
|
||||
local name="$1"
|
||||
local remote_path="$2"
|
||||
local local_path="$3"
|
||||
|
||||
echo "Sync ${name}"
|
||||
|
||||
run_cmd nextcloudcmd \
|
||||
--trust \
|
||||
-n \
|
||||
--path "$remote_path" \
|
||||
"$local_path" \
|
||||
"$SERVER"
|
||||
}
|
||||
|
||||
parse_args() {
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
usage
|
||||
exit 0
|
||||
;;
|
||||
|
||||
--version)
|
||||
echo "${0##*/} 1.0"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
-l)
|
||||
SILENT=false
|
||||
;;
|
||||
|
||||
-w)
|
||||
SYNC_WALLPAPER=0
|
||||
;;
|
||||
|
||||
-d)
|
||||
SYNC_ALINA_DROP=0
|
||||
;;
|
||||
|
||||
-c)
|
||||
SYNC_CAMERA=0
|
||||
;;
|
||||
|
||||
-ab)
|
||||
SYNC_AUDIOBOOKS=0
|
||||
;;
|
||||
|
||||
-m)
|
||||
SYNC_MEMES=0
|
||||
;;
|
||||
|
||||
-al)
|
||||
SYNC_ALINA=0
|
||||
;;
|
||||
|
||||
-all)
|
||||
SYNC_AUDIOBOOKS=0
|
||||
SYNC_RECIPIES=0
|
||||
SYNC_ALINA=0
|
||||
SYNC_MEMES=0
|
||||
SYNC_ALINA_DROP=0
|
||||
SYNC_WALLPAPER=0
|
||||
SYNC_CAMERA=0
|
||||
;;
|
||||
|
||||
-del)
|
||||
if [[ -f "$SYNC_MARKER" ]]; then
|
||||
rm -f "$SYNC_MARKER"
|
||||
echo "Sync marker removed: $SYNC_MARKER"
|
||||
else
|
||||
echo "Sync marker does not exist: $SYNC_MARKER"
|
||||
fi
|
||||
;;
|
||||
|
||||
-*)
|
||||
die "Unknown option: $1"
|
||||
;;
|
||||
|
||||
*)
|
||||
die "Unexpected argument: $1"
|
||||
;;
|
||||
esac
|
||||
|
||||
shift
|
||||
done
|
||||
}
|
||||
|
||||
validate_sync_options() {
|
||||
if (( SYNC_AUDIOBOOKS == 1 &&
|
||||
SYNC_RECIPIES == 1 &&
|
||||
SYNC_ALINA == 1 &&
|
||||
SYNC_MEMES == 1 &&
|
||||
SYNC_ALINA_DROP == 1 &&
|
||||
SYNC_WALLPAPER == 1 &&
|
||||
SYNC_CAMERA == 1 )); then
|
||||
|
||||
die "No synchronization target selected."
|
||||
fi
|
||||
}
|
||||
|
||||
sync_all() {
|
||||
local ret=0
|
||||
local attempt=1
|
||||
|
||||
while (( attempt <= MAX_RETRIES )); do
|
||||
ret=0
|
||||
|
||||
if (( SYNC_AUDIOBOOKS == 0 )); then
|
||||
sync_directory \
|
||||
"Audiobooks" \
|
||||
"Audiobooks/" \
|
||||
"/data/nextcloud/syncuser/Audiobooks"
|
||||
(( ret += $? ))
|
||||
fi
|
||||
|
||||
if (( SYNC_ALINA_DROP == 0 )); then
|
||||
sync_directory \
|
||||
"Alina Share" \
|
||||
"Alina Share/" \
|
||||
"/data/nextcloud/syncuser/alina"
|
||||
(( ret += $? ))
|
||||
fi
|
||||
|
||||
if (( SYNC_MEMES == 0 )); then
|
||||
sync_directory \
|
||||
"Memes" \
|
||||
"Memes/" \
|
||||
"/data/nextcloud/syncuser/Memes"
|
||||
(( ret += $? ))
|
||||
fi
|
||||
|
||||
if (( SYNC_ALINA == 0 )); then
|
||||
sync_directory \
|
||||
"Alina" \
|
||||
"Wachstum" \
|
||||
"/data/nextcloud/syncuser/Wachstum"
|
||||
(( ret += $? ))
|
||||
fi
|
||||
|
||||
if (( SYNC_RECIPIES == 0 )); then
|
||||
sync_directory \
|
||||
"Recipes" \
|
||||
"RecepiesLucas" \
|
||||
"/data/nextcloud/syncuser/RecepiesLucas"
|
||||
(( ret += $? ))
|
||||
fi
|
||||
|
||||
if (( SYNC_CAMERA == 0 )); then
|
||||
sync_directory \
|
||||
"Camera" \
|
||||
"Camera" \
|
||||
"/data/nextcloud/syncuser/camera"
|
||||
(( ret += $? ))
|
||||
fi
|
||||
|
||||
if (( SYNC_WALLPAPER == 0 )); then
|
||||
sync_directory \
|
||||
"Wallpaper" \
|
||||
"Wallpaper" \
|
||||
"/data/nextcloud/syncuser/WallpaperChanger"
|
||||
(( ret += $? ))
|
||||
fi
|
||||
|
||||
if (( ret == 0 )); then
|
||||
echo "Success"
|
||||
return 0
|
||||
fi
|
||||
|
||||
if (( attempt < MAX_RETRIES )); then
|
||||
echo "Sync failed (attempt ${attempt}/${MAX_RETRIES}). Retrying in ${RETRY_DELAY}s..."
|
||||
sleep "$RETRY_DELAY"
|
||||
fi
|
||||
|
||||
((attempt++))
|
||||
done
|
||||
|
||||
echo "Sync failed after ${MAX_RETRIES} attempts."
|
||||
return 1
|
||||
}
|
||||
|
||||
main() {
|
||||
parse_args "$@"
|
||||
validate_sync_options
|
||||
|
||||
if ! command -v nextcloudcmd >/dev/null 2>&1; then
|
||||
die "nextcloudcmd is not installed or not in PATH"
|
||||
fi
|
||||
|
||||
if [[ ! -r "$NETRC" ]]; then
|
||||
die "Cannot read credentials file: $NETRC"
|
||||
fi
|
||||
|
||||
if pgrep -x "nextcloudcmd" >/dev/null; then
|
||||
echo "Nextcloud sync is already active, stopping."
|
||||
return 0
|
||||
fi
|
||||
|
||||
echo "Starting Nextcloud synchronization..."
|
||||
|
||||
sync_all
|
||||
}
|
||||
|
||||
main "$@"
|
||||
exit $?
|
||||
Reference in new issue
Block a user