294 lines
6.6 KiB
Bash
Executable File
294 lines
6.6 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
set -o pipefail
|
|
set -u
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# Nextcloud synchronization script
|
|
#
|
|
# Usage:
|
|
# sync.sh [OPTIONS]
|
|
#
|
|
# -----------------------------------------------------------------------------
|
|
|
|
SERVER="https://cloud.someoneslinux.selfhost.eu"
|
|
#[[ "$(hostname)" == "mediahost" ]] && 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_PICTURES=1
|
|
SYNC_SYNCFOLDER=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 Pictures synchronization
|
|
-w Enable Wallpaper synchronization
|
|
-m Enable Memes synchronization
|
|
-s Enable Syncfolder 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 \
|
|
--logdebug \
|
|
--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
|
|
;;
|
|
|
|
-s)
|
|
SYNC_SYNCFOLDER=0
|
|
;;
|
|
|
|
-c)
|
|
SYNC_PICTURES=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_PICTURES=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_SYNCFOLDER == 1 &&
|
|
SYNC_ALINA_DROP == 1 &&
|
|
SYNC_WALLPAPER == 1 &&
|
|
SYNC_PICTURES == 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_SYNCFOLDER == 0 )); then
|
|
sync_directory \
|
|
"Syncfolder" \
|
|
"Syncfolder/" \
|
|
"/data/nextcloud/syncuser/Syncfolder"
|
|
(( 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_PICTURES == 0 )); then
|
|
sync_directory \
|
|
"Pictures" \
|
|
"Pictures" \
|
|
"/data/nextcloud/syncuser/pictures"
|
|
(( 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 $?
|