Skip to content

Instantly share code, notes, and snippets.

@graysky
Created May 14, 2010 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graysky/401739 to your computer and use it in GitHub Desktop.
Save graysky/401739 to your computer and use it in GitHub Desktop.
# Probably a better way, but couldn't find one to make a mysql database dump
# that includes the schema of all tables, but excludes the contents of certain tables (like sessions, delayed_jobs)
# that just bloat the size of the backup.
#
def mysql_db_backup
raw_dump = Tempfile.new("raw_db_dump")
dump_file = Tempfile.new("dump")
# First dump just the full schema
cmd = "mysqldump --quick --single-transaction --no-data --create-options #{mysql_options} > #{raw_dump.path}"
run(cmd)
# Then the content excluding unneeded tables: sessions and delayed_jobs
db = db_credentials[:database]
cmd = "mysqldump --quick --single-transaction --ignore-table=#{db}.sessions --ignore-table=#{db}.delayed_jobs --create-options #{mysql_options} >> #{raw_dump.path}"
run(cmd)
# Now gzip compress
cmd = "gzip -c #{raw_dump.path} > #{dump_file.path}"
run(cmd)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment