All types of backups and restore


Published on

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

All types of backups and restore

  1. 1. Contents:A. Backup a single DatabaseB. Backup Multiple DatabasesC. Backup all the DatabasesD. Restore all the DatabasesE. Restore a Single DatabaseF. Backup a specific TableG. Import data in MySQL.H. Backup (Script on Linux)I. Import and Export on MySQL DatabaseJ. MySQL Backups on WindowsK. Different types of Backups for MySQL databasesL. Dump MySQL database into a new database on another serverM. How to dump local MySQL data to remote MySQL server through terminal?N. Loading a SQL file into MySQLO. Loading CSV or tab delimited files into MySQLP. Mysqldump Upgrading to 4.1Q. Simple backup options
  2. 2. (A) Backup a single database:Using mysqldump, you can backup a local database and restore it on a remote databaseat the same time, using a single command. In this article, let us review several practicalexamples on how to use mysqldump to backup and restore. For the impatient, here is thequick snippet of how backup and restore MySQL database using mysqldump:Backup: #mysqldump –u root –p[root password] [database name] >dumpfilename.sqlRestore: #mysql –u root –p[root password] [databasename] < dumpfilename.sqlThis example takes a backup of sampledb database and dumps the output to dbdump.sql[mysql@localhost]# /usr/bin/mysqldump –u root –pmysql sampled > /tmp/dbdump.sql[mysql@localhost]# mysqldump –u root –p[rootpassword] [databasename] >/tmp/dbdump.sql (location of data-dir)After you press enter to execute the command, the backup will be generated. If youbrowse to this path: /tmp, then you should see dbdump.sql in there.(B) Backup multiple databases:If you want to backup multiple databases, first identify the databases that you want tobackup using the show databases as shown below:# mysql -u root -ppasswordmysql> show databases;
  3. 3. +--------------------+| Database |+--------------------+| information_schema || newdb || mysql || sampledb |For example, if you want to take backup of both sampledb and newdb database, executethe mysqldump as shown below:[mysql@localhost]# mysqldump –u root –p[rootpassword] --databasessampledbnewdb> /tmp/dbdump.sqlVerify the dbdump.sqldump file contains both the database backup.#grep –I “Current database:” /tmp/dbdump.sql--Current database: ’newdb’--Current database:’sampledb’(C) Backup all the databases:The following example takes a backup of all the database of the MySQL instance.# mysqldump -u root -ptmppassword --all-databases > /tmp/all-database.sql(D) Restore all the databases:(How to restore mysqldump –all-databases backup ?)[mysql@INVIRH54DB3 ~]$ mysql -u root -p < /tmp/alldbs55.sqlEnter password: mysqlOrMysql –u root –ppassword@<alldatabases.sql
  4. 4. (E) Restore a singledatabase:In this example, to restore the newdb database, execute mysql with < as shown below.When you are restoring the dumpfilename.sql on a remote database, make sure to createthe newdb database before you can perform the restore# mysql -u root -p[root_password] [database_name] <dumpfilename.sql
  5. 5. (F) Backup a specific table:In this example, we backup only the ta2 table from sampledb database.#mysqldump –u root –ptemppasswordsample ta2 > /tmp/nwedb_ta2.sql(OR)#mysqldump –c –u username –ppassworddatabasenametablename>/tmp/databasename.tablename.sql
  6. 6. Dump process:
  7. 7. (G) Import data in MySQLCreate an employee table and employee.txt data file.For the examples, let us create a very simple employee table with three columns–employee number, employee name andjob.(1)Mysql -u root -ptmppasswordMysql> use testDatabase changedmysql> create table employee (empnoint,enamevarchar(15),job varchar(10));(2)Create a test datafile employee.txt with fields delimited by tab as shown below.Cat employee.txt100 John Doe DBA200 John Smith Sysadmin300 Raj Patel Developer(3)Upload tab delimited datafile to MySQL tableUse mysqlimport to import the employee.txt datafile to employee table in test database,as shown below:mysqlimport -u root -ptmppassword --local test employee.txttest.employee: Records: 3 Deleted: 0 Skipped: 0 Warnings: 0(4)Verify that the records got uploaded successfully.mysql -u root -ptmppasswordmysql> use test;mysql> select * from employee;Note: In mysqlimport, the name of the datafile should match the name of the table. Theextension of the datafile can be anything. In the above example, only employee.* datafile
  8. 8. can be used to upload data to employee table. You’ll get the following error messagewhen the filename is not same as tablename:mysqlimport -u root -ptmppassword --local test emp.txtmysqlimport: Error: Table test.emp doesnt exist, when using table: emp[Note: The table name is employee. So, datafile name should be employee.*](5) Import multiple datafiles into multiple MySQL tables:The following example uploads data from two different datafiles to two different tables.I.e. It uploads employee.txt to employee table and manager.txt to manager table.mysqlimport -u root -ptmppassword --local test employee.txt manager.txt(6) Use LOAD DATA LOCAL INFILE to upload data to MySQL tables:The mysqlimport client is simply a command-line interface to the LOAD DATA LOCALINFILE SQL statement. Most options to mysqlimport correspond directly to clauses of“load data local infile” syntax. You can perform the same upload explained in example#1using “load data local infile” instead of mysqlimport as explained below:mysql -u root -ptmppasswordmysql> use test;mysql> LOAD DATA LOCAL INFILE /home/ramesh/employee.txt INTO TABLE employeeFIELDS TERMINATED BY t LINES TERMINATED BY n (empno, ename, job);Records: 3 Deleted: 0 Skipped: 0 Warnings: 0mysql> select * from employee;+-------+------------+-----------+| empno | ename | job |+-------+------------+-----------+| 100 | John Doe | DBA || 200 | John Smith | Sysadmin || 300 | Raj Patel | Developer |+-------+------------+-----------+3 rows in set (0.00 sec) Load a CSV file into a table.
  9. 9. Mysql> LOAD DATA INFILE ‘/tmp/filename.csv replace into table *tablename+ fieldsterminated by ‘,’ lines terminated by ‘n’ (field1,field2,field3);(7)Most frequently used mysqlimport options:The most frequently used mysqlimport options are shown in the example below. Most ofthese options are self-explanatory.Compress: Compress all information sent between the client and the serverdelete: This option is very handy when you want to empty the table before importing thetext file local: Read input files locally from the client hostlock-tables: Lock all tables for writing before processing any text files. This ensures that alltables are synchronized on the server.1. mysqlimport --user=root --password=tmppassword --columns=empno,ename,job --compress --delete --fields-optionally-enclosed-by=" --fields-terminated-by=t --fields-escaped-by= --lines-terminated-by=n --local --lock-tables --verbose test employee.txtOutput of the above mysqlimport command:Connecting to localhostSelecting database testLocking tables for writeDeleting the old data from table employee
  10. 10. Loading data from LOCAL file: /home/ramesh/employee.txt into employeetest.employee: Records: 3 Deleted: 0 Skipped: 0 Warnings: 0Disconnecting from localhostIssue while taking a backup on mysql Backup (Script on Linux) :[root@mysql backup]# cat dbbackup.shMAIL_ADMINS=""LOGFILE=/backup/dbbackup.log#Clear the Log filerm -f $LOGFILE#Backup MySQL Databaseif/usr/bin/mysqldump --verbose --log-error=$LOGFILE -u root -pabcd --all-databases |gzip> /backup/database_`date +%m-%d-%Y`.sql.gzthenmail -s "$HOSTNAME MySQL Backup Sucessful" $MAIL_ADMINS < $LOGFILEexit 0elsemail -s "$HOSTNAME MySQL Backup Failed" $MAIL_ADMINSexit 1fiClean Up all files older than 10 days-find /backup/ -mtime +10 -exec rm -Rf {} ;(I) How to import and Export MySQL Database.To export MySQL DB:1.Type the below command to export MySQL db.#mysqldump –u root –p DBName>dbname11092012.sqlTo import MySQL DB:
  11. 11. 1. Go to file location where you saved backupdb and type following command.2. Login into MySQL and create new database.Create database newdb3.exit from sql and type as following#mysql –u root –p newdb<db_04102012.sql[where newdb is newly created db and db_04102012-0700.sql is going to restore newlycreated db.](J)MySQL backups on windows:
  12. 12. On Windows cmd prompt using the following command on taking backup.On Windows cmd prompt using the following command on restoring the tables ordatabases.
  13. 13. (K)Different types of Backups for MySQL databases(1) Logical BackupIt is done by dumping the contents of database into text files containing SQL statementswhich can be used to rebuild the database. They can be used even to build it on a hostwith different architecture and with different engine. But they are generally slower thanthe raw kind, for both backup and recovery operations. And they can be larger than theactual data too. It can be performed using the mysqldump utility.(2) Raw (Binary) BackupIt involves only file copy operations, so can be very fast to perform backup or recoveries.The engine type of tables cannot be changed in this method, as it preserves the actualdata format. Depending on engine type, it can be used to take Cold, Hot or Warmbackups. Various tools are available for this operation, which will be covered in a slideahead.(3) Snapshot Based Backup
  14. 14. This kind of backup uses external utilities to take file system snapshot of MySQL. ForInnoDB, a hot backup can be performed using this, and for other engines, a warm backup.(4) Replication-Based BackupA copy of the Primary installation can be maintained on a separate server using aReplication setup. Using this method, an exact replica of the databases can be maintainedseparately, and can be used for backup purposes. But it is comparatively an expensiveoption.(5) Incremental BackupThe binary logs, which contain a record of all changes done on the database, can bebacked up for the purpose of doing point-in-time-recovery. Using tools like mysqlbinlog,databases can be recovered from Binary logs till a certain point in time, or certain logposition also. Binary logging should be enabled on MySQL servers(6) Backup ToolsThough backups can be taken without the use of any tool, there are some tools designedspecifically to make the operation easier.(7) MysqlhotcopyProvided with the MySQL distribution, this is useful to perform raw backup of MyISAMtype tables only. Contrary to its name, it is not exactly a “hot” backup, as database is notfully available during the operation, but is available only for read. It is locked and cannotbe altered. So, it is also called a “warm” backup.(8) MySQL dumpIt is provided with the MySQL distribution. It helps in taking logical backups for any kind ofengine. Our current masters are designed to automatically enable this backup in cron atthe time of installation.(9) MySQL Administrator
  15. 15. It is available as a separate GUI download from MySQL, and can be used to performlogical backups. It can also be used with any database engine, and has some trackingcapabilities also.(10) InnoDB Hot Backup (ibbackup)It is a commercial product available from InnoDB. It performs raw backups of MySQLdatabases using InnoDB engine only. It is in fact a real “hot backup” as databases arecompletely available for read and update during the operation.(11) Third Party ToolsAmong the many commercially available third party tools available, there is one calledZmanda Recovery Manager that can perform both logical and raw backups for all enginetypes. It can do both warm and hot backups, and can drive Replication and Snapshotbased backups too. It takes consistent backups, and has extensive reporting and Trackingcapabilities(L) Dump MySQL database into a new database on another server :Here I am trying to achieve is to dump a MySQL database over to a new database on adifferent server; I also wanted to do it with limited write to the original server.The plan is to dump the old MySQL database, and import the output into the newdatabase on the second server.Here is what I’ve got; Ive done it this way so that in theory the original server will only bedoing a read, whereas the new server will be doing the write.$mysqldump –uusername –ppassworddbname | sshuser@newhostmysql –u user –ppassword newdbname( )
  16. 16. (M) How to dump local MySQL data to remote mysql server throughterminal?I’m currently working on some local development, which often needs to update theremote database with my own local development database.This is what I try to do , dump the local database and ssh to remote and update the db.mysqldump -ulocaluser -plocalpasslocaldb | ssh user@ "mysql -uremoteuser -premotepassremotedb"It seem like completed without any error, but checking on the remote db, it seem like theold table never drop and replace with the new table data.Check the output ofmysqldump -ulocaluser –plocalpass localdbDoes this contain DROPstatements?(N)Loading a SQL file into MySQL:Import SQL file from MySQL client command line:mysql> source file.sqlORmysql> . file.sqlThe SQL file may have schema generation statements like CREATE TABLE ... or dataload statements like INSERT INTO. The statements in the SQL file will be executedas if they were being specified at the MySQL client command line interface. Onemay import data into the MySQL database from SQL files or "load" data from CSVor tab delimited files using the LOAD command.(O)Loading CSV or tab delimited files into MySQL"LOAD DATA LOCAL INFILE" vs "LOAD DATA INFILE": The term "LOCAL" pertains to
  17. 17. whether the file is local to the MySQL client. Without the keyword "LOCAL", the data filemust reside on the same computer as the database server. The location of the client inthis case would be irrelevant. The "LOAD DATA INFILE" has many file permission pitfallsand is thus tricky. In fact I have never been successful using this method with a userdirectory.Load a tab delimited file into the database:Command:LOAD DATA LOCAL INFILE file.dat INTO TABLE employer;Input tab delimited file: file.datNote: The number of tab delimited fields MUST match the number and order of fields inthe database. Load a comma delimited file (CSV) into the database:Command:LOAD DATA LOCAL INFILE "/tmp/TableData.csv" INTO TABLE employerFIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" LINESTERMINATED BY "rn" (Name, Dept, jobTitle);Note:MS/Windows generated files will have lines terminated by "rn".Linux/Unix generated files will have lines terminated by "n".File locations on database server must be absolute path names, relative path or relativeto the mysqld process owners home directory (typically /var/lib/mysql/). File locations onthe client may be fully qualified or relative to the current mysql client directory.Fully qualified: /tmp/TableData.csvRelative to current mysql client directory: ./TableData.csv (Verify current directory:mysql> ! pwd)Database process owner home directory: TableData.csv(Actual:/var/lib/mysql/TableData.csv)Text strings often are encapsulated by quotes so that the strings may contain a commawithout representing a new field.
  18. 18. ERROR 13 (HY000): Cant get stat of /tmp/TableData.csv (Err code: 13)The fils is local and you have not specified the "LOCAL" directive.ERROR 29 (HY000): File /var/lib/mysql/test/TableData.csv not found (Errcode: 2)Error from command LOAD DATA INFILE TableData.csv INTO ... where the file is assumedto be read from the /database-process-home-directory/mysql-database-name/TableData.csv(Note: Database name "test" is being used.)ERROR 1045 (28000): Access denied for user user1@% (using password: YES)ORERROR 2 (HY000): File /tmp/TableData.csv not found (Err code: 2)Error from command LOAD DATA INFILE /tmp/TableData.csv INTO .... This is a commonpitfall, trying to load a file located on the database server remotely from a client. Gettingthe file permissions correct is difficult. Avoid this method. Use the LOAD DATA LOCALINFILE instead of the LOAD DATA INFILE method (it is so much easier).Also look at the MySQLimport command.Dump/Backup/Transfer Database:The mysqldump command will read the MySQL database and generate a SQL commandtext file. This allows data to be migrated to other versions of mySQL (i.e. upgrade fromtypical Red Hat (RH7.x to FC3) MySQL release 3.23.58 to a more advanced MySQL 4.1 or5.0) or to other SQL databases. SQL command file generated can create tables, insertdata.Option Description-A--all-databases Dump all the databases.-B--databases Dump the specified databases.-h--host= Specify host to connect to.-p--password= Specify password. If you do not specify a password, then you will bequeried.
  19. 19. -u--user= Specify user. Defaults to current user logged in.-a--all Include all mySQL specific SQL "create" options.-e--extended-insert Allows utilization of the new, much faster INSERTSyntax. Database you are migrating to must support this notation.-q--quick Don’t buffer query, dump directly to stdout.-l--lock-tables Lock all tables for read.-?--help Display command line options.(P) MysqldumpUpgrading to 4.1:Upgrading mySQL to 4.1 from 3.23Use the command:mysql_fix_privilege_tables --password=root-passwordThis allows you to use the new GRANT command.Restore MySql Database:Restore using dump generated by mysqldump above:mysql -h host-name -u user-id -psupersecretpassword< total-db-dump-file.sqlOn Source Server :
  20. 20. Mysql database-name -h host-name -u user-id -psupersecretpassword<db-Dump-file.sqlOn Destination server :
  21. 21. (Q) Simple backup optionsBackup all databases uncompressed from the command line (not from within MySQL):Backup:mysqldump -u root -pmypass --all-databases >alldatabases.sqlRestore full:mysql -u username -pmypass<alldatabases.sql (no space in between -p andmypass)Restore single:mysql -u username -pmypassmydb<mydb.sql (no space in between -p andmypass)Backup all databases compressed from the command line (not from within MySQL):With bzip2:mysqldump --all-databases | bzip2 -c > databasebackup.sql.bz2 (use bunzip2to uncompress)With gzip:mysqldump --all-databases | gzip> databasebackup.sql.gz (use gunzip touncompress)Backup a specific database only:mysqldump -u username -pmypassdatabasename>backupfile.sqlBackup database structure only:mysqldump --no-data --databases databasename>structurebackup.sqlBackup a specific database and specific tables within that database only:mysqldump --add-drop-table -u username -pmypassdatabasename table_1 table_2>databasebackup.sqlThe syntax for the command to issue is:mysqldump -u [username] -p[password] [databasename] [table1 table2 ....]>backupfilename.sql
  22. 22. Backing up only the database structure of specific databases, not the actual data:mysqldump --no-data --databases db1 db2 db3 >structurebackup.sql@@@@@@@@@@@@@@@@@@@@@BACKUP AND RECOVER MySQL DATABASEIt is important to back up your databases so that you can recover your data and be up and running again in caseproblems occur, such as system crashes, hardware failures, or users deleting data by mistake.Types of Backup:logical backupThis type of backup is created by saving information that represents thelogical database structures using SQL statements like create database, createtable and insert. This type of backup is ideal when you want to upgrade fromone version of MySQL to another however it is a slower method of backingup.physicalbackupThis type of backup is a backup of the actual database files or disk partitions,this type of backup can be very fast to backup and restore.full backupA full backup is a standalone backup containing everything in the database,this could then be restored on another server. A full backup can be eitherlogical or physical.incrementalbackupThis type of backup only contains the data that has changed from the lastbackup. The advantage of this type of backup is that it is faster as there isnot some much data to backup, however the disadvantage is that it takeslonger to recover.consistentbackupThis is a backup at an exact moment in time, generally you shutdown thedatabase (or quiescent mode) then take the backupThis type of backup is taken when the database is running, during the backupboth reads and writes are not blocked
  23. 23. warm backupThis type of backup is taken when the database is running, however readsare not blocked but writes are prohibited from making any modifications tothe database.cold backupSimilar to a consistent backup as the database is shutdown before thebackup beginspoint-in-timerestoreIt is a restoration of a database to a specified date and time , somedatabases use a full backup and recovery logs to restore to that point-in-time, others can only use the last full backup which means that data mighthave to be re-keyed into the system.Types of Backup tools:Backup tools for MySQLBackup methodStorageengineImpactBackupspeedRecoveryspeedRecoverygranularitymysqldump ALL WARM MEDUIM SLOWESTMOSTFLEXIBLEmysqldump INNODB HOT MEDUIM SLOWESTMOSTFLEXIBLEselect into outfile ALL WARM SLOW SLOWMOSTFLEXIBLEmk-parallel-backup ALL WARM MEDUIM MEDUIM FLEXIBLEibbackup INNODB HOT FAST FAST FLEXIBLEibbackup ALL WARM FAST FAST FLEXIBLEbackup commandin mysqldALL HOT FAST FAST FLEXIBLEfilesystem (copy files) ALL COLD FASTEST FASTEST NOT FLEXIBLE
  24. 24. snapshot (using LVM,ZFS, VMWare)ALLALMOSTHOTFAST FASTLEASTFLEXIBLEmysqlhotcopy MyISAMMOSTLYCOLDFAST FAST FLEXIBLEMySQLdump:mysqldump is an effective tool to backup MySQL database. It creates a *.sql file with DROP table, CREATEtable and INSERT into sql-statements of the source database. To restore the database, execute the *.sql file ondestination database.mysqldump# backup all databasesmysqldump --user=root --password --all-databases > backup_<date>_all.sql# backup a specific databasemysqldump --user=root --password <database_name>>backup_<date>_<database_name>.sql# backup multiple databasesmysqldump --user=root --password <database_name>,<database_name>>backup_<date>.sql# backup a table from a databasemysqldump --user=root --password <database_name><table_name>>backup_<date>_<database_name>_<table_name>.sql# backup some specific datamysqldump --user=root --password <database_name><table_name> --where"last_name=VALLE order by first_name > backup_<date>.sql# dumping from one database to anothermysqldump --databases <database_name> | mysql -h<destination_host><database_name>restore amysqldump# all databasesmysql --user=root --password < backup.sql# specific databasemysql --user=<user> --password <database_name><backup_<dataabse_name>.sql
  25. 25. SELECT INTO OUTFILEThe SELECT INTO OUTFILE SQL statement is actually a variant of the SELECT query. It is used when youwant to direct query output to a text file. This file can then be opened by a spreadsheet application, or imported intoanother database like Microsoft Access, Oracle, or any other software that supports delimitation.ExSELECT id, data INTO @x, @y FROM test.t1 LIMIT 1;LOAD DATA INFILEThe LOAD DATA INFILE statement reads rows from a text file into a table at a very high speed. The file namemust be given as a literal string.ExLOAD DATA INFILE data.txt INTO TABLE db2.my_table;mysqlhotcopymysqlhotcopy is a Perl script that was originally written and contributed by Tim Bunce. It uses FLUSHTABLES, LOCK TABLES, and cp or scp to make a database backup. It is a fast way to make a backup of thedatabase or single tables, but it can be run only on the same machine where the database directories arelocated. mysqlhotcopy works only for backing up MyISAM and ARCHIVE tables.Ex#backup a databasemysqlhotcopy <database_name> /backups# backup multiple databasesmysqlhotcopy <database_name> accounts /backups#backup a database to to another servermysqlhotcopy --method=scp <database_name> username@backup.server:/backup# use pattern match to backup databases and tablesmysqlhotcopy <database_name>./^employees/ /backup10 Ways to Automatically & Manually Backup MySQL Database