简体   繁体   中英

MySql Table wise Backup and Restore

I'm working on Mysql data backup and restore system but stuck that how to go with it. An Option which i may think of

Table Wise Backup Method

Creating A Directory With Dateandtime and directoy will have one definition text file with all databases and its table names plus seperate file for each table contaning table structure and table INSERTS.

Table Wise Restore Method

Reading directory and definition file to Sort backups with respect to dates and table names and user can select either all tables or one specific table to restore.

I'll be using PHP for this purpose as i have to upload these backup files automatically on different servers.

Questions

1- Is above backup and restore method is valid?

2- Is there a way by which i can write single file for each Database but still have some way to restore only selected or all tables in database?

3- What are important points i must to keep in mind for such applications?

Please let me know if anything Ambiguous?

Try mysqldump with a simple shell script. It can be extended to dump tables. It now only dumps databases.

#!/bin/bash

USER="username"
PASSWORD="password"
OUTPUTDIR="./"
MYSQLDUMP=`which mysqldump`
MYSQL=`which mysql`

# get a list of databases
databases=`$MYSQL --user=$USER --password=$PASSWORD \
 -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`

# dump each database in turn
for db in $databases; do

    # skip internal databases
    if [ "$db" == "information_schema" ] || [ "$db" == "mysql" ]; then
        continue
    fi  

    # dump whole database 
    echo "$db"
    $MYSQLDUMP --force --opt --user=$USER --password=$PASSWORD \
    --databases $db > "$OUTPUTDIR/$db.sql.bak"

    # get a list of tables inside database
    tables=`$MYSQL --user=$USER --password=$PASSWORD \
     -e "USE $db;SHOW TABLES;" | tr -d "| " | grep -v Tables_in_$db`

    # dump tables
    for tbl in $tables; do
        echo "$db.$tbl"
        $MYSQLDUMP --force --opt --user=$USER --password=$PASSWORD \
         $db $tbl> "$OUTPUTDIR/$db.$tbl.sql.bak"
    done
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM