Backing Up and Restring Yur MySQL Database Frm the cmmand prmpt In this article, it will utline tw easy ways f backing up and restring databases in MySQL. The easiest way t backup yur database wuld be t telnet t the yur database server machine and use the mysqldump cmmand t dump yur whle database t a backup file. If yu d nt have telnet r shell access t yur server, dn't wrry abut it; I shall utline a methd f ding s using the PHPMyAdmin web interface, which yu can setup n any web server which executes PHP scripts. Playing with mysqldump If yu have either a shell r telnet access t yur database server, yu can backup the database using mysqldump. By default, the utput f the cmmand will dump the cntents f the database in SQL statements t yur cnsle. This utput can then be piped r redirected t any lcatin yu want. If yu plan t backup yur database, yu can pipe the utput t a sql file, which will cntain the SQL statements t recreate and ppulate the database tables when yu wish t restre yur database. There are mre adventurus ways t use the utput f mysqldump. A Simple Database Backup: Yu can use mysqldump t create a simple backup f yur database using the fllwing syntax. mysqldump -u [username] -p [passwrd] [databasename] > [backupfile.sql] [username] - this is yur database username [passwrd] - this is the passwrd fr yur database [databasename] - the name f yur database [backupfile.sql] - the file t which the backup shuld be written. The resultant dump file will cntain all the SQL statements needed t create the table and ppulate the table in a new database server. T backup yur database 'Custmers' with the username 'sadmin' and passwrd 'pass21' t a file custback.sql, yu wuld issue the cmmand: mysqldump -u sadmin -p pass21 Custmers > custback.sql Yu can als ask mysqldump t add a drp table cmmand befre every create cmmand by using the ptin --add-drp-table. This ptin is useful if yu wuld like t create a backup file which can rewrite an existing database withut having t delete the lder database manually first. mysqldump --add-drp-table -u sadmin -p pass21 Custmers > custback.sql Backing up nly specified tables If yu'd like restrict the backup t nly certain tables f yur database, yu can als specify the tables yu want t backup. Let's say that yu want t backup nly custmer_master & custmer_details frm the Custmers database, yu d that by issuing
mysqldump --add-drp-table -u sadmin -p pass21 Custmers custmer_master custmer_details> custback.sql S the syntax fr the cmmand t issue is: mysqldump -u [username] -p [passwrd] [databasename] [table1 table2...] [tables] - This is a list f tables t backup. Each table is separated by a space. mysqldump -u rt -p pass21 --databases Custmers Orders Cmments > multibackup.sql This is kay if yu have a small set f databases yu want t backup. Nw hw abut backing up all the databases in the server? That's an easy ne, just use the --all-databases parameter t backup all the databases in the server in ne step. mysqldump --all-databases> alldatabases.sql Backing up nly the Database Structure Mst develpers need t backup nly the database structure t while they are develping their applicatins. Yu can backup nly the database structure by telling mysqldump nt t back up the data. Yu can d this by using the --n-data parameter when yu call mysqldump. mysqldump --n-data --databases Custmers Orders Cmments > structurebackup.sql Cmpressing yur Backup file n the Fly Backups f databases take up a lt f space. Yu can cmpress the utput f mysqldump t save valuable space while yu're backing up yur databases. Since mysqldump sends its utput t the cnsle, we can pipe the utput thrugh gzip r bzip2 and send the cmpressed dump t the backup file. Here's hw yu wuld d that with bzip2 and gzip respectively. mysqldump --all-databases bzip2 -c >databasebackup.sql.bz2 mysqldump --all-databases gzip >databasebackup.sql.gz A Shell Script fr Autmating Backups? Yu can autmate the backup prcess by making a small shell script which will create a daily backup file. Hw d yu get crn t back up yur database withut verwriting the lder backup? Yu can use a tiny shell script t add the date t yur backup file. An example f a shell script yu culd use is shwn belw. #!/bin/sh date=`date -I` mysqldump --all-databases gzip > /var/backup/backup-$date.sql.gz
Restre using mysql If yu have t re-build yur database frm scratch, yu can easily restre the mysqldump file by using the mysql cmmand. This methd is usually used t recreate r rebuild the database frm scratch. Here's hw yu wuld restre yur custback.sql file t the Custmers database. mysql -u sadmin -p pass21 Custmers < custback.sql Easy isn't it? Here's the general frmat yu wuld fllw: mysql -u [username] -p [passwrd] [database_t_restre] < [backupfile] Nw hw abut thse zipped files? Yu can restre yur zipped backup files by first uncmpressing its cntents and then sending it t mysql. gunzip < custback.sql.sql.gz mysql -u sadmin -p pass21 Custmers Yu can als cmbine tw r mre backup files t restre at the same time, using the cat cmmand. Here's hw yu can d that. cat backup1.sql backup.sql mysql -u sadmin -p pass21 Mving Data Directly Between Databases Hw wuld yu like t replicate yur present database t a new lcatin? When yu are shifting web hsts r database servers, yu can directly cpy data t the new database withut having t create a database backup n yur machine and restring the same n the new server. mysql allws yu t cnnect t a remte database server t run sql cmmands. Using this feature, we can pipe the utput frm mysqldump and ask mysql t cnnect t the remte database server t ppulate the new database. Let's say we want t recreate the Custmers database n a new database server lcated at 202.32.12.32, we can run the fllwing set f cmmands t replicate the present database at the new server. mysqldump -u sadmin -p pass21 Custmers mysql --hst=202.32.12.32 -C Custmers
Backing Up and Restring Yur MySQL Database - Backing Up and Restring Yur Database with PHPMyAdmin Yu can backup yur database using PHPMyAdmin in just a few muse clicks. First head ver t yur database by clicking the database name in the list n the left f the screen. Once yu get t yur database details, yu shuld get a menu header fr yur database which lks like s: Click n Exprt. This will get yu t a screen with the fllwing ptins. Frm here it's just a matter f clicking the right ptins and the 'G' Buttn t get yur database dump. T save the backup file n yur cmputer, click n the Save as file ptin and the crrespnding cmpressin ptin t get the backup t dwnlad t yur system. Restring yur Database via PHPMyAdmin Restring yur database is as easy as backing it up. If yu wuld like t rewrite the backup ver an existing database, click n the database name in the database list n the left, click all the check bxes next t the table names and select Drp in the With selected: drp dwn bx.
This will drp all existing table in the database. Then head ver t the tp menu bar and click n SQL. This will bring up a windw where yu can either type in SQL cmmands, r uplad yur SQL file. The windw shuld lk like this: Click n the Brwse buttn, and select the sql backup file yu wish t uplad t the server. Once yu've selected the file, click n 'G'. This will uplad the backup file t the server and execute the SQL cmmands in the backup and restre yur database. Nw that yu knw hw easy it is t backup and restre yur database, spend a few minutes everyday t backup yur imprtant data. Yu can even autmate the backup prcess by using a task scheduler like crn.