#!/bin/bash # FOR hosts with limited disk space - move to storage server function usage { echo -n "Usage: ${0}" echo -n " [ -A (default /works/archive)]" echo -n " [ -T ]" echo -n " [ -D (default: '2 days ago')]" echo exit 1 } echo Starting $0 $* # ---- D e f a u l t s ArchiveDir=/works/archive DateCriteria="2 days ago" FromHost=$(hostname -s) TargetRoot=hl-storage.cvtt.vpn:/works/archive # ---- D e f a u l t s # ---------------- cmdline while getopts "A:T:D:h" opt; do case ${opt} in A ) ArchiveDir=$OPTARG ;; T ) TargetRoot=$OPTARG ;; D ) DateCriteria=$OPTARG ;; h ) usage ;; \? ) echo "Invalid option: -$OPTARG" >&2 usage ;; : ) echo "Option -$OPTARG requires an argument." >&2 usage ;; esac done # ---------------- cmdline Oldest=$(date -d "${DateCriteria}" '+%Y-%m-%d %H:%M:%S') echo "Looking for log files older than ${DateCriteria} in ${ArchiveDir}" # 1. First, check if any matching files exist using a quick find check if ! find "${ArchiveDir}/" -type f -not -newermt "${Oldest}" -print -quit | grep -q .; then echo "No files found older than ${Oldest} in ${ArchiveDir}" echo "Done ${0} ${*}" exit 0 fi Target="${TargetRoot}/${FromHost}/" echo "Moving files to ${Target}:" echo "-----------------" # 2. Safely pipe find into rsync using relative paths from the ArchiveDir base # This completely avoids "Argument list too long" errors and handles spaces perfectly set -x find "${ArchiveDir}/" -type f -not -newermt "${Oldest}" -printf "%P\0" | \ rsync -ahvv \ -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" \ --remove-source-files \ --mkpath \ --from0 \ --files-from=- \ "${ArchiveDir}/" \ "${Target}" { set +x; } 2>/dev/null echo Done ${0} ${*}