They say you learn something new every day.

Continuing my plan to backup my life, I turned to all the images for this blog. Yesterday I figured out how to back up the content to another WordPress blog, using If This Then That. The next stage is to backup all the images.

I store all of the images on my website so that they’re all in one place. In one way, this is a bit silly, because it means I bear all the bandwidth costs. But, hey, apparently I’ve got 7TB of monthly bandwidth! Considering this site is just a log for me, I think I’ll be okay.

The upside of this is that because all of the pictures are stored in one place, they’re very easy to backup.

But, remembering back to my requirements yesterday:

  1. It must be automatic.
  2. It must be free.
  3. The process must not affect my life or computer use. 

Now that I have If This Then That set up, this shouldn’t be too hard. I want the system to automatically copy a picture to Dropbox each time I add one to my site.

There are three things I’ll need to do to achieve this:

  1. Write a script to recursively list all the files in my site
  2. Turn this into an RSS Feed
  3. Set up an IFTT script to import the contents on this RSS Feed into DropBox

My web server runs PHP so I’ll need to get my PHP thinking cap on.

PHP was one of the first server side scripting languages I learnt. But I haven’t touched it for a while now, and going back to it, it feels really weird.

Why, for example is, the command to replace elements of a string str_replace() rather than, say, replace()? It’s like they’re trying to make this harder!

It’s strange because PHP is considered an easy hackers language, but playing around with it now, after using vb, it feels a bit clumsy.

Anyway, once I’d reminded myself of the syntax, I put together the script.

The first bit was the command to recursively list a directory. Now, there is a PHP class for recursively listing directories, but because of my server setup I couldn’t use that.

So I had to write my own (with a bit of help from the Internet).

function getDirectory($path = ‘.’,$URL)
{

// Directories to ignore when listing output
$ignore = array(‘.’, ‘..’);

// Open the directory
$dh = @opendir($path);

// Loop through the directory
while(false !== ($file=readdir($dh)))
{

// Check that this file is not to be ignored
if( !in_array( $file, $ignore ) )
{

   // If it’s a directory iterate over that
   if( is_dir( “$path/$file” ) )
   { getDirectory(“$path/$file”,$URL);}

   // If it’s a file print out it’s name.
   elseif (strpos($file,”.php”) === FALSE)
   { echo $URL.$filename; }

}

}

// Close the directory handle
closedir( $dh );

}

 One of the key bits is this:

$ignore = array(‘.’, ‘..’);

if( !in_array( $file, $ignore ) )

I’ve looked into listing directories before, but had problems with the “.” and “..”. This neat checks solves that.

The other thing I had to do was produce the full URL so DropBox can download files:

$URL =’http://’.$_SERVER[“SERVER_NAME”].$_SERVER[“REQUEST_URI”];
$URL = substr($URL,0,strrpos($URL, “/”));

This gets the address of the file running, and then trims off the filename. It was the easiest way I could find of doing it.

The next stage was to create an RSS Feed:

//Send headers to Browser

header(“Content-Type: application/rss+xml”);

//Current Date in Correct Format

$now = date(“r”);

// Output beginning to RSS Feed
echo “

Backup File feed
http://www.imagedissectors.com/downloads/tumblr/index.php
Backup Feed
$now
$now

  ITEM TITLE
  URL TO ITEM
  GUIDE
  DATE PUBLISHED
”;

Once this was written, configuring ITTT was easy.

IFTTT Appearence

IFTTT seems to cache the feed and then compare it to it’s cached version to work out what’s a “new feed item”. This confused me at first, but now it means I don’t have to mess around with dates.

It also seems to have a cap of 29 items in one transaction. Again, this threw me at first, but all it means is that I needed to do an initial backup of everything, and then IFTTT does incremental backups from now on.

Part 2 of my backup strategy is complete.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Tag Cloud

%d bloggers like this: