New Plugin: MySQL Tasks 13

Posted by sbecker Sun, 10 Dec 2006 22:59:00 GMT

I wrote some convenience rake tasks to automate creation and backup of MySQL databases, and I use it all the time, so I figured I’d plugin-afy it to make it simple to use in all my projects, and share it with the world as well! Here’s the repository

MySQL Tasks

Some rake tasks to automate common database tasks (create/destroy & backup/restore).

Install

./script/plugin install http://sbecker.net/shared/plugins/mysql_tasks

Components

rake db:mysql:create           # Create database (using database.yml config)
rake db:mysql:destroy          # Destroy database (using database.yml config)
rake db:mysql:backup           # Dump schema and data to an SQL file (/db/backup_YYYY_MM_DD.sql)
rake db:mysql:restore          # Load schema and data from an SQL file (/db/restore.sql)

Specifying RAILS_ENV works if you want to perform operations on test or production databases.

Comments

Leave a response

  1. Avatar
    Nathan Tue, 12 Dec 2006 22:32:04 GMT

    Very nice, this just saved me a 1/2 hour of my life. Thanks!

  2. Avatar
    Nathan Tue, 12 Dec 2006 22:32:53 GMT

    Sorry for the double post, nothing seemed to be happening with the AJAX form … :(

  3. Avatar
    Jason Perry Wed, 20 Dec 2006 20:37:41 GMT

    Maybe use /usr/bin/env instead for mysql_bin_root = "/usr/bin"?

  4. Avatar
    Jason Perry Wed, 20 Dec 2006 20:39:44 GMT

    Maybe use /usr/bin/env instead for mysql_bin_root = "/usr/bin"? Heh, I’ll submit a patch.

  5. Avatar
    Scott Becker Wed, 17 Jan 2007 02:05:53 GMT

    Jason – I’ve updated it to use your method, /usr/bin/env to properly find mysql…

    Yurii – I tried your patch but for some reason it didn’t work for me. The command “env mysql 2>/dev/stdout” brings my right to the mysql prompt, so it hangs there… See if the latest version works as-is… Seems to work fine for me.

  6. Avatar
    Jim Nicholson Tue, 27 Feb 2007 02:38:25 GMT

    I ran into a problem with systems that don’t have a password for the mysql “root” user. I had to change the mysql_execute method as follows:

    def mysql_execute(username, password, sql)
        pwd_opt = password.nil? ? '' : "-p#{password}" 
        system("/usr/bin/env mysql -u #{username} #{pwd_opt} --execute=\"#{sql}\"")
    end
    

    This avoids the ‘password’ prompt being issued when mysql is invoked.

  7. Avatar
    Scott Tadman Tue, 17 Jul 2007 22:13:59 GMT

    This would be handy to include, too:

    desc “Open shell to database (using database.yml config)” task :shell => :environment do database, user, password = retrieve_db_info mysql_shell(user, password, database) end

    1. ...

    def mysql_shell(username, password, database) system(”/usr/bin/env mysql -u #{username} -p’#{password}’ #{database}”) end

  8. Avatar
    Mark Ipkins Wed, 12 Sep 2007 12:31:57 GMT

    Hello, i like cars! Want to have some friends!

  9. Avatar
    Mark Ipkins Wed, 12 Sep 2007 12:34:54 GMT

    Hello, i like cars! Want to have some friends!

  10. Avatar
    J4nus Sun, 07 Oct 2007 20:14:36 GMT

    Nice ! it works ! very usefull.. and should be integrated in Rails in the future ..

  11. Avatar
    Play Blackjack On Line Wed, 13 Feb 2008 05:13:53 GMT

    Cheers for the tip. Saved me quite a bit of time from reading the mySQL docs.

    Thanks mate.

  12. Avatar
    Steve Wed, 17 Dec 2008 21:40:54 GMT

    I incorporated Jim’s change as well as a change to the restore tasks in order to support databases without a password.

    http://github.com/eichert12/mysql_tasks/tree/master

    Probably should kill the create/destroy tasks as well since they’re now in Rails.

  13. Avatar
    manman Sat, 13 Jun 2009 09:51:18 GMT
Comments