Difference between revisions of "Database Backup and Recovery"
From AgileApps Support Wiki
imported>Aeric |
imported>Aeric |
||
Line 1: | Line 1: | ||
<includeonly>=== Database Backup and Recovery ===</includeonly> | <includeonly>=== Database Backup and Recovery ===</includeonly> | ||
MySQL can be backed up using the <tt>mysqldump</tt> command - http://dev.mysql.com/doc/refman/ | MySQL can be backed up using the <tt>mysqldump</tt> command - http://dev.mysql.com/doc/refman/{{MySqlVersion}}/en/mysqldump.html | ||
{{Tip| The replicated database server should be used for backups.<br/> To set up for it, see [[Configuring MySQL to Run on a Separate Server]]}} | {{Tip| The replicated database server should be used for backups.<br/> To set up for it, see [[Configuring MySQL to Run on a Separate Server]]}} | ||
Line 54: | Line 54: | ||
=====Back Up Tenant Data===== | =====Back Up Tenant Data===== | ||
Letâs say you want to back up data for a customer (tenant) whose account number is â1234â. Here is the process: | |||
 | |||
<ol> | |||
<li>Dump the customer database.<pre> | |||
# mysqldump -u âp -R db1234 > db1234.sql | # mysqldump -u âp -R db1234 > db1234.sql | ||
</pre>< | </pre> | ||
Dump records related to this customer: | <li>Dump records related to this customer:<pre> | ||
# mysqldump -u -p --complete-insert relationals NETWORK_CUSTOMER \ | # mysqldump -u -p --complete-insert relationals NETWORK_CUSTOMER \ | ||
   -t -w"id=1234" > network_customer.sql |    -t -w"id=1234" > network_customer.sql | ||
Line 74: | Line 75: | ||
# mysqldump -u -p --complete-insert db892085391 NETWORK_8c5b349851c041a28d19277a12dee8f0 \ | # mysqldump -u -p --complete-insert db892085391 NETWORK_8c5b349851c041a28d19277a12dee8f0 \ | ||
   -t -w"number=1234" > network_tenants.sql |    -t -w"number=1234" > network_tenants.sql | ||
</pre></ | </pre> | ||
<li>If the tenant/customer is an MSP, dump their service configuration records as well:<pre> | |||
# mysqldump -u -p --complete-insert db892085391 NETWORK_ISV_SETTINGS -t -w"id=1234" \ | |||
  > network_isv_settings.sql | |||
</pre> | |||
</ol> | |||
=====Restore Tenant Data===== | =====Restore Tenant Data===== | ||
Line 80: | Line 86: | ||
Restore the data for customer #1234 from the dump files created above: | Restore the data for customer #1234 from the dump files created above: | ||
<ol> | <ol> | ||
<li>Create the database for the tenant if it | <li>Create the database for the tenant if it doesnât exist.<pre> | ||
mysql> create database db1234; | mysql> create database db1234; | ||
</pre> | </pre> | ||
Line 96: | Line 102: | ||
# mysql âuroot âp db892085391 < network_tenants.sql | # mysql âuroot âp db892085391 < network_tenants.sql | ||
</pre> | </pre> | ||
(If you | (If you havenât made changes to the <tt>relationals</tt> and <tt>db892085391</tt> databases, you can safely choose not to restore the records from the related tables.) | ||
</li> | </li> | ||
<li>Set the admin user ID to "3" (the system user) in the tenant records: Â | <li>Set the admin user ID to "3" (the system user) in the tenant records: Â | ||
Line 103: | Line 109: | ||
UPDATE db892085391.NETWORK_8c5b349851c041a28d19277a12dee8f0 SET owner_id = '3' Â | UPDATE db892085391.NETWORK_8c5b349851c041a28d19277a12dee8f0 SET owner_id = '3' Â | ||
   WHERE number = '1234'; |    WHERE number = '1234'; | ||
</pre> | |||
</li> | |||
<li>If the tenant/customer is an MSP, restore their service configuration records, as well: | |||
<pre> | |||
# mysql âuroot âp db892085391 < network_isv_settings.sql | |||
</pre> | </pre> | ||
</li> | </li> | ||
Line 109: | Line 120: | ||
====Learn More==== | ====Learn More==== | ||
For more detailed information on database backup and recovery in MySQL, see: | For more detailed information on database backup and recovery in MySQL, see: | ||
:* http://dev.mysql.com/doc/refman/ | :* http://dev.mysql.com/doc/refman/{{MySqlVersion}}/en/mysqldump.html | ||
:* [http://www.mydigitallife.info/2007/07/21/how-to-backup-and-restore-export-and-import-mysql-databases-tutorial http://www.mydigitallife.info/2007/07/21/]<br/>[http://www.mydigitallife.info/2007/07/21/how-to-backup-and-restore-export-and-import-mysql-databases-tutorial how-to-backup-and-restore-export-and-import-mysql-databases-tutorial] | :* [http://www.mydigitallife.info/2007/07/21/how-to-backup-and-restore-export-and-import-mysql-databases-tutorial http://www.mydigitallife.info/2007/07/21/]<br/>[http://www.mydigitallife.info/2007/07/21/how-to-backup-and-restore-export-and-import-mysql-databases-tutorial how-to-backup-and-restore-export-and-import-mysql-databases-tutorial] | ||
<noinclude> | <noinclude> |
Revision as of 02:21, 19 March 2014
MySQL can be backed up using the mysqldump command - http://dev.mysql.com/doc/refman/5.5/en/mysqldump.html
Tip: The replicated database server should be used for backups.
To set up for it, see Configuring MySQL to Run on a Separate Server
Standard Database Backup
- Dump the database
mysqldump âuroot âpxxx âall-databases âquick âroutines âresult-file=dumpfile.sql
- Dump the database at regular intervals, using a Linux cron job
-
- Put the mysqldump command in a shell script - say, xyz.sh
- Setup cron job e.g. every day at 6 pm
0 18 * * * /yourscriptlocation/xyz.sh > /somedirectory/xyz.out 2>&1
- Use tar to compress the resulting dump file, to save space.
- Maintain daily backups for ten days or so, to reclaim the space they use.
- Maintain monthly backup. Save the dump of last day of the month, for example, and retain the dump file for a year or so.
- Store long-term backups offsite.
Standard Database Restore
- Restoring all data from a dump file
- Drop all the databases.
- Restart mysqld
- Start the restore process and run in background:
mysql âuroot âpxxxx < dumpfile.sql > dumpfile.out 2>&1 &
- Restoring a single database or table from a dump file
- Drop the old database/table:
mysql âuroot âpxxxx $database < dumpfile.sql > dumpfile.out 2>&1 &
Backup and Restore for a Single Tenant
The items to include are:
- The Tenant's database
- Records related to this Tenant from relationals.NETWORK_DATABASES, relationals.NETWORK_CUSTOMER, and relationals.NETWORK_USER_DIRECTORY
- Records related to this Tenant from db892085391
- Tenant's documents from documents/ and public_documents/ folders in the file system.
Back Up Tenant Data
Letâs say you want to back up data for a customer (tenant) whose account number is â1234â. Here is the process:
- Dump the customer database.
# mysqldump -u âp -R db1234 > db1234.sql
- Dump records related to this customer:
# mysqldump -u -p --complete-insert relationals NETWORK_CUSTOMER \ -t -w"id=1234" > network_customer.sql # mysqldump -u -p --complete-insert relationals NETWORK_USER_DIRECTORY \ -t -w"customer_id=1234" > network_userdir.sql # mysqldump -u -p --complete-insert relationals NETWORK_DATABASES \ -t -w"id=1234" > network_databases.sql # mysqldump -u -p --complete-insert db892085391 NETWORK_ACCOUNTS -t -w"number=1234" \ > network_acc.sql # mysqldump -u -p --complete-insert db892085391 NETWORK_8c5b349851c041a28d19277a12dee8f0 \ -t -w"number=1234" > network_tenants.sql
- If the tenant/customer is an MSP, dump their service configuration records as well:
# mysqldump -u -p --complete-insert db892085391 NETWORK_ISV_SETTINGS -t -w"id=1234" \ > network_isv_settings.sql
Restore Tenant Data
Restore the data for customer #1234 from the dump files created above:
- Create the database for the tenant if it doesnât exist.
mysql> create database db1234;
- Restore the data.
# mysql âuroot âp db1234 < db1234.sql
- Restore customer records from the dumped tables:
# mysql âuroot âp relationals < network_customer.sql # mysql âuroot âp relationals < network_userdir.sql # mysql âuroot âp relationals < network_databases.sql # mysql âuroot âp db892085391 < network_acc.sql # mysql âuroot âp db892085391 < network_tenants.sql
(If you havenât made changes to the relationals and db892085391 databases, you can safely choose not to restore the records from the related tables.)
- Set the admin user ID to "3" (the system user) in the tenant records:
UPDATE db892085391.NETWORK_ACCOUNTS SET owner_id = '3' WHERE number = '1234'; UPDATE db892085391.NETWORK_8c5b349851c041a28d19277a12dee8f0 SET owner_id = '3' WHERE number = '1234';
- If the tenant/customer is an MSP, restore their service configuration records, as well:
# mysql âuroot âp db892085391 < network_isv_settings.sql
Learn More
For more detailed information on database backup and recovery in MySQL, see: