From 3168e3928b3a078fc26c521e62877d29c003740e Mon Sep 17 00:00:00 2001 From: Lucas Faber Date: Fri, 25 Sep 2026 12:11:09 +0200 Subject: [PATCH] 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 --- nextcloud/syncNC.sh | 278 +++++++++++++++++++++++++++++++++++++++++++ serverhost/syncNC.sh | 163 ------------------------- 2 files changed, 278 insertions(+), 163 deletions(-) create mode 100755 nextcloud/syncNC.sh delete mode 100755 serverhost/syncNC.sh diff --git a/nextcloud/syncNC.sh b/nextcloud/syncNC.sh new file mode 100755 index 0000000..f099914 --- /dev/null +++ b/nextcloud/syncNC.sh @@ -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 <&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 $? diff --git a/serverhost/syncNC.sh b/serverhost/syncNC.sh deleted file mode 100755 index 9b60453..0000000 --- a/serverhost/syncNC.sh +++ /dev/null @@ -1,163 +0,0 @@ -#!/bin/bash -run_cmd() { - #echo $@ - if [[ "$SILENT" == "true" ]]; then - "$@" > ~/sync.log 2>&1 - else - "$@" 2>&1 | tee ~/sync.log - fi -} -ret=0 -count=20 -PW=$(cat /home/lucas/.PW) -PW_M="multimedia123" -flatpack1="" -flatpack2="" -debug="" -SYNC_MEDIA=1 -SYNC_AUDIOBOOKS=1 -SYNC_RECIPIES=1 -SYNC_ALINA=1 -SYNC_ALINA_DROP=1 -SYNC_THEMES=1 -SYNC_WALLPAPER=1 -SYNC_CAMERA=1 -SYNC_SERVER=0 -SILENT=true -SERVER="https://someoneslinux.ddns.net" -while [[ ${1:0:1} == - ]]; do - echo "Param: $1" - [[ $1 == -l ]] && { - SILENT=false; - shift 1; - continue; - }; - [[ $1 == -f ]] && { - flatpack2="com.nextcloud.desktopclient.nextcloud " - flatpack1="flatpak run --command=" - shift 1; - continue; - }; - [[ $1 == -o ]] && { - SYNC_SERVER=1; - SYNC_WALLPAPER=1 - shift 1; - continue; - }; - [[ $1 == -d ]] && { - SYNC_ALINA_DROP=0; - shift 1; - continue; - }; - [[ $1 == -c ]] && { - SYNC_CAMERA=0; - shift 1; - continue; - }; - [[ $1 == -ab ]] && { - SYNC_AUDIOBOOKS=0; - shift 1; - continue; - }; - [[ $1 == -al ]] && { - SYNC_ALINA=0; - shift 1; - continue; - }; - [[ $1 == -m ]] && { - SYNC_MEDIA=0; - shift 1; - continue; - }; - [[ $1 == -t ]] && { - SYNC_THEMES=0; - shift 1; - continue; - }; - [[ $1 == -del ]] && { - if [ -f /tmp/nextcloud_sync ]; then - rm /tmp/nextcloud_sync - echo "File removed" - fi - shift 1; - continue; - }; - echo "Parameter $1 discarded" - shift 1; -done - -if pgrep -x "nextcloudcmd" > /dev/null; then - echo "Nextcloud Sync active, stop" -else - echo "Sync Nextcloud" - while true; do - ret=0 - if [ ${SYNC_MEDIA} -eq 0 ];then - echo "Sync Multmedia" - run_cmd sudo /home/lucas/scripts/serverhost/tvbitch_user_cmd.sh ${flatpack1}nextcloudcmd ${debug} --trust --user multimedia --password ${PW_M} --path Filme/New /torrent/multimedia/Filme/New ${flatpack2}${SERVER} -# echo run_cmd sudo /home/lucas/scripts/serverhost/tvbitch_user_cmd.sh ${flatpack1}nextcloudcmd ${debug} --trust --user multimedia --password ${PW_M} --path Filme/New /torrent/multimedia/Filme/New ${flatpack2}${SERVER} - ret=$(( ret + $? )) - #echo Return: $ret - fi - if [[ ${SYNC_THEMES} -eq 0 ]] && [[ ${SYNC_MEDIA_ONLY} -eq 0 ]]; then - echo "Sync Themes" - run_cmd ${flatpack1}nextcloudcmd --trust --user lucas --password ${PW} --path Dokumente/Dokumente/Projekte/Handy/ThemeProject/SolarSystemTheme /home/lucas/Documents/Dokumente/Projekte/Handy/ThemeProject/SolarSystemTheme ${flatpack2}${SERVER} - ret=$(( ret + $? )) - #echo Return: $ret - fi - if [[ ${SYNC_AUDIOBOOKS} -eq 0 ]];then - echo "Sync Audiobooks" - run_cmd ${flatpack1}nextcloudcmd --trust --user lucas --password ${PW} --path Media/Audiobooks/ /nextcloud/syncAudiobooks ${flatpack2}${SERVER} - ret=$(( ret + $? )) - #echo Return: $ret - fi - if [[ ${SYNC_ALINA_DROP} -eq 0 ]];then - echo "Sync Alina Share" - run_cmd ${flatpack1}nextcloudcmd --trust --user lucas --password ${PW} --path Media/Pictures/Alina\ Share/ /torrent/alina ${flatpack2}${SERVER} - ret=$(( ret + $? )) - #echo Return: $ret - fi - if [[ ${SYNC_ALINA} -eq 0 ]];then - echo "Sync Alina" - run_cmd ${flatpack1}nextcloudcmd --trust --user lucas --password ${PW} --path Dokumente/Dokumente/Shared/Alina/Wachstum /home/lucas/Documents/Dokumente/Shared/Alina/Wachstum ${flatpack2}${SERVER} - ret=$(( ret + $? )) - #echo Return: $ret - fi - if [[ ${SYNC_RECIPIES} -eq 0 ]];then - echo "Sync Recipies" - run_cmd ${flatpack1}nextcloudcmd --trust --user lucas --password ${PW} --path Dokumente/Dokumente/Shared/Diverses/Kochbuch/RecepiesLucas /home/lucas/Documents/Dokumente/Shared/Diverses/Kochbuch/RecepiesLucas ${flatpack2}${SERVER} - ret=$(( ret + $? )) - #echo Return: $ret - fi - if [[ ${SYNC_CAMERA} -eq 0 ]];then - echo "Sync Camera" - ${flatpack1}nextcloudcmd --trust --user lucas --password ${PW} --path Media/Pictures/Camera /torrent/camera ${flatpack2}${SERVER} 1>/dev/null 2>&1 - ret=$(( ret + $? )) - #echo Return: $ret - fi - if [[ ${SYNC_SERVER} -eq 0 ]];then - echo "Sync Server" - run_cmd ${flatpack1}nextcloudcmd --trust --user lucas --password ${PW} --path Dokumente/Dokumente/Projekte/Server /home/lucas/Documents/Dokumente/Projekte/Server ${flatpack2}${SERVER} - ret=$(( ret + $? )) - #echo Return: $ret - fi - if [[ ${SYNC_WALLPAPER} -eq 0 ]]; then - echo "Sync Wallpaper" - run_cmd ${flatpack1}nextcloudcmd --trust --user lucas --password ${PW} --path Media/Stuff/Wallpaper /torrent/WallpaperChanger ${flatpack2}${SERVER} - ret=$(( ret + $? )) - #echo Return: $ret - fi - if [[ "${ret}" == "0" ]]; then - echo "Sucess" - break - elif [[ ${count} -gt 15 ]]; then - echo "Massive Fail" - break - else - echo "Fail" - sleep 5 - fi - ((count++)) - done -fi -exit ${ret}