Search code examples
linuxbashshellimagemagickrsync

rsync and (post) process synced file


i like rsync my photos from one (linux) disc partition to an other (backup location) using an shell script. The problem is, that I need to re-scale all photos which are saved on the backup location, for example with mogrify.

Is it possible to post-process every file, which is synced/copied by rsync? In oder to execute mogrify on every synced file?

An other way could using rsync (only) to generate the list of files which have to be synced. Next step: run a loop to mogrify every list entry in order to output the scaled photo to the backup location.

The problem is, that I have to add all the directories and child-directories to keep the original folder structure before saving the photo.

Using rsync would handle the folder creation "on the fly".

So: is it possible to execute an command on every file synced with rsync?


Solution

  • rsync has a -i/--itemize-changes flag to output a resume of what it does with each file.

    I suggest you to play a bit with it, I'm seeing it outputs lines like >f+++++++++ file1 for a new file, >f..T...... file1 for an unchanged file, >f.sT...... file1 for an update, etc...

    Having that, you can read this output into a variable, and parse this with grep and cut:

    #!/bin/bash
    log=$(rsync -i rsync-client/* rsync-server/)
    newFiles=$(echo "$log" | grep '>f+++++++++' | cut -d' ' -f2)
    for file in $newFiles
    do
        echo "Added file $file"
    done