How to backup stored procedures in MySQL - mysql

I use mysqldump with MySQL 5.0 and I back it up every day, but do not understand the method that only stored procedure backs up.
How can I back it up?

I'm not sure whether you're asking to back up stored procedures as well as everything else, or just the stored procedures on their own...
Stored procedured in dump with everything else:
mysqldump -R <dbname> #or
mysqldump --routines <dbname>
Just the stored procedures:
mysqldump -n -t -d -R <dbname> #or
mysqldump --no-create-db --no-create-info --no-data --routines <dbname>
Does that help?

You can also put routines=true in the [mysqldump] section of your my.cnf file (you may have to add this section as it is not usually present in a virgin my.cnf file) to include routines in a normal dump.

mysqldump - u dbusername (ex: -uroot) -ppassword (ex:-pmysql#dbpas) --routines <dbname>
use the username and password could be more helpful.

Related

Perform backup with MySql-Commandlineclient

how to take backup of all table and procedure of project in mysql with the help of command line client
From OS shell, and not from inside MySQL shell
mysqldump --routines <db name> -u<username> -p > backup.sql
If you want to save all databases:
mysqldump --routes --all-databases -u<username> -p > backup.sql
Just as a side note, for security reasons avoid storing plain text password in DB tables, and avoid printing your actual database online ;)

mysqldump in sh script not working

I've got a sh script to backup a database-server.
#!/bin/bash
mysqldump -u <username> -p<password> --all-databases --single-transaction --opt > /home/backup/h_157_2-1.sql
rsync -zrp --partial /home/backup/h_157_2-1.sql root#<server-ip>:/home/backup/H_157_2/
When I execute those two command on their own in the command line, they work as expected and I get a .sql file with content. But when I execute the script the file only contains this:
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump --help
I already tried to change up the order of the options or leave out the --opt but the result was still the same. So what could cause the command to not work in the script?
I couldn't find a way to resolve the initial problem. Even with the help from Elzo Valugi in chat the result was still the same: Excecuted on command line the mysqldump worked finde. Excecuted in the script I got the same msg all the time. (see initial question).
To resolve it I built a workaround to dump every database on it's own.
#!/bin/sh
for dir in /var/lib/mysql/*/;
do
dir=${dir%*/}
mysqldump -u <user> -p<password> ${dir##*/} --single-transaction --opt > /home/backup/h_157_2_${dir##*/}.sql
rsync -zrp --partial /home/backup/h_157_2_${dir##*/}.sql root#<server-ip>:/home/backup/H_157_2/
done;
With this I loop through all the directorys in the mysql storage directory, cut them down to the dir-name and use that for the mysqldump. This puts every database in its own file. I think this could be resolved to combine them all to one, but for my needs I'm fine with different files.

How to make dump of all MySQL databases besides two

this is probably massively simple, however I will be doing this for a live server and don't want to mess it up.
Can someone please let me know how I can do a mysqldump of all databases, procedures, triggers etc except the mysql and performance_schema databases?
Yes, you can dump several schemas at the same time :
mysqldump --user=[USER] --password=[PASS] --host=[HOST] --databases mydb1 mydb2 mydb3 [...] --routines > dumpfile.sql
OR
mysqldump --user=[USER] --password=[p --host=[HOST] --all-databases --routines > dumpfile.sql
concerning the last command, if you don't want to dump performance_schema (EDIT: as mentioned by #Barranka, by default mysqldump won't dump it), mysql, phpMyAdmin schema, etc. you just need to ensure that [USER] can't access them.
As stated in the reference manual:
mysqldump does not dump the INFORMATION_SCHEMA or performance_schema database by default. To dump either of these, name it explicitly on the command line and also use the --skip-lock-tables option. You can also name them with the --databases option.
So that takes care of your concern about dumping those databases.
Now, to dump all databases, I think you should do something like this:
mysqldump -h Host -u User -pPassword -A -R > very_big_dump.sql
To test it without dumping all data, you can add the -d flag to dump only database, table (and routine) definitions with no data.
As mentioned by Basile in his answer, the easiest way to ommit dumping the mysql database is to invoke mysqldump with a user that does not have access to it. So the punch line is: use or create a user that has access only to the databases you mean to dump.
There's no option in mysqldump that you could use to filter the databases list, but you can run two commands:
# DATABASES=$(mysql -N -B -e "SHOW DATABASES" | grep -Ev '(mysql|performance_schema)')
# mysqldump -B $DATABASES

mysqldump wont dump my data

here is the command I'm using:
mysqldump.exe -u root -d capstone -verbse --skip-quote-names > capstone.sql
and the output I get
mysqldump: Warning: Can't set SQL_QUOTE_SHOW_CREATE option ()
-- Skipping dump data for table 'users', --no-data was used
any ideas? if I dump to XML it works but the place I'm importing it to doesn't handle XML and my data ruins the CSV output somehow too.
the -d option is alias of --no-data, see https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_no-data
perhaps you intended to state "use database capstone" but in that case it wouldn't be -d capstone, the database name doesn't need any switch/option, just put it in there
shell> mysqldump [options] db_name [tbl_name ...]
shell> mysqldump [options] --databases db_name ...
shell> mysqldump [options] --all-databases
https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#mysqldump-syntax
I think you mean to use either -B / --databases (which includes allows you to indicate multiple databases to dump instead of a database and tables) or no such argument at all. I think you also mistyped --verbose.
Note that if you include --databases a CREATE DATABASE statement is also included. This could be important depending up on how you intend to use the data.

Dump only the data with mysqldump without any table information?

I am looking for the syntax for dumping all data in my mysql database. I don't want any table information.
mysqldump --no-create-info ...
Also you may use:
--skip-triggers: if you are using triggers
--no-create-db: if you are using --databases ... option
--compact: if you want to get rid of extra comments
This should work:
# To export to file (data only)
mysqldump -u [user] -p[pass] --no-create-info mydb > mydb.sql
# To export to file (structure only)
mysqldump -u [user] -p[pass] --no-data mydb > mydb.sql
# To import to database
mysql -u [user] -p[pass] mydb < mydb.sql
NOTE: there's no space between -p & [pass]
If you just want the INSERT queries, use the following:
mysqldump --skip-triggers --compact --no-create-info
>> man -k mysqldump [enter in the terminal]
you will find the below explanation
--no-create-info, -t
Do not write CREATE TABLE statements that re-create each dumped table.
Note This option does not not exclude statements creating log file
groups or tablespaces from mysqldump output; however, you can use the
--no-tablespaces option for this purpose.
--no-data, -d
Do not write any table row information (that is, do not dump table
contents). This is useful if you want to dump only the CREATE TABLE
statement for the table (for example, to create an empty copy of the
table by loading the dump file).
# To export to file (data only)
mysqldump -t -u [user] -p[pass] -t mydb > mydb_data.sql
# To export to file (structure only)
mysqldump -d -u [user] -p[pass] -d mydb > mydb_structure.sql
Best to dump to a compressed file
mysqldump --no-create-info -u username -hhostname -p dbname | gzip > /backupsql.gz
and to restore using pv apt-get install pv to monitor progress
pv backupsql.gz | gunzip | mysql -uusername -hhostip -p dbname
Would suggest using the following snippet. Works fine even with huge tables (otherwise you'd open dump in editor and strip unneeded stuff, right? ;)
mysqldump --no-create-info --skip-triggers --extended-insert --lock-tables --quick DB TABLE > dump.sql
At least mysql 5.x required, but who runs old stuff nowadays.. :)
Just dump the data in delimited-text format.
Try to dump to a delimited file.
mysqldump -u [username] -p -t -T/path/to/directory [database] --fields-enclosed-by=\" --fields-terminated-by=,
When attempting to export data using the accepted answer I got an error:
ERROR 1235 (42000) at line 3367: This version of MySQL doesn't yet support 'multiple triggers with the same action time and event for one table'
As mentioned above:
mysqldump --no-create-info
Will export the data but it will also export the create trigger statements. If like me your outputting database structure (which also includes triggers) with one command and then using the above command to get the data you should also use '--skip-triggers'.
So if you want JUST the data:
mysqldump --no-create-info --skip-triggers