Posted By

terrencewood on 11/10/10


Tagged


Versions (?)

Drop mysql databases matching a pattern


 / Published in: Perl
 

  1. #!/usr/bin/perl
  2. $username = 'root';
  3. $password = 'root';
  4. $pattern = 'tmpdb';
  5. $filename = '/tmp/tmpdb.txt';
  6. $mysql = '/opt/local/bin/mysql5';
  7.  
  8.  
  9. $show_command = "$mysql -u$username -p$password -e 'show databases;' > $filename";
  10. system($show_command);
  11.  
  12. open(INPUT, $filename);
  13. $delete_command = "$mysql -u$username -p$password -e 'drop database ";
  14. @databases = <INPUT>;
  15. foreach (@databases){
  16. if (/$pattern/) {
  17. print($_);
  18. system($delete_command . $_ . "'");
  19. }
  20. }
  21.  
  22. print("Done\n");
  23.  
  24. system("rm $filename");

Report this snippet  

You need to login to post a comment.