rsync files newer than 1 week

25

6

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.

mm1

Posted 2011-06-14T12:49:12.563

Reputation: 353

b) how often do you run rsync? – None – 2011-06-14T13:53:52.487

Answers

31

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

sehe

Posted 2011-06-14T12:49:12.563

Reputation: 1 796

23To 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 – 2014-08-22T16:04:32.930

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 – 2016-10-27T17:09:18.950

@cybertoast What does basename mean in your command? Can you explain please? – Kemat Rochi – 2017-06-29T19:21:26.763

@KematRochi - "basename, dirname -- return filename or directory portion of pathname". Hope that helps. – cybertoast – 2017-06-29T23:40:18.663

6

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

Rohmer

Posted 2011-06-14T12:49:12.563

Reputation: 281