23

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.

migrated from stackoverflow.com Jun 15 '11 at 1:53

This question came from our site for professional and enthusiast programmers.

  • b) how often do you run rsync? – phresnel Jun 14 '11 at 13:53
28

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

  • 22
    To set that as a remote filter: rsync -avn --files-from=<(ssh user@A 'find /path/on/A/ -mtime -7 -type f -exec basename {} \;') user@A:/path/on/A/ user@B:targetdir – cybertoast Aug 22 '14 at 16:04
  • I want to add a correction for cybertoast's comment. Perhaps the context on his different from mine but I was trying to simple extract files from a remote server given the time criteria. So, server A is my destination and server B is my source, then: rsync -avn --files-from=<(ssh user@A 'find /path/on/A/ -mtime -7 -type f -exec basename {} \;') user@B:/ /path/in/server/A Notice there's only a leading / for source. The --files-from takes care of the relative path for you. If you have doubts use man rsync and see the --files-from section. – einarc Oct 27 '16 at 17:09
  • @cybertoast What does basename mean in your command? Can you explain please? – Kemat Rochi Jun 29 '17 at 19:21
  • @KematRochi - "basename, dirname -- return filename or directory portion of pathname". Hope that helps. – cybertoast Jun 29 '17 at 23:40
5

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:. .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.