26

I would like to rsync folders from a specific date and forward. for example. I want to rsync my folders that were created from 3 days ago (and of course 2 days ago, one day ago etc.). I know I need to use find and rsync but I'm not sure how. any idea? Thanks! Dotan.

edotan
  • 1,998

5 Answers5

31
rsync --progress --files-from=<(find /src_path -mtime -3 -type f -exec basename {} \;) /src_path/ /dst_path
fredrik
  • 781
10

Modifying the answer from Thomas which syncs based on the modifiction date of a file to a script, to be more human readeable and sync nested folders as well.

#!/bin/bash

TARGET=/PATH/TO/TARGET HOST=username@host SOURCE=/ABSOLUTE/SOURCE/PATH/ON/HOST

touch $TARGET/last_sync

rsync
-ahrv
--update
--files-from=<(ssh $HOST "find $SOURCE -type f -newer $SOURCE/last_sync -exec realpath --relative-to=$SOURCE '{}' ;")
$HOST:$SOURCE
$TARGET

rsync -ahv $TARGET/last_sync $HOST:$SOURCE

For init one should probably create a last_sync file remotely, to which the following command comes in handy

touch -d "2 hours ago" last_sync

which creates a file called last_sync with a creation date of 2 hours ago.

capitalg
  • 171
9

You would want to do a find then sync

find /path -file -mtime +3 -exec rsync {} destination \;
2

Assuming you want to sync some folder from a server to a local folder but you always want to only sync the files that have been created since the last sync. Then the following command might be useful. Putting this in, e.g., your .bashrc defines an alias that syncs all the newly created files. The files can be locally deleted and will not be synced again when calling the sync command again. Only files that have been created after the last sync on the server will be copied to the local folder.

TARGET=/local/target/folder/
SOURCE=/server/folder/
alias sync-since-last="touch $TARGET/last_sync && rsync -ahv --update --files-from=<(ssh user@SERVER.IP 'find $SOURCE/source/ -type f -newer $SOURCE/last_sync -exec basename {} \;') user@SERVER.IP:$SOURCE/source/ $TARGET && rsync -ahv $TARGET/last_sync user@SERVER.IP:$SOURCE"
Thomas
  • 121
-2

For particular date you can use below command from current directory:

for file in $(ls -ltr *.gz | grep 'Apr 17' | awk {'print $9'} | xargs ls)
  do rsync -avzh --progress -e ssh $file  user@*******:/tmp
done