<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>LifeUML &#187; Restore</title>
	<atom:link href="http://lifeuml.com/tag/restore/feed/" rel="self" type="application/rss+xml" />
	<link>http://lifeuml.com</link>
	<description>from actor, to actors..</description>
	<lastBuildDate>Fri, 28 Aug 2009 10:17:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Backing Up and Restoring MySQL Database with MySQL Commands</title>
		<link>http://lifeuml.com/backing-up-and-restoring-your-mysql-database/</link>
		<comments>http://lifeuml.com/backing-up-and-restoring-your-mysql-database/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 06:44:12 +0000</pubDate>
		<dc:creator>TuyenT</dc:creator>
				<category><![CDATA[Administration]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Back up]]></category>
		<category><![CDATA[Command]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Restore]]></category>

		<guid isPermaLink="false">http://lifeuml.com/?p=67</guid>
		<description><![CDATA[Original post from: devshed.com
Do you need to change your web host or switch your database server? This is probably the only time when you really think of backing up your MySQL data. If you&#8217;ve got a website with a database or your custom database running for your applications, it is imperative that you make regular [...]]]></description>
			<content:encoded><![CDATA[<p>Original post from: <a href="http://www.devshed.com/c/a/MySQL/Backing-up-and-restoring-your-MySQL-Database/" target="_blank">devshed.com</a></p>
<p><strong>Do you need to change your web host or switch your database server? This is probably the only time when you really think of backing up your MySQL data. If you&#8217;ve got a website with a database or your custom database running for your applications, it is imperative that you make regular backups of the database.</strong></p>
<p>The easiest way to backup your database would be to telnet to the your database server machine and use the mysqldump command to dump your whole database to a backup file.</p>
<p><strong>Playing with mysqldump</strong></p>
<p>If you have either a shell or telnet access to your database server, you can backup the database using mysqldump. By default, the output of the command will dump the contents of the database in SQL statements to your console. This output can then be piped or redirected to any location you want. If you plan to backup your database, you can pipe the output to a sql file, which will contain the SQL statements to recreate and populate the database tables when you wish to restore your database. There are more adventurous ways to use the output of mysqldump.</p>
<p><strong>A Simple Database Backup:</strong></p>
<p>You can use mysqldump to create a simple backup of your database using the following syntax.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysqldump <span style="color: #660033;">-u</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>username<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #660033;">-p</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>password<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>databasename<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>backupfile.sql<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<ul>
<li>[username] &#8211; this is your database username</li>
<li>[password] &#8211; this is the password for your database</li>
<li>[databasename] &#8211; the name of your database</li>
<li>[backupfile.sql] &#8211; the file to which the backup should be written.</li>
</ul>
<p>The resultant dump file will contain all the SQL statements needed to create the table and populate the table in a new database server. To backup your database &#8216;Customers&#8217; with the username &#8217;sadmin&#8217; and password &#8216;pass21&#8242; to a file custback.sql, you would issue the command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysqldump <span style="color: #660033;">-u</span> sadmin <span style="color: #660033;">-p</span> pass21 Customers <span style="color: #000000; font-weight: bold;">&gt;</span> custback.sql</pre></div></div>

<p>You can also ask mysqldump to add a drop table command before every create command by using the option &#8211;add-drop-table. This option is useful if you would like to create a backup file which can rewrite an existing database without having to delete the older database manually first.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysqldump <span style="color: #660033;">--add-drop-table</span> <span style="color: #660033;">-u</span> sadmin <span style="color: #660033;">-p</span> pass21 Customers <span style="color: #000000; font-weight: bold;">&gt;</span> custback.sql</pre></div></div>

<p><strong>Backing up only specified tables</strong></p>
<p>If you&#8217;d like restrict the backup to only certain tables of your database, you can also specify the tables you want to backup. Let&#8217;s say that you want to backup only customer_master &amp; customer_details from the Customers database, you do that by issuing</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysqldump <span style="color: #660033;">--add-drop-table</span> <span style="color: #660033;">-u</span> sadmin <span style="color: #660033;">-p</span> pass21 Customers customer_master customer_details <span style="color: #000000; font-weight: bold;">&gt;</span> custback.sql</pre></div></div>

<p>So the syntax for the command to issue is:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysqldump <span style="color: #660033;">-u</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>username<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #660033;">-p</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>password<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>databasename<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>table1 table2 ....<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<ul>
<li>[tables] &#8211; This is a list of tables to backup. Each table is separated by a space.</li>
</ul>
<p><span id="more-67"></span></p>
<p><strong>What about Multiple Databases? </strong></p>
<p>If you are a database administrator who has to look after multiple databases, you&#8217;ll need to back up more than one database at a time. Here&#8217;s how you can backup multiple databases in one shot.</p>
<p>If you want to specify the databases to backup, you can use the &#8211;databases parameter followed by the list of databases you would like to backup. Each database name has to be separated by at least one space when you type in the command. So if you have to backup 3 databases, let say Customers, Orders and Comments, you can issue the following command to back them up. Make sure the username you specify has permissions to access the databases you would like to backup.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysqldump <span style="color: #660033;">-u</span> root <span style="color: #660033;">-p</span> pass21 <span style="color: #660033;">--databases</span> Customers Orders Comments <span style="color: #000000; font-weight: bold;">&gt;</span> multibackup.sql</pre></div></div>

<p>This is okay if you have a small set of databases you want to backup. Now how about backing up all the databases in the server? That&#8217;s an easy one, just use the &#8211;all-databases parameter to backup all the databases in the server in one step.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysqldump <span style="color: #660033;">--all-databases</span> <span style="color: #000000; font-weight: bold;">&gt;</span> alldatabases.sql</pre></div></div>

<p><strong>Backing up only the Database Structure</strong></p>
<p>Most developers need to backup only the database structure to while they are developing their applications. You can backup only the database structure by telling mysqldump not to back up the data. You can do this by using the &#8211;no-data parameter when you call mysqldump.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysqldump <span style="color: #660033;">--no-data</span> <span style="color: #660033;">--databases</span> Customers Orders Comments <span style="color: #000000; font-weight: bold;">&gt;</span> structurebackup.sql</pre></div></div>

<p><strong>Compressing your Backup file on the Fly</strong></p>
<p>Backups of databases take up a lot of space. You can compress the output of mysqldump to save valuable space while you&#8217;re backing up your databases. Since mysqldump sends its output to the console, we can pipe the output through gzip or bzip2 and send the compressed dump to the backup file. Here&#8217;s how you would do that with bzip2 and gzip respectively.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysqldump <span style="color: #660033;">--all-databases</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">bzip2</span> <span style="color: #660033;">-c</span> <span style="color: #000000; font-weight: bold;">&gt;</span> databasebackup.sql.bz2
mysqldump <span style="color: #660033;">--all-databases</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">gzip</span> <span style="color: #000000; font-weight: bold;">&gt;</span> databasebackup.sql.gz</pre></div></div>

<p><strong>A Shell Script for Automating Backups?</strong></p>
<p>You can automate the backup process by making a small shell script which will create a daily backup file. How do you get cron to back up your database without overwriting the older backup? You can use a tiny shell script to add the date to your backup file. An example of a shell script you could use is shown below.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;">#!/bin/sh</span>
date<span style="color: pink;">=</span>`date <span style="color: pink;">-</span>I`
mysqldump <span style="color: pink;">--</span>all<span style="color: pink;">-</span>databases <span style="color: pink;">|</span> gzip <span style="color: pink;">&gt;</span> <span style="color: pink;">/</span>var<span style="color: pink;">/</span>backup<span style="color: pink;">/</span>backup<span style="color: pink;">-</span><span style="color: #800080;">$date</span>.sql.gz</pre></div></div>

<p><strong>Restore using mysql</strong></p>
<p>If you have to re-build your database from scratch, you can easily restore the mysqldump file by using the mysql command. This method is usually used to recreate or rebuild the database from scratch.</p>
<p>Here&#8217;s how you would restore your custback.sql file to the Customers database.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysql <span style="color: #660033;">-u</span> sadmin <span style="color: #660033;">-p</span> pass21 Customers <span style="color: #000000; font-weight: bold;">&lt;</span> custback.sql</pre></div></div>

<p>Easy isn&#8217;t it ? Here&#8217;s the general format you would follow:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysql <span style="color: #660033;">-u</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>username<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #660033;">-p</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>password<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>database_to_restore<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>backupfile<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p>Now how about those zipped files? You can restore your zipped backup files by first uncompressing its contents and then sending it to mysql.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">gunzip</span> <span style="color: #000000; font-weight: bold;">&lt;</span> custback.sql.sql.gz <span style="color: #000000; font-weight: bold;">|</span> mysql <span style="color: #660033;">-u</span> sadmin <span style="color: #660033;">-p</span> pass21 Customers</pre></div></div>

<p>You can also combine two or more backup files to restore at the same time, using the cat command. Here&#8217;s how you can do that.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cat</span> backup1.sql backup.sql <span style="color: #000000; font-weight: bold;">|</span> mysql <span style="color: #660033;">-u</span> sadmin <span style="color: #660033;">-p</span> pass21</pre></div></div>

<p><strong>Moving Data Directly Between Databases</strong></p>
<p>How would you like to replicate your present database to a new location? When you are shifting web hosts or database servers, you can directly copy data to the new database without having to create a database backup on your machine and restoring the same on the new server. mysql allows you to connect to a remote database server to run sql commands. Using this feature, we can pipe the output from mysqldump and ask mysql to connect to the remote database server to populate the new database. Let&#8217;s say we want to recreate the Customers database on a new database server located at 202.32.12.32, we can run the following set of commands to replicate the present database at the new server.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysqldump <span style="color: #660033;">-u</span> sadmin <span style="color: #660033;">-p</span> pass21 Customers <span style="color: #000000; font-weight: bold;">|</span> mysql <span style="color: #660033;">--host</span>=202.32.12.32 <span style="color: #660033;">-C</span> Customers</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://lifeuml.com/backing-up-and-restoring-your-mysql-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
