rsync files newer than 1 week

I want to run rsync on server A to copy all files from Server B when they are newer than 7 days.

find . -mtime -7

I don't want to delete the files on Server B.


Solution 1:

This should get you underway in a solid way

rsync -RDa0P \
    --files-from=<(find sourcedir/./ -mtime -7 -print0) \
    . user@B:targetdir/

This copies device nodes, permissions, timestamps. I'm pretty sure the -H option won't be accurate with --files-from

Solution 2:

I wrote this script based on cybertoast's comment to sync from a remote server to local.

You can call the script with ./script.sh 3 or ./script.sh 3 dry for a dry run.

#!/bin/bash
TIME=$1
DRYRUN=$2

if [[ -z $TIME ]]; then
  echo "Error: no time argument."
  echo "Please enter the number of days to sync."
  exit 1
fi

if [[ $DRYRUN = "dry" ]]; then
  DRYRUNCMD="--dry-run"
  echo "Dry run initiated..."
fi

rsync -avz $DRYRUNCMD --files-from=<(ssh \
    user@remote "find path/to/data/ \
    -mtime -$TIME ! -name *.mkv -type f \
    -exec ls $(basename {}) \;") \
  user@remote:. .