Automatically store files on a remote server

trollboy
  12 years ago
  9

Introduction

This tutorial is inspired by the Motion tutorial written by RayWoods. It's aim is to go a step further with the security aspect. The reasoning is that storing pictures of someone breaking in to your home on a local hard drive will only provide them with a nice souvenier of the theft. On a less cynical note, you could also use this to automatically publish webcam shots to a website ^_^

This tutorial will be using the command line and some basic scripting, as well as a tool called incron.

Target audience

Pretty much anybody.

Assumptions

That you have a web domain with some disk space and FTP access, an always on broadband or cable connection and finally, that you have a terminal open!

Create the FTP script

We are going to be using the command line FTP client as this is a scripted solution. Additionally, the script needs to automatically log on to the FTP server. The FTP command will look for credentials in a file called .netrc in your home directory.

nano ~/.netrc

Add the following to it and then save, substitute your details where appropriate.

machine www.example.com login trollboy password yourgoodpassword

Additionally, this file needs the permissions set to 0600, which means that no-one else can intefere with it and therefore it should be trustworthy.

chmod 0600 ~/.netrc

Once this file is created, we can test the connection by running the following commands to connect and create the remote storage directory. Obviously you should substitute the correct domain.

ftp www.example.com
mkdir securitypictures
quit

The next step is to script the FTP upload that will happen when a picture is saved to your chosen directory. So, create your script and mark it as executable

touch ~/ftpuploader
chmod +x ~/ftpuploader

Use nano to add the following in to the newly created file. Again substituting details where appropriate.

if [ -z "$1" ]
then
echo "No directory specified"
exit
fi

if [ -z "$2" ]
then
echo "No filename specified"
exit
fi

ftp www.example.com<<ENDFTP
cd securitypictures
put $1/$2 $2
quit
ENDFTP

You can now test your FTP script. Assuming that you have a jpg called test1.jpg in your Pictures directory, the command is as shown below. Once you have run the following, you should be able to see the picture on your remote server.

./ftpuploader ~/Pictures test1.jpg

Install and configure incron

Incron is a system daemon that works a lot like cron. Most people have heard of cron which waits for a specified time before running a command or script; incron waits for a specified file system event before running a command or script.

sudo apt-get install incron

incron is set up in a similar way to cron but it uses the incrontab instead of the crontab. To edit the incrontab you use the following command

incrontab -e

You may find at this stage that you are not allowed to use incron. In such a case, simply edit the file /etc/incron.allow as root and add your user name to it. One user name per line. Once you can edit the incrontab, add the following to it.

/home/YOUR_USER/webcam/ IN_CREATE /home/YOUR_USER/ftpuploader $@ $#

There is much information to be found for incron on the Internet, but what that line says is essentially, "Watch your webcam directory and when a file is created in there, run your ftpuploader script passing it the name of the watched directory and file name that has been created.

You can test this simply by copying an image into your web cam directory. It should automagically appear on the remote server.

Comments
hithirdwavedust 12 years ago

Interesting. I have no direct application for this...
yet.

Again, superbly written. Thank you again for you time and effort. You have made Linux a much friendlier place.


efthialex 12 years ago

Great tutorial! Thanks.


remoulder 12 years ago

Nice