Restoring select databases/tables from an --all-databases backup

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Raffi

    Restoring select databases/tables from an --all-databases backup

    I use the --all-databases switch to backup my entire database.
    Sometimes there's a need to restore individual databases or tables form
    the backup file. What command should I use for this?

    Thanks,
    Raffi

  • Bill Turczyn

    #2
    Re: Restoring select databases/tables from an --all-databases backup


    Taken from: Linux Server Hacks
    By Rob Flickenger

    Here is a method for restoring a single mysql table from a huge
    mysqldump

    Like a good admin, you faithfully dump your mysql tables every night,
    and save them to the filesystem in compressed form (presumably to be
    picked up by a backup script later). You probably have something like
    this running in cron on your database server (or one of its replicated
    slaves):

    for x in `mysql -Bse show databases`; do
    mysqldump $x | gzip -9 > /var/spool/mysqldump/$x.`date +%Y%m%d`.gz
    done


    This will cover you if anything catastrophic happens to your live
    database. But if your database grows to an appreciable size, doing
    partial restores can be difficult. On a database with several million
    rows, your dumps suddenly become massive piles of data that need to be
    sifted through. How can you easily restore a single table out of a
    several hundred megabyte compressed dump?

    Here's a simple method using Perl. Create a script called
    extract-table, with this in it:

    #!/usr/bin/perl -wn
    BEGIN { $table = shift @ARGV }
    print if /^create table $table\b/io .. /^create table (?!$table)\b/io;


    To extract the User table from the dump of a database called randomdb,
    try something like this:

    # zcat /var/spool/mysqldump/randomdb.200209 01.gz | extract-table Users[color=blue]
    > ~/[/color]
    Users.dump


    Now you can restore your Users table with a simple:

    # mysql randomdb -e "drop table Users"
    # mysql randomdb < ~/Users.dump

    Comment

    • Raffi

      #3
      Re: Restoring select databases/tables from an --all-databases backup

      Bill Turczyn wrote:[color=blue]
      > Taken from: Linux Server Hacks
      > By Rob Flickenger
      >
      > Here is a method for restoring a single mysql table from a huge
      > mysqldump
      >
      > Like a good admin, you faithfully dump your mysql tables every night,
      > and save them to the filesystem in compressed form (presumably to be
      > picked up by a backup script later). You probably have something like
      > this running in cron on your database server (or one of its[/color]
      replicated[color=blue]
      > slaves):
      >
      > for x in `mysql -Bse show databases`; do
      > mysqldump $x | gzip -9 > /var/spool/mysqldump/$x.`date +%Y%m%d`.gz
      > done
      >
      >
      > This will cover you if anything catastrophic happens to your live
      > database. But if your database grows to an appreciable size, doing
      > partial restores can be difficult. On a database with several million
      > rows, your dumps suddenly become massive piles of data that need to[/color]
      be[color=blue]
      > sifted through. How can you easily restore a single table out of a
      > several hundred megabyte compressed dump?
      >
      > Here's a simple method using Perl. Create a script called
      > extract-table, with this in it:
      >
      > #!/usr/bin/perl -wn
      > BEGIN { $table = shift @ARGV }
      > print if /^create table $table\b/io .. /^create table[/color]
      (?!$table)\b/io;[color=blue]
      >
      >
      > To extract the User table from the dump of a database called[/color]
      randomdb,[color=blue]
      > try something like this:
      >
      > # zcat /var/spool/mysqldump/randomdb.200209 01.gz | extract-table[/color]
      Users[color=blue][color=green]
      > > ~/[/color]
      > Users.dump
      >
      >
      > Now you can restore your Users table with a simple:
      >
      > # mysql randomdb -e "drop table Users"
      > # mysql randomdb < ~/Users.dump[/color]

      That's exactly what is happening and many times only specific
      tables/databases get corrupted and it's inefficient to restore the
      complete database structure. I'll try the script out as soon as I get a
      chance. I guess mysql doesn't have a built in feature that resores
      individual tables and databases from a full backup and leaves
      everything else alone.

      Raffi

      Comment

      Working...