Add rsync script
authorHugo Villeneuve <hugo@hugovil.com>
Tue, 16 Sep 2025 20:29:57 +0000 (16:29 -0400)
committerHugo Villeneuve <hugo@hugovil.com>
Mon, 6 Oct 2025 01:01:10 +0000 (21:01 -0400)
Signed-off-by: Hugo Villeneuve <hugo@hugovil.com>
scripts/.hv-rsync.conf [new file with mode: 0644]
scripts/Makefile.am
scripts/hv-rsync.sh [new file with mode: 0755]

diff --git a/scripts/.hv-rsync.conf b/scripts/.hv-rsync.conf
new file mode 100644 (file)
index 0000000..fd18ced
--- /dev/null
@@ -0,0 +1,10 @@
+#!/bin/bash
+
+RSYNC_USER=rsync-user
+RSYNC_SERVER=${RSYNC_USER}.rsync.net
+
+RSYNC_LIST="data musique videos"
+
+RSYNC_REMOTE_DIR=nas
+
+RSYNC_LOCAL_DIR=/home/my-user/
index af8f969..d8a9797 100644 (file)
@@ -49,8 +49,13 @@ dist_bin_SCRIPTS = \
     hv-scan \
     hvpdf-rotate \
     hv-resize \
+    hv-rsync.sh \
     tape-backup tape-backup-mult tape-list tape-restore
 
+# Install scripts in $(sysconfdir) and distribute them.
+skeldir = $(sysconfdir)/skel
+skel_DATA = \
+    .hv-rsync.conf
 
 # we want these in the dist tarball
 # (for scripts that we don't want to install but want to distribute)
diff --git a/scripts/hv-rsync.sh b/scripts/hv-rsync.sh
new file mode 100755 (executable)
index 0000000..99d56f4
--- /dev/null
@@ -0,0 +1,104 @@
+#!/bin/bash
+
+PROG_NAME=$(basename $0)
+
+HVRSYNC_CONF=${HOME}/.hv-rsync.conf
+
+print_usage()
+{
+    echo "${PROG_NAME} -- synchronisation serveur RSYNC"
+    echo "Usage: ${PROG_NAME} [OPTIONS...] MODE"
+    echo
+    echo "MODE:"
+    echo "  push"
+    echo "  pull"
+    echo "  diff"
+    echo
+    echo "Options:"
+    echo "  -h   display this help and exit"
+    echo
+}
+
+while getopts "h" flag ;do
+    case ${flag} in
+        h)
+            print_usage
+            exit 0
+            ;;
+        ?)
+            echo "${PROG_NAME}: Invalid option: ${OPTARG}."
+            echo "Try \`${PROG_NAME} -h' for more information."
+            exit 1
+            ;;
+    esac
+done
+shift `expr "${OPTIND}" - 1`
+
+# `$#' now represents the number of arguments after the options.
+# `$1' is the first argument, etc.
+if [ ${#} -ne 1 ]; then
+    echo "${PROG_NAME}: mode manquant."
+    echo "Essayez \`${PROG_NAME} -h' pour plus d'informations."
+    exit 1
+fi
+
+mode="${1}"
+
+case "${mode}" in
+    push|pull|diff)
+        # Valid
+        ;;
+    *)
+        echo "Mode invalide"
+        print_usage
+        exit 1
+        ;;
+esac
+
+if [ ! -f ${HVRSYNC_CONF} ]; then
+    echo "Fichier de configuration manquant: ${HVRSYNC_CONF}"
+    exit 1
+fi
+
+source ${HVRSYNC_CONF}
+
+# rsync options:
+# -a: sync recursively and preserves symbolic links, special and device
+#     files, modification times, groups, owners, and permissions.
+# -n: dry-run
+
+for f in ${RSYNC_LIST}; do
+    if [ x"${mode}" = x"diff" ]; then
+
+        if [ ! -d ${RSYNC_LOCAL_DIR}/${f} ]; then
+            echo "RĂ©pertoire local absent: ${RSYNC_LOCAL_DIR}/${f}"
+            exit 1
+        fi
+
+        echo "Diff (push) for folder ${RSYNC_REMOTE_DIR}/${f}:"
+
+        rsync -avun --delete \
+              "${RSYNC_LOCAL_DIR}/${f}/" \
+              ${RSYNC_USER}@${RSYNC_SERVER}:"${RSYNC_REMOTE_DIR}/${f}" | \
+            grep "^deleting "
+
+        echo "Diff (pull) for folder ${RSYNC_REMOTE_DIR}/${f}:"
+
+        rsync -avun --delete \
+              ${RSYNC_USER}@${RSYNC_SERVER}:"${RSYNC_REMOTE_DIR}/${f}/" \
+              "${RSYNC_LOCAL_DIR}/${f}" | \
+            grep "^deleting "
+    elif [ x"${mode}" = x"pull" ]; then
+        echo "RSYNC for folder ${RSYNC_REMOTE_DIR}/${f}:"
+        mkdir -p ${RSYNC_LOCAL_DIR}/${f}
+        rsync -avu --delete --progress \
+              ${RSYNC_USER}@${RSYNC_SERVER}:${RSYNC_REMOTE_DIR}/${f}/ \
+              ${RSYNC_LOCAL_DIR}/${f}
+    elif [ x"${mode}" = x"push" ]; then
+        echo "RSYNC for folder ${RSYNC_LOCAL_DIR}/${f}:"
+        ssh ${RSYNC_USER}@${RSYNC_SERVER} "mkdir -p ${RSYNC_REMOTE_DIR}/${f}"
+        rsync -avu --delete --progress \
+              ${RSYNC_LOCAL_DIR}/${f}/ \
+              ${RSYNC_USER}@${RSYNC_SERVER}:${RSYNC_REMOTE_DIR}/${f}
+    fi
+done