mysqldump in sh script not working - mysql

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.

Related

Mysqldump rewriting input. Wordpress backup

I am running the most recent version of MariaDB on a CentOS7 machine. I have two databases I need to back up. One for Postfix/Dovecot and Another for Wordpress. I have read a few guides and most just say that I should be good running
sudo mysqldump -h 127.0.0.1 -u root -p somepassword --all_databases > /tmp/backup.sql
Produces the output
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS]
However, I suspect wordpress is complicating this process a bit because my CLI input is rewritten as
sudo mysqldump -h 127.0.0.1 -u root -p SomeIncorrectPassword find /var/www/somewordpressdir/wordpress/ -type d -exec chmod 755 {} \;! --all-databases > /tmp/backup.sql
I'm not sure why this is happening and I didn't come across anything similar. I am clearly missing something very basic here. What additional options do I need to provide?
I was reading an article that used the
--single_transaction
tag and this produced a different output
mysqldump: You can't use --single-transaction and --lock-all-tables at the same time.
it's not "--all_databases" with underscore but it's "--all-databases" with minus :-)

mysqldump will not export an individual database

I am trying to export a database using mysqldump from command line. I am using the following syntax:
mysqldump -u root -ppassword databasename > outputfile.sql
I've tried several variations on this, but I always end up with the following as the contents of the output file:
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS]
I can get mysqldump to export all of my databases if I exclude the database name, but it will not export just a single database.
Am I overlooking something here?
Troubleshooting from the comments above:
That is correct syntax. I'd guess that mysqldump is picking up some other options somewhere. Maybe it's a shell alias with an option like -A included in the alias definition? Try running \mysqldump ... to run it un-aliased.
Your reply:
#BillKarwin you were on the right track with -A. I tried mysqldump --print-defaults and apparently --all-databases is in the default arguments. I ran it with --no-defaults and it worked like a charm.
The problem is that --all-databases was configured as a default option. When you try using that option together with an argument specifying one database, it outputs the usage error you described.
http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html says that all-databases can be either a command-line flag, or an option in the config file.
I'd suggest looking in your /etc/my.cnf or $HOME/.my.cnf for the all-databases option. It can appear either in the [mysqldump] group or the [client] group.
How about (without the space between u and root)
mysqldump -uroot -ppassword databasename > outputfile.sql
Putting your root password in a command line is a really bad idea. At the very least, create a .my.cnf in your home directory, setting permissions to 600 (rw for you only) containing:
[mysqldump]
user=root
password=yourpassword
This will allow you to perform that particular command without a password. Since there's no particular reason your root user needs to be doing the dump, why not just create a user that can do this?
Presumably, you're doing the mysqldump to back things up. To make life even easier on you, set it in cron as in the example below which executes at midnight. Because of the presence of the .my.cnf file containing the password, it doesn't need a password in the command
0 0 * * * /usr/bin/mysqldump -u root -h localhost databasename > /home/someuser/outputfile.sql 2>&1

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.

Downloading MySQL dump from command line

I am moving away from Linode because I don't have the Linux sysadmin skills necessary; before I complete the transition to a more noob-friendly service, I need to download the contents of a MySQL database. Is there a way I can do this from the command line?
You can accomplish this using the mysqldump command-line function.
For example:
If it's an entire DB, then:
$ mysqldump -u [uname] -p db_name > db_backup.sql
If it's all DBs, then:
$ mysqldump -u [uname] -p --all-databases > all_db_backup.sql
If it's specific tables within a DB, then:
$ mysqldump -u [uname] -p db_name table1 table2 > table_backup.sql
You can even go as far as auto-compressing the output using gzip (if your DB is very big):
$ mysqldump -u [uname] -p db_name | gzip > db_backup.sql.gz
If you want to do this remotely and you have the access to the server in question, then the following would work (presuming the MySQL server is on port 3306):
$ mysqldump -P 3306 -h [ip_address] -u [uname] -p db_name > db_backup.sql
It should drop the .sql file in the folder you run the command-line from.
EDIT: Updated to avoid inclusion of passwords in CLI commands, use the -p option without the password. It will prompt you for it and not record it.
In latest versions of mysql, at least in mine, you cannot put your pass in the command directly.
You have to run:
mysqldump -u [uname] -p db_name > db_backup.sql
and then it will ask for the password.
If downloading from remote server, here is a simple example:
mysqldump -h my.address.amazonaws.com -u my_username -p db_name > /home/username/db_backup_name.sql
The -p indicates you will enter a password, it does not relate to the db_name. After entering the command you will be prompted for the password. Type it in and press enter.
On windows you need to specify the mysql bin where the mysqldump.exe resides.
cd C:\xampp\mysql\bin
mysqldump -u[username] -p[password] --all-databases > C:\localhost.sql
save this into a text file such as backup.cmd
Don't go inside mysql, just open Command prompt and directly type this:
mysqldump -u [uname] -p[pass] db_name > db_backup.sql
Just type mysqldump or mysqldump --help in your cmd will show how to use
Here is my cmd result
C:\Program Files\MySQL\MySQL Server 5.0\bin>mysqldump
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
Go to MySQL installation directory and open cmd from there.
Then execute the below command to get a backup of your database.
mysqldump -u root -p --add-drop-database --databases db> C:\db-dontdelete\db.sql
If you are running the MySQL other than default port:
mysqldump.exe -u username -p -P PORT_NO database > backup.sql
For those who wants to type password within the command line. It is possible but recommend to pass it inside quotes so that the special character won't cause any issue.
mysqldump -h'my.address.amazonaws.com' -u'my_username' -p'password' db_name > /path/backupname.sql
If you have the database named archiedb, use this:
mysql -p <password for the database> --databases archiedb > /home/database_backup.sql
Assuming this is Linux, choose where the backup file will be saved.
For some versions of MySQL try.
sudo mysqldump [database name] > db_backup.sql
mysqldump is another program (.exe file) in the MySQL directory
Program Files\MySQL\MySQL Server 8.0\bin
step 1: First you have to go to the path and open CMD from the folder.
step 2: Then type mysqldump in the CMD
it should display as follows
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
step 3: Then type this command
mysqldump -u [user_name] -p [database_name] > D:\db_dump.sql
Note :
you should provide an absolute path for the output file.
Here I provide D:\
For Windows users you can go to your mysql folder to run the command
e.g.
cd c:\wamp64\bin\mysql\mysql5.7.26\bin
mysqldump -u root -p databasename > dbname_dump.sql
Note: This step only comes after dumping your MySQL file(which most of the answers above have addressed).
It assumes that you have the said dump file in your remote server and now you want to bring it down to your local computer.
To download the dumped .sql file from your remote server to your local computer, do
scp -i YOUR_SSH_KEY your_username#IP:name_of_file.sql ./my_local_project_dir
#echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%.%MM%.%DD%.%HH%.%Min%.%Sec%"
set drive=your backup folder
set databaseName=your databasename
set user="your database user"
set password="your database password"
subst Z: "C:\Program Files\7-Zip"
subst M: "D:\AppServ\MySQL\bin"
set zipFile="%drive%\%databaseName%-%datestamp%.zip"
set sqlFile="%drive%\%databaseName%-%datestamp%.sql"
M:\mysqldump.exe --user=%user% --password=%password% --result-file="%sqlFile%" --databases %databaseName%
#echo Mysql Backup Created
Z:\7z.exe a -tzip "%zipFile%" "%sqlFile%"
#echo File Compress End
del %sqlFile%
#echo Delete mysql file
pause;

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