I'd like to avoid mysqldump since that outputs in a form that is only convenient for mysql to read. CSV seems more universal (one file per table is fine). But if there are advantages to mysqldump, I'm all ears. Also, I'd like something I can run from the command line (linux). If that's a mysql script, pointers to how to make such a thing would be helpful.
Join Stack Overflow to learn, share knowledge, and build your career.
|
|
If you can cope with table-at-a-time, and your data is not binary, use the
Alternatively, if you've got direct access to the server's file system, use
|
|||||||||||||||||||||
|
|
In MySQL itself, you can specify CSV output like:
From http://www.tech-recipes.com/rx/1475/save-mysql-query-results-into-a-text-or-csv-file/ |
|||||||||
|
|
You can dump a whole database in one go with mysqldump's --tab option. You supply a directory path and it creates one .sql file with the CREATE TABLE DROP IF EXISTS syntax and a .txt file with the contents, tab separated. To create comma separated files you could use the following:
That path needs to be writable by both the mysql user and the user running the command, so for simplicity I recommend |
|||||
|
|
The select into outfile option wouldn't work for me but the below roundabout way of piping tab-delimited file through SED did:
|
|||||
|
|
If you really need a "Backup" then you also need database schema, like table definitions, view definitions, store procedures and so on. A backup of a database isn't just the data. The value of the mysqldump format for backup is specifically that it is very EASY to use it to restore mysql databases. A backup that isn't easily restored is far less useful. If you are looking for a method to reliably backup mysql data to so you can restore to a mysql server then I think you should stick with the mysqldump tool. Mysql is free and runs on many different platforms. Setting up a new mysql server that I can restore to is simple. I am not at all worried about not being able to setup mysql so I can do a restore. I would be far more worried about a custom backup/restore based on a fragile format like csv/tsv failing. Are you sure that all your quotes, commas, or tabs that are in your data would get escaped correctly and then parsed correctly by your restore tool? If you are looking for a method to extract the data then see several in the other answers. |
|||||||||||||
|
|
Here is the simplest command for it
If there is a comma in the column value then we can generate .tsv instead of .csv with the following command
|
||||
|
|
|
Check out mk-parallel-dump which is part of the ever-useful maatkit suite of tools. This can dump comma-separated files with the --csv option. This can do your whole db without specifying individual tables, and you can specify groups of tables in a backupset table. Note that it also dumps table definitions, views and triggers into separate files. In addition providing a complete backup in a more universally accessible form, it also immediately restorable with mk-parallel-restore |
||||
|
You can use below script to get the output to csv files. One file per table with headers.
user is your user name, password is the password if you don't want to keep typing the password for each table and mydb is the database name. Explanation of the script: The first expression in sed, will replace the tabs with "," so you have fields enclosed in double quotes and separated by commas. The second one insert double quote in the beginning and the third one insert double quote at the end. And the final one takes care of the \n. |
||||
|
|
|
Two line PowerShell answer:
Boom-bata-boom
|
|||
|
|