From: Hugo Villeneuve Date: Tue, 16 Sep 2025 20:29:57 +0000 (-0400) Subject: Add rsync script X-Git-Url: http://gitweb.hugovil.com/?a=commitdiff_plain;h=5c8af296845e72de005e4c58443f4f69b83b74e1;p=hvutilities.git Add rsync script Signed-off-by: Hugo Villeneuve --- diff --git a/scripts/.hv-rsync.conf b/scripts/.hv-rsync.conf new file mode 100644 index 0000000..fd18ced --- /dev/null +++ b/scripts/.hv-rsync.conf @@ -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/ diff --git a/scripts/Makefile.am b/scripts/Makefile.am index af8f969..d8a9797 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -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 index 0000000..99d56f4 --- /dev/null +++ b/scripts/hv-rsync.sh @@ -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