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.
-
It is not the same "from a specific date forward" than "created from 3 days ago". Could match today, but tomorrow... – dstonek Dec 5 '16 at 18:28
rsync --progress --files-from=<(find /src_path -mtime -3 -type f -exec basename {} \;) /src_path/ /dst_path
-
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 1 '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 0: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
You would want to do a find
then sync
find /path -file -mtime +3 -exec rsync {} destination \;
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"
-
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 8 '19 at 4: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 8 '19 at 7:02
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.