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.
5 Answers
rsync --progress --files-from=<(find /src_path -mtime -3 -type f -exec basename {} \;) /src_path/ /dst_path

- 731
- 15
- 20
-
Well placing in cron fails with error "rsync: failed to open files-from file <(find /......;): No such file or directory rsync error: syntax or usage error (code 1) at main.c(1084) – user1595858 Jul 01 '16 at 14:14
-
@user1595858 did you just pasted the command as is? :-V. There's a typo in the /src_path-mtime, missing space. – m3nda Apr 21 '18 at 00:25
-
-
Here is what worked for me: # 1) Go to the directory you want to sync FROM # 2) Define the number of days with (-mtime +day)[newer than] or (-mtime -day)[ older than] rsync -av --progress --files-from=<(find . -type f -mtime -1 ) ./ /my_backup/ – gryzli Aug 12 '20 at 11:00
-
2@user1595858, cron might run the commands with `/bin/sh`, which is only guaranteed to be a POSIX shell. The `<(command)` syntax is a bash extension – Simon Richter Apr 22 '21 at 11:04
-
will this work if you have anything more complicated than updated or added files? As in, files moved to a different folder (but their dates didn't change) or files were deleted? – Michael Jul 18 '22 at 22:31
-
-
How to apply the above for *photos*, with the date the photo was taken (via EXIF / meta data) ? E.g., utilizing `identify -verbose IMG_20200430_113445.jpg | grep -i date` and `exif:DateTime`. – nutty about natty Feb 19 '23 at 20:33
You would want to do a find
then sync
find /path -file -mtime +3 -exec rsync {} destination \;

- 6,017
- 2
- 30
- 34
-
1
-
3Note that this runs `rsync` for every file. That can get really slow compared to executing rsync only once with `--files-from` and if the destination is remote server that requires password authentication, inputting all those passwords is getting to be tedious. – Mikko Rantalainen Jun 14 '22 at 14:16
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.

- 141
- 1
- 5
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"

- 121
- 3
-
Uh... you are using ssh to get to the server, but are touching a file locally that the server is supposed to access? – Michael Dec 08 '19 at 04:50
-
1In the last step, that file is moved to the server. This way, the start date and time for the next sync is set to right before the sync starts. And it is only set if the sync finished correctly. – Thomas Dec 08 '19 at 07:02
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

- 8,811
- 21
- 32
- 47