Automatically storing database backups in Amazon s3


I needed a script to backup a mysql database daily and send it to Amazon s3 automatically. Here is a tutorial of how to do ti.

Backing up a database can be done in many ways. Since the project I was working on is a Drupal site there are modules for this. However I wanted to keep the backup handling out of Drupal so that backups would be ran even if problems would occur on the site.
I started with reading my friend Tobias notes on how to backup databases with cron. Before you start you need to have an Amazon s3 account and both the Access and Secret Key.

Set up the cron job

Edit an existing or create a new cron file and put the following in it. In my case /etc/cron.d/mysql-backup.
59 7 * * * root /root/
This tells cron to run the script /root/ as root user at 7:59 every day.

Database credentials

Keep the credentials safe in a separate file. Create /var/mysql.cfn and set permissions $ chmod 400 /root/ Put the following in the file.


Set up s3 command line tools

Install s3 command line tools $ sudo aptitude install s3cmd. Run the configuration as root user $ sudo -u root s3cmd --configure. You will be asked to put in your keys and there are also a couple of other options which you can skip (I decided to send the backups over https). When the configuration is set up you can create a new bucket $ s3cmd mb mybucket-for-databases check in the s3 UI that the bucket got created.

Bash script

Create the file /root/ Edit the file.



FILE=$DBNAME-`date "+%Y%m%d-%H%M"`.sql.gz
mysqldump --defaults-extra-file=/var/mysql.cfn $DBNAME | gzip -9 > /var/backups/$FILE
/usr/bin/s3cmd --config /root/.s3cfg put /var/backups/$FILE s3://$S3BUCKET/ >> /var/log/mysqlback.log
sleep 5
rm /var/backups/$FILE

The script creates the filename, dumps the database using our credentials in mysql.cfn and moves the database to s3 with the put command. The last line removes the file from our server.
That's it, you should now have a new backup named mydatabase-YYYYMMDD-HHMM.sql.gz daily in your bucket. Under Properties > Lifecycle in the s3 UI for the bucket you can set up rules for how long the backups should be kept in s3.
If it doesn't work start with checking the syslog $ tail -f /var/log/syslog when the cron job is executed.

Today we launched Smartburk on Symfony2


Since september I've been working on project for tv series with a friend of mine. Today we're launching the site called Smartburk which is built with Symfony2 framework.

I built Smartburk with my friend Tobias Sjösten. Smartburk lets its users keep track on the tv series they are following and also get recommendations for new series. One thing that makes it unique is that it is the first site of its kind in in Swedish. At the moment Smartburk only lives in the browser but mobile apps are in development.

We built Smartburk with Symfony2 PHP framework. This was my first large Symfony2 project and I've learnt a lot along the way. Knowledge I will also make use of as a Drupal developer since Drupal 8 will use a lot of Symfony components. Symfony and Drupal have a lot in common since they are both written in PHP and Open sourced. But they are also very different from each other. When you build with Drupal you get a lot to start with and then you configure and tweak it to suit your needs. With Symfony you start more from scratch and build exactly what you want. Which one is better for you depends on the project you are working on.

So if you like tv series (and speak Swedish) you should definitely check out Smartburk!

Push notifications for Android and iOS with Drupal and Titanium


I recently developed a Drupal site that serves as a backend for a mobile application (that I also built parts of). At an early stage of the project it came clear that the client wanted push notifications implemented.

Drupal was chosen by the client since they already knew the system and was going to create content for the application from the web. They wanted an application that could run on both iOS and Android why we used Titanium Appcelerator. The core of the application was around certain dates and the push was to go out on the day before these dates. Also not all devices should be notified for all dates. The users needed to be able to pick dates themselves.

Titanium has it's own service for push notifications which has integration built in. Two other popular services are Urban Airship and Parse. These two have modules available in the Market place. However the services where all to expensive and since I needed to write logic for when the pushes where to go out on the Drupal side I decided to set this up by myself without third party services.


I started researching for how to get the push notification functionality up on Drupal and soon found out that of course there is a module for that. The module integrates with the Services module to let devices sign up for pushes and then it integrates with both APNS (Apple Push Notification Service) and GCM (Google Cloud Messaging) to send out pushes to devices. For Android all that is needed is to get an API key for GCM. However for iOS there is bit more work. You will need to enable Push notifications for the application you are building by logging into Apple developer and generate certificates etc. All instructions are in the readme for the module. I had some troubled that got solved with some help from the maintainer.

Test a push

After you have this set up on the Drupal side you can test to send pushes from the admin interface of the module at admin/config/services/push_notifications/message. To test you need to add some devices in the database.


The Push notifications module comes with a Services endpoint for devices to be added to recieve pushes. I created my own endpoint in a custom module since I needed more data to be stored and also more functionality around when to push. In my final solution I did not use a lot of the functionality from the contrib module but had my own endpoint, database table and implemented hook_cron to run push_notifications_apns_send_message().


On the client side you first want the application to register for push notifications.


On iOS the user only gets the popup to enable push notifications for an application once which is good to keep in mind. It does´t matter if you do a full reinstall of the application. When the application starts up you need to implement the method Titanium.Network.registerForPushNotifications() and make a call to your Drupal endpoint.


For Android I found this module on GitHub. I installed the module but ran in to some problems with the settings in tiapp.xml. I made it work with the following settings.

    <property name="com.activate.gcm.sender_id" type="string">[Sender ID from GCM service]</property>
    <property name="com.activate.gcm.icon" type="int">2130837504</property>
    <property name="com.activate.gcm.component" type="string"></property>
        <module platform="android" version="0.5">com.activate.gcm</module>

1. Make sure you set the correct version number. 2. You will need to have a Google account on the device you use for testing.

The module comes with installation instructions and code examples that made it easy to use.


Not much code in this tutorial since most of the process is reading documentation and configuration. On the Titanium side you need to check the device OS with Ti.Platform.osname. After that register notifications separately for Android and iOS. The next step is to send your token to Drupal with Titanium.Network.HTTPClient.

I used

Titanium 2.1.3
Drupal 7
Push notifications drupal module 7.x-1.0
GCM Titanium module v0.5

Freelancer again


August was my last month at Kollegorna, the web agence I co-founded three years ago. First of all I'd like to say thank you to Per, Tobias, Joakim and Andreas for the awesome time we've had. You guys rock!

Running a web agency has been a great experience with lots of wonderful memories. We created a company to be proud of and built some great web sites.

There are lots of reasons that I'm leaving and one of the bigger is that I just felt done and ready for something new.

Another reason is to have more time for side projects. Something we had as a goal when starting the agency, but naturally clients always came first. Besides freelancing I will work together with my friend Tobias Sjösten and finally realizing some of our web projects we just haven't had time for. If you understand Swedish you can follow our adventures at our blog Interversum (soon to go live).

I wish Per och Joakim the best of luck together with their new colleague Henrik.

Also I now hope to have more time to keep this blog better updated :)

My first Android application built with Phonegap


My first Android application is now up on Android Market. It's a very simple application that let's you keep track of Facebook likes and Twitter tweets for your sites.

I've been interested in building something with Phonegap for quite awhile now. The last week I finally took the time and it ended up in my first Android application.

Phonegap let's you use html/css and javascript for building applications for touch devices. For me that's really great since I don't know Java or Objective-c.

I also used jQuery mobile to simplify the work and I used one of the built in themes for the design.

The application let's you add sites that uses Facebook like or Twitter buttons and gives you the count of how many people have used the buttons for the specified URL. I think that the whole process from getting the developing environment up to have the application up on Android Market took me 10 to 15 hours. It was great fun and now I'm looking forward to built something more advanced next.

Subscribe to Freelance web developer from Stockholm