MySQL database entry - Backup Database

mysqldump can export all database operations for a single table, multiple tables, a single database, multiple databases

mysqldump common options
 -h, --host = name: server IP
 -u, --user = name: logins
 -p, --password [= name]: Password
 -A, --all- Databases: Export all databases
 -B, - databases: export specified database, a plurality of database names separated by spaces
 - tables: export specified table
 -d, --no- data: export table only and is not exporting data
 -t, --no- create- info: Do not export table creation statements
 -n, --no-create- db: Do not export cREATE DATABASE IF EXISTS statement
 -e, --extended- iNSERT: merge multiple records into an iNSERT statement to increase the efficiency of insertion
 - drop---add table: DROP tABLE statement is added before creating the table
 --hex- BLOB: deriving the binary data in hexadecimal
 -R & lt, - routines: export stored procedures and stored functions
 --triggers: deriving trigger

 

Single database backup

mysqldump -uroot -p test >/download/testbak_$(date +%F).sql

Enter password: 

ll /download/

 

When the library automatically creates restore -B

-B effect parameters, that is, when our database is missing, you can directly use this backup file recovery, without having to re-build the library, built form, and then perform data recovery operation

mysqldump -uroot -p -B test >/download/testbak_$(date +%F)_b.sql

Compressed backup

mysqldump -uroot -p -B test|gzip >/download/testbak_$(date +%F).sql.gz

Backup single table

mysqldump -uroot -p -B test test >/download/test_testbak_$(date +%F).sql      

Back up only the table structure

mysqldump --no-data --databases mydatabase1 mydatabase2 mydatabase3 > test.dump

 

1 . Data backup and database structures all 

the mysqldump -uroot--p123456 -A> F.: \ All.sql 

2 backup of all configuration database (plus -. D parameter) 

the mysqldump -uroot--p123456 -Ad> F.: \ All_struct.sql 

3 . all the backup data in the database (plus - T parameter) 

the mysqldump -uroot--p123456 -At> F.: \ all_data.sql 

. 4 a backup of a single database and data structure (database name mydb). 

the mysqldump -uroot--p123456 mydb> F. : \ mydb.sql 

. 5 the structure of a single database backup. 

the mysqldump -uroot-D--p123456 mydb> F.: \ mydb.sql 

. 6 data backup of a single database. 

the mysqldump -uroot-T--p123456 mydb> F.: \ mydb.sql 

. 7 the data structures and a plurality of backup tables (data, and a separate backup method of the same structure)

the mysqldump -uroot--p123456 mydb T1 T2> F: \ multables.sql 

. 8 . multiple database backup 

the mysqldump -uroot-DB1 DB2 -p123456 --databases> F: \ muldbs.sql

 

Simple backup script

bak.sh 

! # / bin / SH
 

IP = `grep ' the IPADDR ' / etc / sysconfig / Network-scripts / the ifcfg-eth0 | awk -F " = "  ' {Print $ 2} ' ` 

# definition server IP variable 

BAKDIR = / Backup   

# define backup path 

[ ! BAKDIR -d $ / $ {IP}] && BAKDIR mkdir -p $ / $ {IP} 

# if there is no determination that a path is created, the server in order to see more easily when 

DB_PWD = " 123456 " 

DB_USER = " the root " 

MYSQL Based = " / file application / MySQL / bin / MySQL " 

MYSQL_DUMP = "/ file application / MySQL / bin / the mysqldump " 

the DATA =% +` DATE F` 

#### of Test Data BAK ' S #### databses 

the DB_NAME = -u MYSQL Based `$ $ $ DB_PWD DB_USER -p -e " Show Databases ; " | Sed ' 1,5d ' ` 

# define variables database 

for name in $ the DB_NAME 

#for loop take library name 

do 

  $ MYSQL_DUMP -u -p DB_USER $ $ $ {name} DB_PWD -B | the gzip> BAKDIR $ / $ IP} {/ $ {name} _ $ DATA.sql.gz   

# full backup 

  [ ! BAKDIR -d $ / $ {IP} / $ {name}] && BAKDIR mkdir -p $ / $ {IP} / $ { } name 

# judge this path, in order to distinguish which library backup files 

  for TableNamein `MYSQL Based -u $ $ $ DB_PWD DB_USER -p -e " Show Tables from $ {name}; " | Sed ' 1D ' ` 

#for loop statement taken table 

  do 

   $ MYSQL_DUMP -u -p DB_USER $ $ $ {DB_PWD TableName}} $ {name | the gzip> BAKDIR $ / $ {IP} / $ {name} / $ {} _ $ TableName DATA.sql.gz 

# minutes table backup 

  DONE 

DONE

 

backup.sh

#vi /backup/backup.sh

#!bin/bash
cd /backup
echo "You are in backup dir"mv backup* /oldbackup
echo "Old dbs are moved to oldbackup folder"
File = backup-$Now.sql
mysqldump -u user -p password database-name > $File
echo "Your database backup successfully completed"
 
Save the above script file backup.sh, and the system has created two directories / olcbackup and / backup. Each time backup.sh perform all the names moved / oldbackup directory for file backup at the beginning of the first will be / backup directory.
      Develop implementation plans for the above script follows:
#crontab -e
30 1 * * * /backup.sh

 

The combination of Linux cron command to achieve regular backup
Such as the need to back up all databases on a host at 1:30 am every day and gz compressed dump file format, you can add the following line of code in / etc / crontab configuration file:
30 1 * * * root mysqldump -u root -pPASSWORD --all-databases | gzip > /mnt/disk2/database_`date '+%m-%d-%Y'`.sql.gz

 

reduction

Partial reduction portion ( 1 ) and MySQL command line source method ( 2 ) Method system command line 

1 . All Restore Database: 

( 1 ) MySQL command line: MySQL> source F: \ all.sql 

( 2 ) system command line: mysql - -p123456 -uroot < F: \ all.sql 

2 individual database (to be specified database). 

( . 1 ) MySQL> use mydb 

    MySQL > Source F: \ mydb.sql 

( 2 ) MySQL -uroot--p123456 mydb < F: \ mydb .sql 

. 3 . a plurality of tables (to be specified database) the individual database 

( . 1 ) MySQL> use mydb 

    MySQL > Source F: \ multables.sql 

( 2) MySQL -uroot--p123456 mydb < F: \ multables.sql 

. 4 . Restore multiple databases, (a backup file backup plurality of databases, the database need not be specified at this time) 

( . 1 ) MySQL command line: MySQL> Source F: \ muldbs.sql 

( 2 ) system command line: mysql -uroot -p123456 <f: \ muldbs.sql

 

Guess you like

Origin www.cnblogs.com/alter888/p/11130403.html