<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>opensoul.org - Automatically backing up your remote database on deploy Changes</title>
  <id>tag:opensoul.org,2009:/2007/2/9/automatically-backing-up-your-remote-database-on-deploy/changes</id>
  <generator uri="http://mephistoblog.com" version="0.8.0">Mephisto Drax</generator>
  <link href="http://opensoul.org/2007/2/9/automatically-backing-up-your-remote-database-on-deploy/changes.xml" rel="self" type="application/atom+xml"/>
  <link href="/2007/2/9/automatically-backing-up-your-remote-database-on-deploy" rel="alternate" type="text/html"/>
  <updated>2007-02-15T19:35:47Z</updated>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:opensoul.org,2007-02-15:251:3</id>
    <published>2007-02-09T15:08:00Z</published>
    <updated>2007-02-15T19:35:47Z</updated>
    <link href="http://opensoul.org/2007/2/9/automatically-backing-up-your-remote-database-on-deploy" rel="alternate" type="text/html"/>
    <title>Automatically backing up your remote database on deploy</title>
<content type="html">&lt;p&gt;Ever had a migration fail when you&#8217;re deploying a sparkling new release of your snazzy web app, leaving your database in an inconsistent state? I have. Fortunately, a faulty migration has never completely hosed my production database, but I have had to ssh in, comment some lines out of a migration, and re-run it.&lt;/p&gt;


	&lt;p&gt;Now, I can rest assured that I&#8217;ll never have the experience of hosing my production database without being able to recover it.  Here are Capistrano recipes to backup your remote production database whenever you run &lt;code&gt;migrate&lt;/code&gt; on your remote database.&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;require 'yaml'

desc &amp;quot;Backup the remote production database&amp;quot;
task :backup, :roles =&amp;gt; :db, :only =&amp;gt; { :primary =&amp;gt; true } do
  filename = &amp;quot;#{application}.dump.#{Time.now.to_i}.sql.bz2&amp;quot;
  file = &amp;quot;/tmp/#{filename}&amp;quot;
  on_rollback { delete file }
  db = YAML::load(ERB.new(IO.read(File.join(File.dirname(__FILE__), 'database.yml'))).result)['production']
  run &amp;quot;mysqldump -u #{db['username']} --password=#{db['password']} #{db['database']} | bzip2 -c &amp;gt; #{file}&amp;quot;  do |ch, stream, data|
    puts data
  end
  `mkdir -p #{File.dirname(__FILE__)}/../backups/`
  get file, &amp;quot;backups/#{filename}&amp;quot;
  # capistrano &amp;lt; 1.4
  # `rsync #{user}@#{roles[:db][0].host}:#{filename} #{File.dirname(__FILE__)}/../backups/`
  delete file
end

desc &amp;quot;Backup the database before running migrations&amp;quot;
task :before_migrate do 
  backup
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The backup recipe is adapted from ones presented by &lt;a href=&quot;http://blog.caboo.se/articles/2006/12/28/a-better-capistrano-backup&quot;&gt;Caboo.se&lt;/a&gt; and &lt;a href=&quot;http://source.mihelac.org/articles/2007/01/11/capistrano-get-method-download-files-from-server&quot;&gt;Bojan Mihelac&lt;/a&gt;. I modified it to use my local database.yml. Thanks to &lt;a href=&quot;http://daniel.collectiveidea.com/blog&quot;&gt;Daniel Morrison&lt;/a&gt;, who came up with the idea of running the backup task before migrating.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:opensoul.org,2007-02-09:230:2</id>
    <published>2007-02-09T15:08:09Z</published>
    <updated>2007-02-09T15:14:27Z</updated>
    <link href="http://opensoul.org/2007/2/9/automatically-backing-up-your-remote-database-on-deploy" rel="alternate" type="text/html"/>
    <title>Automatically backing up your remote database on deploy</title>
<content type="html">&lt;p&gt;Ever had a migration fail when you&#8217;re deploying a sparkling new release of your snazzy web app, leaving your database in an inconsistent state? I have. Fortunately, a faulty migration has never completely hosed my production database, but I have had to ssh in, comment some lines out of a migration, and re-run it.&lt;/p&gt;


	&lt;p&gt;Now, I can rest assured that I&#8217;ll never have the experience of hosing my production database without being able to recover it.  Here are Capistrano recipes to backup your remote production database whenever you run &lt;code&gt;migrate&lt;/code&gt; on your remote database.&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;require 'yaml'

desc &amp;quot;Backup the remote production database&amp;quot;
task :backup, :roles =&amp;gt; :db, :only =&amp;gt; { :primary =&amp;gt; true } do
  filename = &amp;quot;#{application}.dump.#{Time.now.to_i}.sql.bz2&amp;quot;
  file = &amp;quot;/tmp/#{filename}&amp;quot;
  on_rollback { delete file }
  db = YAML::load(ERB.new(IO.read(File.join(File.dirname(__FILE__), 'database.yml'))).result)['production']
  run &amp;quot;mysqldump -u #{db['username']} --password=#{db['password']} #{db['database']} | bzip2 -c &amp;gt; #{file}&amp;quot;  do |ch, stream, out|
    puts data
  end
  `mkdir -p #{File.dirname(__FILE__)}/../backups/`
  get file, &amp;quot;backups/#{filename}&amp;quot;
  # capistrano &amp;lt; 1.4
  # `rsync #{user}@#{roles[:db][0].host}:#{filename} #{File.dirname(__FILE__)}/../backups/`
  delete file
end

desc &amp;quot;Backup the database before running migrations&amp;quot;
task :before_migrate do 
  backup
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The backup recipe is adapted from ones presented by &lt;a href=&quot;http://blog.caboo.se/articles/2006/12/28/a-better-capistrano-backup&quot;&gt;Josh Susser&lt;/a&gt; and &lt;a href=&quot;http://source.mihelac.org/articles/2007/01/11/capistrano-get-method-download-files-from-server&quot;&gt;Bojan Mihelac&lt;/a&gt;. I modified it to use my local database.yml. Thanks to &lt;a href=&quot;http://daniel.collectiveidea.com/blog&quot;&gt;Daniel Morrison&lt;/a&gt;, who came up with the idea of running the backup task before migrating.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:opensoul.org,2007-02-09:229:1</id>
    <updated>2007-02-09T15:08:07Z</updated>
    <link href="http://opensoul.org/2009/3/12/automatically-backing-up-your-remote-database-on-deploy" rel="alternate" type="text/html"/>
    <title>Automatically backing up your remote database on deploy</title>
<content type="html">&lt;p&gt;Ever had a migration fail when you&#8217;re deploying a sparkling new release of your snazzy web app, leaving your database in an inconsistent state? I have. Fortunately, a faulty migration has never completely hosed my production database, but I have had to ssh in, comment some lines out of a migration, and re-run it.&lt;/p&gt;


	&lt;p&gt;Now, I can rest assured that I&#8217;ll never have the experience of hosing my production database without being able to recover it.  Here are Capistrano recipes to backup your remote production database whenever you run &lt;code&gt;migrate&lt;/code&gt; on your remote database.&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;require 'yaml'

desc &amp;quot;Backup the remote production database&amp;quot;
task :backup, :roles =&amp;gt; :db, :only =&amp;gt; { :primary =&amp;gt; true } do
  filename = &amp;quot;#{application}.dump.#{Time.now.to_i}.sql.bz2&amp;quot;
  file = &amp;quot;/tmp/#{filename}&amp;quot;
  on_rollback { delete file }
  db = YAML::load(ERB.new(IO.read(File.join(File.dirname(__FILE__), 'database.yml'))).result)['production']
  run &amp;quot;mysqldump -u #{db['username']} --password=#{db['password']} #{db['database']} | bzip2 -c &amp;gt; #{file}&amp;quot;  do |ch, stream, out|
    puts data
  end
  get file, &amp;quot;backups/#{filename}&amp;quot;
  # capistrano &amp;lt; 1.4
  # `rsync #{user}@#{roles[:db][0].host}:#{filename} #{File.dirname(__FILE__)}/../backups/`
  delete file
end

desc &amp;quot;Backup the database before running migrations&amp;quot;
task :before_migrate do 
  backup
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The backup recipe is adapted from ones presented by &lt;a href=&quot;http://blog.caboo.se/articles/2006/12/28/a-better-capistrano-backup&quot;&gt;Josh Susser&lt;/a&gt; and &lt;a href=&quot;http://source.mihelac.org/articles/2007/01/11/capistrano-get-method-download-files-from-server&quot;&gt;Bojan Mihelac&lt;/a&gt;. I modified it to use my local database.yml. Thanks to &lt;a href=&quot;http://daniel.collectiveidea.com/blog&quot;&gt;Daniel Morrison&lt;/a&gt;, who came up with the idea of running the backup task before migrating.&lt;/p&gt;</content>  </entry>
</feed>
