Answers.Body: <p>If your plugin overrides <code>onBeforeUninstall()</code>, then you could call <code>craft()-&gt;db-&gt;backup();</code></p>
  
  <p>But that will backup the whole database via PHP code, which might not be what you want.</p>
  
  <p>Since you're in a plugin, I'd probably make sure <a href="https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html" rel="nofollow noreferrer">mysqldump</a> is available to PHP via the command line and use that to execute it and that way you can also specify the exact tables you'd like to dump.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-24T18:22:57.160Z
Answers.LastActivityDate: 2017-03-24T18:22:57.160Z
Answers.UserDisplayName: Brad Bell
Body: <p>When you delete/remove a plugin all the database tables related to it will be deleted and I would like to know if there is a way to keep them or store them into a file and force the user to download it.</p>
  
  <p>I made some kind of forum and saved many relevant information in a custom table but if someone deletes/removes the plugin accidentally or to include another row nothing will work anymore. </p>
  
  <p>Is there I function I can use in <code>public function onBeforeUninstall()</code> to save my table?</p>
  
  <p>I know you can go to phpmyadmin/adminer an use the export function by hand but we are all people and we can forget that.</p>
  
  <p>Thank you very much</p>
  
Tags: <plugin-development><php>
Title: Export database table as .sql before deleting/removing a plugin
ThreadText: # Export database table as .sql before deleting/removing a plugin
  
  (Asked by: username_0 on 2017-03-24)
  
  When you delete/remove a plugin all the database tables related to it will be deleted and I would like to know if there is a way to keep them or store them into a file and force the user to download it.
  
  I made some kind of forum and saved many relevant information in a custom table but if someone deletes/removes the plugin accidentally or to include another row nothing will work anymore.
  
  Is there I function I can use in `public function onBeforeUninstall()` to save my table?
  
  I know you can go to phpmyadmin/adminer an use the export function by hand but we are all people and we can forget that.
  
  Thank you very much
  
  **Tags:** plugin-development, php
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-03-24. Score: 1 | Accepted answer
  
  If your plugin overrides `onBeforeUninstall()`, then you could call `craft()->db->backup();`
  
  But that will backup the whole database via PHP code, which might not be what you want.
  
  Since you're in a plugin, I'd probably make sure [mysqldump](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html) is available to PHP via the command line and use that to execute it and that way you can also specify the exact tables you'd like to dump.
  
  
  ---
  
  