Advertisement
Guest User

Untitled

a guest
May 1st, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. #!/bin/bash
  2. # USAGE: Will create a .tar.gz with CSVs of all tables in schema.
  3. # Configure below and run as root (i.e. the user mysql runs as)
  4. #
  5. # The script will (or should) SELECT * INTO OUTFILE your tables
  6. # and save them into csv files under /tmp dir first, then name them
  7. # like the tables and move them thogether into a directory. Then
  8. # it will tar everything together and chown you the tarball.
  9.  
  10. # Schema to export:
  11. DB=schemaname
  12.  
  13. # Directory to export files into: (before tar-ing)
  14. DIR=csvs-$DB-$(date +%Y-%m-%d-%H-%M-%S)/
  15.  
  16. # Final tarball's location:
  17. TARBALL=csvs-$DB-$(date +%Y-%m-%d-%H-%M-%S).tar.gz
  18.  
  19. # Config file for mysql password: (you would have to enter it many times)
  20. CONFIG=/home/burny/.my.cnf
  21. # This file looks sth like this (my.cnf-style):
  22. # > [mysql]
  23. # > user = sampleuser
  24. # > password = samplepasswd
  25.  
  26. # Set owner to this user: (lets you access the export)
  27. OWNER=bxt:bxtsgroup
  28.  
  29. # Name of file to save tables names to
  30. SCHEMAFILE=schema.txt
  31.  
  32. # ---------------------------------------------------------------------
  33. # Now following: the script. You shuldn't have to change
  34. # somthing after this line, but you might save you some
  35. # trouble if look at stuff before running it as root ;)
  36.  
  37. echo Saving to $DIR;
  38. mkdir "$DIR";
  39.  
  40. for table in $(mysql --defaults-extra-file=$CONFIG $DB -B -e "show tables;");
  41. do
  42. echo Processing $table
  43. mysql --defaults-extra-file=$CONFIG $DB -B -e "
  44. SELECT * INTO OUTFILE '/tmp/tabledump.csv'
  45. FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'
  46. LINES TERMINATED BY '\n'
  47. FROM \`$table\`;"
  48. mv /tmp/tabledump.csv "$DIR$table.csv"
  49. cat <(echo -n "$table ") <(mysql $DB -B -e "DESCRIBE $table;" | awk 'BEGIN {ORS=","; getline} { print $1}' ) <(echo) >> "$DIR$SCHEMAFILE"
  50. done
  51.  
  52. echo Zipping
  53.  
  54. tar -czvf "$TARBALL" "$DIR"
  55.  
  56. echo Changing permissions
  57. chown $OWNER "$TARBALL"
  58.  
  59. echo Removing uncompressed
  60. rm -R "$DIR"
  61.  
  62. echo -n "Done, filesize: "
  63. ls -l "$TARBALL" | awk '{print $5}'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement