Automatically Sync Files on WIFI Connect

Mon 06 October 2014

Filed under automation

Tags linux lifheack

I recently discovered wicd (thanks to Gefoo). It is a really nifty network manager. And it supports running scripts on each connection stage for a network. This immediately gave me an idea:

I like taking photos, and sometimes I unload them from my camera to my laptop for immediate housekeeping. I keep them on an in-house NAS for safe-keeping. Being able to run scripts once the laptop connects, lets me sync the files automagically to my NAS. This is awesome, and this article summarises how I've implemented it.

It is actually quite straight-forward. The wicd documentation mentions that you should modify one the following settings (depending on what you need):

  • afterscript
  • postdisconnectscript
  • beforescript
  • predisconnectscript

However, this did not work for me. After editing the file, the values reverted to their original value (None). I assume that you need to stop the wicd service before editing it. I did not do that, and I assume that the file is written when wicd ends. This would explain this issue. But there is also another way, which I followed fo the rest of this.

As an alternative, you can just drop executable scripts into one of the /etc/wicd/scripts subfolders. For my task, I chose postconnect. I use this script as an "orchestration" script which launches other scripts as needed. For now it only contains one script, and looks like this:


if [ "$conntype" == "wireless" ]; then
    if [ "$essid" == "MichTik" ]; then

This says, that as soon as a wireless connection is established on the SSID "MichTik", the script /opt/wicd/scripts/filedrop should be executed.

That script in turn looks like this:


 # SSH Private Key for fully unattended operation. This key must be
 # passwordless, so the containing directory should have the proper access
 # rights.

 # Any file created by this script (the logfile) should be secured.
 umask 0077

 echo "$(date +"%F %T") dropping new files..." >> $LOGFILE

 # drop(src, dest)
 # Moved files from "src" to "dest".
 # This uses rsync with --remove-source-files, so you should make sure that
 # files are not modified *while* this is running! Read the according
 # section of the rsync man-page for details!
 function drop() {
     rsync -a \
         --partial \
         --remove-source-files \
         -e "ssh -l exhuma -i ${IDENTITY}" \
         ${src} \
         ${dst} >> ${LOGFILE}

 drop /home/exhuma/photos \

 echo "$(date +"%F %T") DONE." >> $LOGFILE

And that's it!


Security of this could be improved. For example, I am using my own user on the filedrop target. Which means that if my laptop is compromised, and someone would get their hands on this passwordless key, that person could wreak all kinds of havoc. After I finish this article, I will set up a dedicated user on the NAS for this purpose. And you should do so too :P


Michel Albert © Michel Albert Powered by Pelican and Twitter Bootstrap. Icons by Font Awesome and Font Awesome More