Advertisement
Guest User

Untitled

a guest
Nov 29th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. With the default mysqldump format, each record dumped will generate an individual INSERT command in the dump file (i.e., the sql file), each on its own line. This is perfect for source control (e.g., svn, git, etc.) as it makes the diff and delta resolution much finer, and ultimately results in a more efficient source control process. However, for significantly sized tables, executing all those INSERT queries can potentially make restoration from the sql file prohibitively slow.
  2.  
  3. Using the --extended-insert option fixes the multiple INSERT problem by wrapping all the records into a single INSERT command on a single line in the dumped sql file. However, the source control process becomes very inefficient. The entire table contents is represented on a single line in the sql file, and if a single character changes anywhere in that table, source control will flag the entire line (i.e., the entire table) as the delta between versions. And, for large tables, this negates many of the benefits of using a formal source control system.
  4.  
  5. So ideally, for efficient database restoration, in the sql file, we want each table to be represented by a single INSERT. For an efficient source control process, in the sql file, we want each record in that INSERT command to reside on its own line.
  6.  
  7. My solution to this is the following back-up script:
  8.  
  9. #!/bin/bash
  10.  
  11. cd my_git_directory/
  12.  
  13. ARGS="--host=myhostname --user=myusername --password=mypassword --opt --skip-dump-date"
  14. /usr/bin/mysqldump $ARGS --database mydatabase | sed 's$VALUES ($VALUES\n($g' | sed 's$),($),\n($g' > mydatabase.sql
  15.  
  16. git fetch origin master
  17. git merge origin/master
  18. git add mydatabase.sql
  19. git commit -m "Daily backup."
  20. git push origin master
  21. The result is a sql file INSERT command format that looks like:
  22.  
  23. INSERT INTO `mytable` VALUES
  24. (r1c1value, r1c2value, r1c3value),
  25. (r2c1value, r2c2value, r2c3value),
  26. (r3c1value, r3c2value, r3c3value);
  27. Some notes:
  28.  
  29. password on the command line ... I know, not secure, different discussion.
  30. --opt: Among other things, turns on the --extended-insert option (i.e., one INSERT per table).
  31. --skip-dump-date: mysqldump normally puts a date/time stamp in the sql file when created. This can become annoying in source control when the only delta between versions is that date/time stamp. The OS and source control system will date/time stamp the file and version. Its not really needed in the sql file.
  32. The git commands are not central to the fundamental question (formatting the sql file), but shows how I get my sql file back into source control, something similar can be done with svn. When combining this sql file format with your source control of choice, you will find that when your users update their working copies, they only need to move the deltas (i.e., changed records) across the internet, and they can take advantage of diff utilities to easily see what records in the database have changed.
  33. If you're dumping a database that resides on a remote server, if possible, run this script on that server to avoid pushing the entire contents of the database across the network with each dump.
  34. If possible, establish a working source control repository for your sql files on the same server you are running this script from; check them into the repository from there. This will also help prevent having to push the entire database across the network with every dump.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement