From 1883f1b95a6d08c1fe096a1b25f4b132eed70fe6 Mon Sep 17 00:00:00 2001 From: Steph Enders Date: Fri, 12 Dec 2025 17:13:01 -0500 Subject: Enable rsync backend for ssync An improvement could be adding "remote_root_dir" option to ssync-fetch so we can use it in cases of rsync --- ssync | 16 ++++++++++++++-- ssync-fetch | 60 ++++++++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 58 insertions(+), 18 deletions(-) diff --git a/ssync b/ssync index 83eed3f..b61528c 100755 --- a/ssync +++ b/ssync @@ -3,6 +3,7 @@ set -e USAGE="ssync [options] CONFIG_FILE OPTIONS + -b BACKEND -n no locking -v verbose logging -h print this message" @@ -20,7 +21,7 @@ function lines { # OPTIONS NO_LOCK_FLAG= VERBOSE_FLAG= -while getopts "nvh" opt; do +while getopts "nvhb:" opt; do case "${opt}" in h) echo "$USAGE" exit 1 @@ -29,6 +30,9 @@ while getopts "nvh" opt; do ;; v) VERBOSE_FLAG=1 ;; + b) BACKEND_FLAG=1 + BACKEND="$OPTARG" + ;; esac done @@ -145,6 +149,14 @@ if [ ! -z "$VERBOSE_FLAG" ]; then verbose_log "Using verbose logging for ssync subprocesses" fi +if [ ! -z "$BACKEND_FLAG" ]; then + backend_opt="-b $BACKEND" +elif [ ! -z "$backend" ]; then + backend_opt="-b $backend" +fi + + + RUN=$(date -Is | sed 's/[:]/-/g') output_dir=$output_files_dir/$RUN verbose_log "Creating run output directory: $output_dir" @@ -184,7 +196,7 @@ $ssync_dir/ssync-queue $verbose_opt \ # Fetching verbose_log "Fetching files from queue" -$ssync_dir/ssync-fetch $verbose_opt $key_file_opt \ +$ssync_dir/ssync-fetch $verbose_opt $key_file_opt $backend_opt \ -r $remote_host \ $queue_file \ $fetch_output_dir diff --git a/ssync-fetch b/ssync-fetch index 24b9c59..0fab1b9 100755 --- a/ssync-fetch +++ b/ssync-fetch @@ -2,14 +2,16 @@ USAGE="ssync-fetch [options] QUEUE_FILE DEST_DIR OPTIONS - -r REMOTE_HOST - remote host to download from such as user@hostname - username can be omitted if identical to $USER - or if set in ssh_config - -k KEY_FILE - ssh-key file to use (needs to be non-interactive) - optional: will use default session key - or key set in ssh_config for REMOTE_HOST + -r REMOTE_HOST + remote host to download from such as user@hostname + username can be omitted if identical to $USER + or if set in ssh_config + -k KEY_FILE + ssh-key file to use (needs to be non-interactive) + optional: will use default session key + or key set in ssh_config for REMOTE_HOST + -b BACKEND + sftp (default) | rsync -v verbose logging -h print this message" @@ -17,7 +19,7 @@ USAGE="ssync-fetch [options] QUEUE_FILE DEST_DIR function verbose_log { if [ ! -z "$VERBOSE_FLAG" ]; then - echo "$@" + echo "$@" fi } function lines { @@ -30,9 +32,11 @@ KEY_FILE_FLAG= KEY_FILE_ARG= REMOTE_HOST_FLAG= REMOTE_HOST_ARG= +BACKEND_FLAG= +BACKEND_ARG= VERBOSE_FLAG= -while getopts "hvr:k:" opt; do +while getopts "hvr:k:b:" opt; do case "${opt}" in h) echo "$USAGE" exit 1 @@ -45,6 +49,9 @@ while getopts "hvr:k:" opt; do r) REMOTE_HOST_FLAG=1 REMOTE_HOST_ARG="${OPTARG}" ;; + b) BACKEND_FLAG=1 + BACKEND_ARG="${OPTARG}" + ;; esac done @@ -81,6 +88,19 @@ elif [ -z "$REMOTE_HOST_ARG" ]; then exit 1 fi +if [ -z "$BACKEND_FLAG" ]; then + BACKEND="sftp" +else + BACKEND=$BACKEND_ARG +fi + +if [ "$BACKEND" != "sftp" ]; then + if [ "$BACKEND" != "rsync" ]; then + echo "Invalid backend $BACKEND" + exit 1 + fi +fi + # CONFIGURATIONS ssh_id_param="" @@ -98,13 +118,21 @@ ts=$(date +%s) verbose_log "Writing temp files to ${tmp_dir} with timestamp ${ts}" # GENERATE BATCH -batch_file=$tmp_dir/batch_${ts} -verbose_log "Converting the queue file to sftp batch file: ${batch_file}" -cat $QUEUE_FILE | xargs -I{} echo "@reget {} ${real_dest}/" >> $batch_file -verbose_log "Beginning download" -verbose_log "sftp -N ${ssh_id_param} -b ${batch_file} ${REMOTE_HOST_ARG}" +if [ "$BACKEND" == "sftp" ]; then + batch_file=$tmp_dir/batch_${ts} + verbose_log "Converting the queue file to sftp batch file: ${batch_file}" + cat $QUEUE_FILE | xargs -I{} echo -e "@reget {} ${real_dest}/" > $batch_file + + verbose_log "Beginning download" + verbose_log "sftp -N ${ssh_id_param} -b ${batch_file} ${REMOTE_HOST_ARG}" -sftp -N ${ssh_id_param} -b ${batch_file} ${REMOTE_HOST_ARG} + sftp -N ${ssh_id_param} -b ${batch_file} ${REMOTE_HOST_ARG} +elif [ "$BACKEND" == "rsync" ]; then + verbose_log "Fetching files from queue file: $QUEUE_FILE" + verbose_log "Beginning download" + verbose_log "rsync --files-from=${QUEUE_FILE} rsync://${REMOTE_HOST_ARG} ${real_dest}" + rsync -av --no-relative --files-from=${QUEUE_FILE} ${REMOTE_HOST_ARG}:/ ${real_dest} +fi verbose_log "ssync-fetch finished" -- cgit v1.2.3-54-g00ecf