sh 备份或还原单个数据库表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 备份或还原单个数据库表相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env bash

# Backup a table. Naming defaults to *_backup, pass additional argument for *_whatever.
function backup_table {
    local t1=${1}
    local t2=${1}_${2:-backup}
    rails db << EOF
    \! echo "dropping $t2 if it exists..."
    drop table if exists $t2;
    \! echo "creating $t2..."
    create table $t2 like $t1;
    \! echo "copying $t1 to $t2..."
    insert into $t2 select * from $t1;
    \! echo "Done!"
    select count(*) from $t2;
EOF
}

# Restore a table. Assumes *_backup, pass additional argument for *_whatever.
function restore_table {
  local t1=${1}
  local t2=${1}_${2:-backup}
  rails db << EOF
    \! echo "dropping $t1..."
    drop table $1;
    \! echo "creating $t1..."
    create table $t1 like $t2;
    \! echo "copying $t2 to $t1...";
    insert into $t1 select * from $t2;
    \! echo "Done!"
    select count(*) from $t1;
EOF
}

以上是关于sh 备份或还原单个数据库表的主要内容,如果未能解决你的问题,请参考以下文章

SQL 还原或备份失败数据库变成单个用户模式无法访问

xtrabackup 备份还原单库/表

mysql中怎么单独备份一个表

MySQL之XtraBackup实现完全备份增量备份数据还原

Linux/Windows下MySQL数据库的备份与还原(mysqldump)

mysql数据备份