Backup and Restore with MySQL

Wednesday, February 24, 2010 10:32 PM

There's a lot of ways to backup a MySQL database. I've had to look this up from time to time. And I have not found anyone take these scenarios and boil them down into a short concise guide. Enjoy.

Backup

The simple way of backing up a MySQL database.

$ mysqldump -u [username] -p [password] [database_name] > [backup_filename.sql]

What if you want to restore an existing database? You can add 'drop table' statements to your backup like so...

$ mysqldump --add-drop-table -u [username] -p [password] [database_name] > [backup_filename.sql]

What if you just wanted a table and not the whole database? Yes, you can do that too...

$ mysqldump --add-drop-table -u [username] -p [password] [database_name] [table1 table2 ...] > [backup_filename.sql]

More than one database to backup?

$ mysqldump -u [username] -p [password] --databases [database1 database2 ...] > [backup_filename.sql]

Need to backup all databases in a MySQL instance?

$ mysqldump --all-databases > [backup_filename.sql]

How about the database, but no data please. Coming right up!

$ mysqldump --no-data --all-databases > [backup_filename.sql]

Automating Backups

Need to automate your MySQL backup? There's a script for that...

#!/bin/sh
date=`date -I`
mysqldump --all-databases | gzip > /some/path/backup-db-name-$date.sql.gz 

Restore

The way to restore a MySQL database.

$ mysql -u [username] -p [password] [database_name_to_restore] < [backup_filename.sql]

Manipulating Geo Coordinates in Python and Foursquare Cheating

Sunday, February 21, 2010 11:21 PM

I've been tinkering with the Foursquare API. It's a lot of fun to play with and is as clean as the Twitter API. Anyway, the reason for all of this madness is this guy. He's funny and decided "wouldn't it be cool if.." he became mayor of the North Pole. I thought a lot about this and decided that eventually Foursquare will attempt to battle this "fake check-in" issue. I think another way to cheat the system is by using a venue's supplied geo coordinates, and a bounding box I could "fake" a geo coordinate, and use it to check-in.

My "Strategery"

Pick a Fourquare venue, query the Foursquare API for the venue data. Use the venue's associated "geolat" and "geolong" as a starting geo coordinate. Then all you need is a bearing (in degrees), and a distance (in kilometers). This problem should take you all the way back to high school trigonometry (it did for me). Take those 4 values and plug them into the "move_lat_long" function (below), and voila your geo coordinate has been moved!

import math

def move_lat_long(latitude, longitude, distance, bearing):
        bearing = convert_to_radians(bearing)
        latitude = convert_to_radians(latitude)
        longitude = convert_to_radians(longitude)
        # earths radius in kilometers
        earth_radius = 6371
        d = float(distance)/float(earth_radius)
        latitude2 = math.asin( math.sin(latitude) * math.cos(d) + math.cos(latitude) * math.sin(d) * math.cos(bearing) )
        longitude2 = longitude + math.atan2( math.sin(bearing) * math.sin(d) * math.cos(latitude), math.cos(d) - math.sin(latitude) * math.sin(latitude2) )
        return convert_to_degrees(latitude2), convert_to_degrees(longitude2)

def convert_to_degrees(number):
        return (float(number) * (180/math.pi))

def convert_to_radians(number):
        return (float(number) * (math.pi/180))

For extra credit (I'm lame it's okay) I pulled apart the geo coordinates that are usually expressed in degrees ... into degrees, minutes, seconds.

def expand_geo_coordinate(coordinate):
        geo_coordinate = float(coordinate)
        geo_degrees = int(geo_coordinate)
        geo_minutes = (abs(geo_coordinate) % abs(geo_degrees)) * 60
        geo_seconds = (geo_minutes % int(geo_minutes)) * 60
        random_format = "[%s]  %d degress   %d minutes   %d seconds"
        return random_format % (coordinate, geo_degrees, geo_minutes, geo_seconds)

The only part that's left of "my diabolical plan" is picking a random distance, and a random bearing. There will be another post on the full project once I've successfully become mayor of a targeted venue.

VMware Fusion 3 headless mode

Tuesday, February 16, 2010 01:38 AM

I don't know if a lot of people know about this but you can run virtual machines (some people call them vm's for short) in VMware Fusion 2 or 3 in headless mode. I've just upgraded to VMware Fusion 3. My script (below) has been tested on OSX Leopard running VMware Fusion 3. I find this perfect for running a Linux based server image on OSX.

filename: headless_vm.sh

#!/bin/bash
USERNAME=your_username_here
IMAGE_NAME=your_vm_image_name_here

/Library/Application\ Support/VMware\ Fusion/vmrun -T fusion start /Users/$USERNAME/Documents/Virtual\ Machines.localized/$IMAGE_NAME.vmwarevm/$IMAGE_NAME.vmx nogui

RSS Feed Proxy in C# .NET

Monday, January 25, 2010 04:18 PM

A quick and dirty example of how to make a feed proxy in C#/.NET for that cross site request browser security issue on the client side.

You would call it via a url like this ...

http://mydomain.com/Proxy.ashx?u=http://feeds.heatxsink.com/heatxsink/atom

filename: Proxy.ashx

<%@ WebHandler Language="C#" CodeBehind="Proxy.ashx.cs" Class="FeedProxy.Proxy" %>

filename: Proxy.ashx.cs

using System;
using System.Web;
using System.Net;
using System.IO;

namespace FeedProxy
{
    public class Proxy : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string url = context.Request.Params["u"];
            if(!string.IsNullOrEmpty(url))
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "GET";
                request.KeepAlive = true;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string responseBody = string.Empty;
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    responseBody = reader.ReadToEnd();
                }

                context.Response.ContentType = response.ContentType;
                context.Response.Write(responseBody);
            }
            else
            {
                context.Response.ContentType = "text/html";
                string message = string.Format("You must supply a 'url' query parameter.");
                context.Response.Write(message);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

Conan O'Brien Farewell Speech

Sunday, January 24, 2010 09:20 PM

Please do not be cynical. I hate cynicism. For the record, it's my least favorite quality. It doesn't lead anywhere. Nobody in life gets exactly what they thought they were going to get. But if you work really hard, and you're kind, amazing things will happen. I'm telling you, amazing things will happen. Conan O'Brien