Posts tagged ‘helpful’

Backup Mysql Database on GoDaddy

Assumptions: You are trying to back up MySQL Databases on a GoDaddy Shared Linux package where you have access to SSH and know what your credentials are. If you know either of these, but are unsure of how to get to it, check out this article first: Enabling SSH on your Godaddy Account.

Here are the steps we will use to back up a MySQL database on GoDaddy:

  1. SSH To your GoDaddy Account
  2. Make a directory to store your script
  3. Write the script
  4. Change the script to executable
  5. Execute the script
  6. Verify your backup was created

It’s easy to know that you should run backups on a normal basis, and as my web presence grows and I continue gaining more information (i.e. this example here), then it becomes more important to make sure I don’t lose this information. If you are here, you probably have or know the reasons for completing these steps.

SSH’ing to your GoDaddy Account:
I complete this step with my Unix Mac, but if you are on Windows, you may want to try Putty or some other interface for SSH’ing. For Mac’s, it’s as easy as:

ssh username@mydomain.com

If prompted to accept a certificate, type yes and hit enter, and then put in the password for your GoDaddy Shared Hosting SSH Login.

Make a directory to store your scripts:
This is debatable as to how you want to do this, I created a directory for my scripts and a directory for my backups.

mkdir -p site_backups/scripts
mkdir -p site_backups/databases

Note: Some of this upcoming tasks depend on the location of the database directory, so make sure you remember where that is.

Write the script:
This you can do however you want, I chose to use an ftp editor and save it in my local git repository and then I upload it to the server. Here are the contents of the script that I create. To complete this script, you will want to log into your GoDaddy hosting account and and go to Databases -> MySQL, and click the little pencil button. That should give you everything you need except your password. Here is what you need:

  • Database Name: DBNAME
  • Database Host: DBHOST
  • Database Password: DBPASSWORD

Below is the contents of the bash script. This script generates a database backup in the previously created directory, and down one more directory to the DBNAME, with a time stamp that drills down to the minute. I have somewhat modulated this script because I have several backups. It could even be more modular, and as you mature this script, you should keep that in mind. Additionally, telling me what you did as a comment here may help others, so feel free to coment.

site_backups/scripts/backup_database.sh

#!/bin/bash

function create_directory {
	if [ ! -d "$1" ]; then
	    mkdir -p "$1"
	fi
}

function execute_command {
	$1 | gzip > $2`date '+%m-%d-%Y_%H-%M'`.sql.gz
}

# hurlme_main
DBNAME="demodatabase"
DBHOST="$DBNAME.db.YOUR.hostedresource.com"
DBPASSWORD="your_password"
BACKUPLOCATION="$HOME/site_backup/databases/$DBNAME/"

COMMAND="mysqldump --add-drop-table -h $DBHOST -u $DBNAME -p$DBPASSWORD $DBNAME"

echo "Creating directory if it doesn't exist: $BACKUPLOCATION"
create_directory "$BACKUPLOCATION"
echo "Executing $DBNAME backup..."
execute_command "$COMMAND" "$BACKUPLOCATION"

Make this script above executable:
You can do this via FTP or SSH, pick your flavor and change the script permissions to 700. Via ssh, you can do it:
IMPORTANT: If you put your passwords and locations in this file, be weary to make sure you can’t access it via the public web. It is helpful to make sure this ISN’T in your HTML directory

ssh my_user@myresource
cd site_backup/scripts
chmod 700 backup_database.sh

Execute and verify your script ran properly:
Here, I am just chaining off of the above, so I am ssh’ed into my directory and executing the script directly:

./backup_database.sh
ll ../databases/DBNAME
-->  inetuser  2388 Mar 27 15:01 03-27-2011_15-01.sql.gz

The 2388 symbolizes the size of the database, If this is very small, like 20, then you probably haven’t got a good back up. For sakes sake, I would restore this to a local database or manually view the SQL to make sure it has done exactly what you wanted it to do.

Now, we must look at our next steps. Some people use CRONTAB to complete a schedule of scripts and you can do this easily right in your home directory. I, however, like to manage everything using Jenkins or any web based solution that makes this stuff easier to remember as I am very forgetful. We will look at this in an upcoming article. Until then, feel free to make suggestions and improve my solution and share it with all of us in the comments.

Show ActiveRecord SQL in Rails 3 Console

If you want to show sql in your console for Active Record, then you may have run across the past Rails Recipes book or similar formulas for Rails < v3.0.  I just tweaked a formula I saw in that book to work with Rails 3 and here it is (This is designed to work on a Mac or most Linux flavors, Windows users, YMMV):

Create ~/.irbrc or add the following to your .irbrc, Mine is located at ~/.irbrc

if defined?(Rails) && Rails.env
  load File.dirname(__FILE__) + "/.railsrc"
end

Create ~/.railsrc or add the following to your ~/.railsrc:

def toggle_sql
  if ActiveRecord::Base.logger
    set_logger nil and return false
  else
    set_logger Logger.new(STDOUT) and return true
  end
end

def set_logger(logger)
  ActiveRecord::Base.logger = logger
  ActiveRecord::Base.clear_active_connections!
end
set_logger nil # start with the normal logger

Then, when you’re in your rails console world, just call toggle_sql. It returns true if the sql will be on, and false if the sql will be off.

Please feel free to leave suggestions and improvements here as I am always open to these suggestions.

Add Widget and Ads Twenty Ten WordPress Theme

I sat down today to add a widget container and ads to my WordPress blog with the latest popular Twenty Ten theme. This would also likely apply to all other themes as well where there is no Top Widget Container. To view this in action, just take a gander above the name of my blog and know, that this is exactly how I did it.

To add a Widget Container, Browse to http://your-wordpress-blog.com/wp-admin -> Appearance -> Editor

On the right hand side of this page, you will see a link to all of the different pages you can edit directly.  Select the “functions page”, and continue to the next step.

In here, around line 355 (YMMV), you will see text like:

function twentyten_widgets_init() {

Now add this text behind this line (You may want to change the naming strategy, but be wary when doing this as you will have to change the respective names later in this series of articles).

  // Header Widget :)  Whee
  register_sidebar( array(
   'name' => __( 'Header Top Widget', 'twentyten' ),
   'id' => 'header-top-widget',
   'description' => __( 'This widget is displayed above all other content on every page', 'twentyten' ),
   'before_widget' => '<div id="header-top-widget">',
   'after_widget' => '</div>',
   'before_title' => '<h3 class="widget-title">',
   'after_title' => '</h3>',
  ) );

Now, browse to Admin -> Widgets and notice that you now have a new widget that you can configure and add whatever you want to it here.  I’m using this as just a display for ads and another little cushion image I added, so I won’t go too much into styling this widget container.

In the next article we will look at displaying this container on every page of your blog, as I do here (browse around and see it in action).

Displaying our New Widget Container on WordPress

Keep Multiple Ruby Projects Separated

RVM LogoHow do I keep multiple Ruby projects separate?.

This is really helpful article for those of us that have many projects that span great amounts of time.  It gives you the high level of how to leverage RVM, with Bundler for managing deployment recipes on servers that may have different versions of everything for each project.

I am going to probably build on this with an example, since I need to use this technique on my production server.  Stay tuned, as he states in the article, I always have much more going on than I can actually accomplish, so hope to do this on the next weekend.

© 2012 - Jeff Ancel
Wordpress Themes
Scroll to Top