First, I should note that I am pretty new to writing scripts, but I know my way around ubuntu well enough that I don't mind getting my hands dirty...
I have a small script, which does a simple directory watch and when it finds a new file in the directory, copies that file to another directory. The script I am using looks like this:
#!/bin/sh # sudo rsync -av --omit-dir-times --delete --progress /home/sunmorgus/tempx/Complete/ /media/complete/ while [ 1 ] do echo 'Watching directory: '$WATCHED_DIR 'for new files' while file=$(inotifywait -q -e create "$WATCHED_DIR" --format "%f") do echo 'New file to upload to moonback' $file notify-send -i "gtk-go-up" "Complete Folder Monitor" "Uploading file $file" #rsync goes here sudo rsync -av --omit-dir-times --delete --progress /home/sunmorgus/tempx/Complete/ /media/complete/ notify-send -i "gtk-home" "Complete Folder Monitor" "File uploaded." done done
It's a modification of a script I found here: http://www.webupd8.org/2010/11/script-to-watch-folder-and-upload-new.html
I have a few questions about the script. First off...is this the best way to do what I am trying to do? Like I said, it works fine, but I'm always open to new ideas. Note: the destination directory is an SMB share on a Windows 7 machine.
Second...what would be the best way to run this script at startup? Right now, I went into the "Startup Applications" screen and added a new record that runs this line "gnome-terminal -e /script.sh" which works, but doesn't seem ideal. I tried adding the script to the init.d folder and running update-rc.d, and adding the script to /etc/rc.local but neither of those worked (I got no errors, but the script wasn't running after a reboot).
Lastly...is there a way to modify this script so that it also deletes from the destination when deleted from the source? The script only runs when files are added, not deleted, and while the rsync portion will delete from the destination when deleted from the source, it won't do it until a new file is added. (hopefully that paragraph makes sense).