Posts Tagged ‘php’


Backups or The Dreaded `rm -rf /*`

Thursday, July 29th, 2010

At my startup, we have a moderately sane deployment environment. Check-ins are automatically pushed to a staging server (And i’m notified via notifo with this script). When we want to push code to our live server, we manually move things.

For a while that meant we had to run a

svn export

command by hand once a day or so. (we are under heavy development still)

Eventually we needed a couple more things done when we moved code to the live server (among them run google’s closure compiler to reduce our js footprint). So I wrote a script to handle these things. Apparently while testing, I got distracted and never finished it, because when i ran it, the code was this:

# ... snipped ...
#
# code to assure you're running as root
#

if [ -z $2 ]
    then
        loc= '/path/to/code'
    else
        $loc = $2
fi

# nuke old files
rm -rf $loc/*

# ... snipped ... 

Now, to the astute, you will notice there is a bug here. Can you find it? It’s in the assignment of

$loc = $2

It SHOULD be similar to the loc assignment above it.

loc=$2

Well. This throws an error, but happily keeps moving on down the script. Next question: what happens here when $loc is an empty string ”? That’s right. I just ran rm -rf /*as sudo on our main server.

I realized quickly what had happened. Errors whizz by about permissions, and finally I Ctrl+c’d it out of existence. Everything still in memory was intact. Our application was still running, I was still ssh’d in. So I took stock of my life, and tried to calm down. I had no idea what to do at this point; thoughts ranged from spending the next 12 hours slowly rebuilding everything (I had the latest copy of our code and db on my local computer, so our code was safe) to never returning my CEO’s emails again and abandoning our company. I initially did some fruitless googling for ‘restore rm -rf *’ even though I knew – short of serious hard drive-level manipulation – I was NOT getting anything back.

Then I remembered that we are using Jungle Disk to do daily and weekly backup. I figured out that Media Temple (our awesome host) allows us to reinstall our OS if something like… this happens. So first I checked that we had a recent backup. We did. So i went into our dasboard, and clicked on the button to ‘Revert to Default’ (MT’s oh-shit button). And waited. After that was done, the first thing I reinstalled was Jungle Disk and set it up to recognize itself. Then I simply ran Jungle Disk’s restore program, and voilà! I installed a few more programs that we needed, and we were 100% back up and running in about 3 hours.

We back up our /etc, /var, and /home folders daily and our entire file system weekly, so not too much data was lost. As it turns out, this doesn’t include our Postfix maildir, so we lost about a week’s worth of email (personally, I use gmail and it downloads all the email, so I didn’t lose anything). We have since fixed this to save email daily.

This story is another among many to remind you to back your shit up. Fortunately we ended up ok; thanks in no small part to Jungle Disk.

If you have any questions about our staging=>live process (which is now Idiot Proof©), or our backup strategy, drop me a line at hello at andrebluehs dot net.


You and Urls and Browser Plugins

Thursday, March 4th, 2010

Someone recently offered to pay me to write them a url shortening backend/script to use for themselves. Not only did I decide to not charge them for it, but I decided to write a chrome extension so you can use it with as little hassle as possible.

For the tl;dr; crowd: download the url shortening script and the chrome extension to use with your shiny new shortener. Instructions are included.

The following instructions are included in the README.txt of the tar file. But here they are for convenience, and for any questions: leave them in the comments. NOTE: these instructions are if you want a default installation setup. This assumes you want all the default table, user name, and password. It’s probably a good idea to change them, but that’s up to you.

Step 0: Things You Need

You’re going to need a url. Probably a short one? There are tons of crazy tld’s available. i chose ablu.us cause it’s moderately close, yours might work even better.

Apache with mod_rewrite. Or equivalent server/rewrite setup.

PHP.

Step 1: The Database

You’re going to need to create the database for your service. NOTE: the user@server$ and mysql> are the prompts you should  see (clearly user@server$ will be different for you).

user@server$ msyql -u root -p

mysql> CREATE DATABASE `uploads`;

mysql> GRANT ALL PRIVILEGES ON `uploads`.* TO 'uploads_user'@localhost IDENTIFIED BY 'uploads_pass';

mysql> FLUSH PRIVILEGES;

msyql> quit;

This will create the database we need to install things in the next step.

Step 2: Installation

If you table name, username, or password you need to edit config.php accordingly. There are also a number of other things you can customize in config.php like the alphabet used to chose characters from, the length of the shortened url.

One thing you MUST change the $site_url in config.php. Otherwise it will insert things into your database, but not give you the correct link back.

Visit /install of the folder from above from your browser (http://yourdoma.in/install). If you kept the defaults from the above step, you don’t need to change any of these fields.

Click “Install >>”. And barring any database errors, you should have a working url shortening service!

NOTE: You may want to consider removing the /install folder after this step. It’s not necessary as running it again won’t destroy any data, but you never know.

Chrome Extension

The chrome extension is pretty easy to use. The first time you try to use it, it will tell you to Set Shortener. You have two choices here: If you want to use your own service you just created, enter the following:

yourdomain/up.php?site=

If you want to use MY service, enter:

ablu.us/s/up.php?site=

I’m considering doing a Firefox addon as well, but creating one of those is a bit more involved.

This extension will work with any url shortening api that returns just the plain text of the short url. Examples include http://fizl.ushttp://is.gd.

That’s It!

You have your own url shortening service!

I am running a modified version of this on my own site: http://ablu.us/s/. The main ablu.us is an image uploader. Feel free to use it if you so chose, but I just created it for my own personal use for some coding practice.

Leave any questions in the comments below, and i’ll try to get to them.


Ablu.us and Chrome Extensions

Friday, February 26th, 2010

I recently created a file hosting and url shortening service for myself: ablu.us. Now as a pet project, i’m starting to get some feature creep action going on. Case in point: I just wrote a chrome extension to use this service.

NOTE: I am never going to release this to the gallery, as I don’t want THAT much attention to this thing, it’s just sweet for me to have for myself. In all actuality, I might do something else with this url eventually, so releasing this out into the world would probably be a bad idea.

That said, here is a link to download it if you actually want it:

INSTALL ablu.crx.

CHANGELOG :

version 0.2 now includes using fizl.us.

version 0.2.1 fixed an initial settings bug

It’ll ask you if you really trust me, and to continue. And that’s it.

I won’t get into the nitty gritty details of HOW TO create your own chrome extension quite yet, but expect a blog post about it soon. Also, Google has a bunch of getting started tutorials that helped me so well it only took me about 2 hours to write this extension from scratch.

Some interesting things are as follows:

These babies are written in javascript

This was news to me. I knew Firefox plugins are written in XUL, which is similar to javascript. Chrome extensions are written in plain, vanilla javascript. Not only that, the part that gets displayed is literally an html page that you can do whatever you want with. If you want to include jQuery, you can (mine does not). Any other library? yup. It also means that you can use any of the HTML 5 capabilities Chrome offers: local storage, canvas, image rotation.

This strikes me as a bit excessive as you can load an unlimited number of scripts from anywhere. Seems to me that this could be abused.

Chrome allows copying to clipboard

That’s right. You can copy things to clipboard just like in IE with

document.execCommand('Copy')

This is exactly how similar URL Shortening extensions work. After seeing how awesome this is, I have to wonder… why doesn’t Firefox support this? I don’t see it being a security risk more than copying profanity into the clipboard. Whatever, it’s nifty that chrome has it.

Autoupdating is scary

Hoo dangle is it scary. What happens if the dev’s life suddenly tanks and decides he wants to have your browser randomly redirect to a porn site at random intervals? If you have a previously installed extension of his and he decides to update this new functionality, he can (there are some caveats to this, like what permissions the extension already has). This could be problematic as it would be difficult to track down exactly what’s causing this browser behavior. This has the potential to turn any previously useful and non-porn-redirecting extension into a very messy thing to be a part of.

If you are in the market for (another) url shortening extension, give it a try. Let me know what you think. It could probably use a much better logo, so if  you want to help drop me a line at hello () andrebluehs [] net.