Skip to content

Instantly share code, notes, and snippets.

@anthonyclarka2
Created August 30, 2017 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonyclarka2/d4a8626714aa42c0980943c38514e25c to your computer and use it in GitHub Desktop.
Save anthonyclarka2/d4a8626714aa42c0980943c38514e25c to your computer and use it in GitHub Desktop.
from a remote server, rsync only files newer than a date
#!/usr/bin/bash
# https://unix.stackexchange.com/questions/301434/rsync-only-new-files-from-a-remote-server
# Modified by anthonyclarka2 AT GMAIL 2017-08-30.
# Comments, criticism welcome.
# exclude file has basic wildcards like *i386* or *debug*
# I have not tested this with anything other than rsync daemons, YMMV
SOURCE="rsync://server.example.edu/test"
# Remember the slash at the end of the destination
DEST="/home/username/repository/test/"
EXCLUDE="/etc/rsync/exclude.example.edu.txt"
# Look up the manpage for "date" to see what format to put into "-d"
CUTOFF=$( date -d '2016-01-01' +%s )
rsync -na --no-motd --exclude-from="${EXCLUDE}" --out-format='%M %f' "${SOURCE}" "${DEST}" | \
while IFS= read -r line; do
D=${line%% *}
FN=${line#* }
FDATE=$( date -d "${D/-/ }" +%s ) || continue
if [ "${CUTOFF}" -le "${FDATE}" ]; then printf '%s\0' "${FN}"; fi
done | \
# IMPORTANT!! the "n" in "-avn" below means "dry run". Remove it to really transfer files
rsync -avn --delete --exclude-from="${EXCLUDE}" --files-from=- -0 "${SOURCE}" "${DEST}"
# This is pretty cool. Rsync can output in a given format (line 12) which we run
# a while loop over. Some bash variable trickery gives us 2 epoch unix timestamps
# to compare (on line 17). That output is then piped into a new rsync process
# that does the actual transfer work.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment