1

I have this file structure:

...folders/to/copy
...folders/to/mirror

I've been doing:

rsync --ignore-existing -raz --progress $source $destination

This is serving well for the purpose of keeping everything up to date on both folder trees. But now I have the need to do this only for files that have been changed more than 24 hours ago. I have tried something like this:

rsync -rav `find $source -mtime 1` $destination

The problem is this messes up the file tree and doesn't work as I need. Is there a better way to do this?

I want to backup the file structure, using the relative paths say from "folders" and everything in it, from one place to another leaving only files older than 24 hours (mtime).

Acidorr
  • 13
  • find $source -mtime 1 lists files and directories and links and sockets and ... which were "modified" between 24 hours and 48 hours ago. You probably don't want to list directories, and you may not want to have the 48 hour restriction as well. – icarus Mar 07 '22 at 16:27
  • That's my problem, I don't want to list the files just want to "signal" rsync that only files that have been modified more than 24 hours ago are clear to be backed up. – Acidorr Mar 07 '22 at 16:30
  • the -a is doing recursion. So if your find adds any directories, the rsync will run all the items underneath. You'd at least want to turn that off. – BowlOfRed Mar 08 '22 at 00:29
  • Thanks @BowlOfRed, that was the point I was trying to make when I said "You probably don't want to list directories" but you expressed it better. – icarus Mar 08 '22 at 20:03
  • Ah, I see what you mean! Thanks! – Acidorr Mar 09 '22 at 15:22

1 Answers1

2

You can use find | rsync to filter the files you're interested in copying, like this

src=/source/folders/to/./copy
dst=/destination/mirrored/folders

find "$src/" -type f -mtime +0 -print0 | rsync --dry-run -a -iv --files-from - --from0 / "$dst"

Remove the --dry-run when you're happy with the set of results. Remove -iv if you want a quieter operation.

I should point out that the /./ part of the source path defines the point from which the file paths should be retained. For example, if you have a path /source/folders/to/./copy/sub/here.txt then the corresponding destination will become /destination/mirrored/folders/copy/sub/here.txt. See the --files-from and --relative (-R) options in the man rsync documentation for the details. If your source path is relative that will work too.

This solution presupposes GNU find or some other variant that offers -print0. If you don't have that option, you could also remove --from0, but better would be to replace -print0 with -exec printf '%s\0' {} +. See the discussion at POSIX alternative to GNU find's -print0.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287