I'm using rsync to maintain two copies of a music library on my main workstation and a server running MPD. I'm wondering about using the script below as a basis for cron to update my MPD db if new files have been added or existing files modified:
find -type f -iname 'FILENAME' -mmin -10 -exec 'script' \;
Would it be ill advised to simply put ~/music/*.mp3 for FILENAME? with ~160GB of music in that dir?
Would I perhaps be able to use -type d to monitor the directory itself for changes? In which case, what denotes a change to the directory (ie, adding a file?)
I do not quite understand - if you want to have 2 repositories of files keep synced, why not just have rsync be triggered in an interval. Depending on the command switches you hand to it, it will keep it synced for you. It does not really need the help of find to do this.
find will run always over this 160 GB (which can still be a lot of files), which can take some time. rsync maintains a file list it builds just once and tracks only the differences which should be much faster.
Right, but I'm trying to automate the maintenance of my MPD database because it must be manually updated and I have to SSH into that machine each time. So even though the files are identical, the MPD db is not current until I do "mpd --update-db". The cron job would look something like this:
Maybe I'm not being clear, I have no problem using rsync to maintain the two directories, that's easy. I'm trying to run 'mpd --update-db' only if changes have been made to the directory located on the server.
Instead of having every file inspected by a find and maybe some other additional commands, you could just issue your rsync and redirect it's output to a temporary file. Usually if it had nothing to do, it is just 4 lines of output like:
sending incremental file list
sent 95 bytes received 12 bytes 214.00 bytes/sec
total size is 0 speedup is 0.00
So if it has more than 4 lines, you can issue your update. Can test it with
if [ $(wc -l < /tmp/rsync.out) -gt 4 ]; then
mpd --update-db
fi
I ended up creating a simple script that checked for the existence of /tmp/rsync.log on remotehost, updating the MPD db if it was present then removing the file which would be then be recreated the next time rsync was run. This way the update isn't run redundantly simply because the log is four lines in length.
#!/bin/sh
if [ -e /tmp/rsync.log ]
then
etc/init.d/mpd stop
mpd --create-db
rm /tmp/rsync.log
fi
I realize now that using find with the -exec action was way off and appreciate all the input that led me to this solution.