sqldump somehow looks for a table instead of full backup? - mysql

I have a simple BAT file that I want to run at times to take backups of two local development MySQL databases.
The file name should be "testdb1-Fri 6/6/2018 194233.sql" for example. However, my BAT script is making mysqldump look for a table called 6/6/2018 somehow? Script below, and screenshot of error.
#ECHO OFF
set filename=%date:~0,4%%date:~5,2%%date:~8,2%%date:~10,4% %time:~0,2%%time:~3,2%%time:~6,2%
"C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqldump.exe" -u root -proot -hlocalhost cadsys > C:\temp\testdb1-%filename%.sql
"C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqldump.exe" -u root -proot -hlocalhost comm1 > C:\temp\testdb-%filename%.sql
pause
cls
exit
Any pointers?

Add quotes around the suggested filename, the spaces within the filename are interpreted by mysqldump as seperate commands
set filename="%date:~0,4%%date:~5,2%%date:~8,2%%date:~10,4% %time:~0,2%%time:~3,2%%time:~6,2%"

Related

Where does mysqldump files go?

I've read a post saying that I would have to use this command below, to dump my sql files
$ mysqldump -u [uname] -p db_name > db_backup.sql
However, my question is, I don't quite get where the db_backup.sql comes from. Am I suppose to create a file or is it being created as I enter any name for the backup sql?
Also, where can I find the backup.sql at?
More information: I am doing this on Mariadb/phpmyadmin on the xampp shell
Whichever directory you are in when you run that command will have db_backup.sql.
The statement:
mysqldump -u user -p db_name generates an output on the terminal screen, assuming you are SSH'ed into the server. Instead of the output written on the screen, you can redirect the output to a file.
To do that, you use > db_backup.sql after the command.
If you are in directory /home/hyunjae/backups and run the command:
$ mysqldump -u [uname] -p db_name > db_backup.sql
You will see a new file created called db_backup.sql. If there's already a file with that name, it will be overwritten.
If you don't know what directory you are in, you can type pwd for present working directory.
db_backup.sql is automatically generated by the mysqldump command, and is located in the current directory (.) by default. If you need to specify a directory, you can directly specify /path/to/target/db_backup.sql.

Running mysql command from bat file with redirection

I have the following batch file:
#ECHO on
cd "C:\Program Files\MariaDB\mariadb\bin"
mysql -u root < "C:\database_setup.sql"
When I run the command directly in the command line, it works fine. When I run this batch file I get that it's trying to execute:
mysql -u root 0<"C:\database_setup.sql"
To solve this, I tried to escape the less than sign with:
mysql -u root ^< "C:\database_setup.sql"
It appears to be correct in the console but it's dumping the mysql options instead of inserting the contents of database_setup.sql.
I'm thinking that this is because the "<" is actually be referred to as a string since I'm escaping it and not as the redirection operator.
How does one accomplish running this command in a batch file (which works fine directly in the console)?
The following workaround could help you:
mysql -u root -e "SOURCE C:\database_setup.sql"
Also the following should work:
type C:\database_setup.sql | mysql -u root

using batch to detect whether database exists,if not ,create it

I am going to write a .bat file to realize this function:
to detect whether the database and the table existed,if not exist,create them.
I tried to write the bat file like this:
"C:\Program Files\MySQL\MySQL Server 5.6\bin\mysql.exe" -h localhost -u root --password=
select * from martin.aaaperson
Then in cmd when I execute this, the bat file will not run the query until after I exit from mysql.
C:\000test>"C:\Program Files\MySQL\MySQL Server 5.6\bin\mysql.exe" -h localhost
-u root --password=
(information of mysql)
mysql> exit
Bye
C:\000test>select * from martin.aaaperson
"select" is not knowby cmd
you need to do something like
mysql.exe -u root -p < your_sql_commands_in_this_file.sql
Put your sql into a file, and then redirect that file into mysql, so it's used as actual commands.
Batch files can only work at the command line. Once you run mysql.exe the .bat file is suspended until mysql exits, and then the batch resumes. That means by the time the batch fires back up again, you've exited mysql and are no longer doing sql operations - you're just back at a command prompt.

MySQL full backup with mysqldump.exe

set dateStr=%date:~-7,2%-%date:~-10,2%-%date:~-4,4%
"C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin\mysqldump.exe" -u user -p password --all-
databases --single-transaction --flush-logs --master-data=2 > full_backup_%dateStr%.sql
it works for another server we have. this is a new server but with the same database.
It only creates a 1 KB file with the content:
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
please help.
Found this question when I was trying to get my own Mysql database backed up and thought I'd share how I ended up doing it.
The code basically loops over all databases and creates a .sql file containing all structure and data for each database.
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: MySQL Backup
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: MySQl DB user
set dbuser=BackupUser
:: MySQl DB users password
set dbpass=BackUpUserPassWord
:: Switch to the MySQL data directory and collect the folder names
pushd "C:\Server\databases\mysql\data"
:: Loop through the folders and use the file names for the sql files, collects all databases automatically this way
:: Pass each name to mysqldump.exe and output an individual .sql file for each
FOR /D %%F IN (*) DO (
"C:\Program Files\MySQL\bin\mysqldump.exe" --user=%dbuser% --password=%dbpass% --databases %%F > "C:\Server\backups\mysql\%%F.%TODAY%.sql"
)
To answer your specific question, check to see if you are running the script as administrator and that the user you are trying to backup using has relevant privileges to the databases. I was stuck on this very problem until I ran my bat file as administrator.
To check if its a user problem, try running the mysqldump with the root user and see if that helps, if it does you know its a problem with database rights..
To run as administrator right click the bat file, and select Run as administrator, and don't forget to set the checkbox "run with highest privileges" in the scheduled task if you are using that.
Code is copied from here:
http://www.syntaxwarriors.com/2012/backup-up-a-windows-server-using-only-free-tools/
I testes your code and it worked. I think you should check again your bat file for any syntax mistake.
set dateStr=%date:~-7,2%-%date:~-10,2%-%date:~-4,4%
"D:\Sync\Apps\Wamp\bin\mysql\mysql5.5.20\bin\mysqldump.exe" -u root --all-databases --single-transaction --flush-logs --master-data=2 > full_backup_%dateStr%.sql
I removed the -pPASSWORD part because there is no password for root on my local server. So, if you're going to C/P it from here, don't forget to change mysqldump's path and to add that -p part.

How to enter password in mysql5.5 using batch script?

i have written a batch script but it still ask me for a password. i want to enter it automatically. please help me
here is my batch script :
setlocal EnableDelayedExpansion
c:
cd "C:\Program Files\MySQL\MySQL Server 5.5\bin"
mysql -u root -p root
but still in output it ask for a password as:
Enter Password :
i got the answer. for that find below my comment
You can't have a space between the option and the password. So it should be:
mysql -u root -proot
Or use --password=root
For other googlers like me, I'll add my solution.
Unfortunately, official documentation doesn't tell this clearly
Short parameters like these didn't work to me and prompted for password
mysqldump -uroot -p "qwerty" mybase > Z:\mybase.sql
With "full" name parameters it worked, just warning about this action as insecure
mysqldump --user="root" --password="qwerty" mybase > Z:\mybase.sql
echo 'Backup OK' > mysql_dump.log
Problem Solved Got the solution
c:
cd "C:\Program Files\MySQL\MySQL Server 5.5\bin"
mysql -uroot -proot -e "delete from db.tablename where columnname =
'something';
The problem is In this line "setlocal EnableDelayedExpansion"
This should be deleted. and the code runs smoothly :)
AFAIK you cannot do this using the mysql bin, but mysqladmin can.
See the docs here: http://dev.mysql.com/doc/refman/5.5/en/mysqladmin.html
-ppassword displays warning
Warning: Using a password on the command line interface can be insecure.
https://dev.mysql.com/doc/refman/5.7/en/password-security-user.html
Solution: You can use cnf options file
https://dev.mysql.com/doc/refman/5.7/en/option-files.html
linux:
echo -e "[client]\nhost=mysqlhost\nport=3306\nuser=root\npassword=${ROOT_PASS}" > root.cnf
mysql --defauls-file=root.cnf -e "SELECT * FROM users;" ${MYDB}
windows:
Ship root.cnf file with batch script and run
"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysql" --defauls-file=root.cnf -e "SELECT * FROM users;" %MYDB%
try this create a file for password .pw and below command works in batch fine.
mysql --default-file=C:\path*.pw -u %username% blah blah..
Your best option is to use MySQL options files.
MySQL 5.5 makes use of the .my.cnf file, you just need to supply your username and password. This approach means you can login to mysql just by typing mysql in your script and the .my.cnf file will fill in the rest; Here's how:
Create a .my.cnf file in the home directory of the user which will run the script. The contents should look like the following code block, swapping out your_user and your_password for their actual values.
[client]
user=your_user
password=your_password
It's prudent to change the permission of this file to either 400 or 600, this will mean only your user and root will be able to see the file.
chmod 600 .my.cnf
You're all set!
N.B.
From version 5.6 onwards you can use the mysql_config_editor utility, which generates a .mylogin.cnf, it has the same result as the above apporoach with the added benefit that the password will be encrypted by the utility.
Links
MySQL 5.5
https://dev.mysql.com/doc/refman/5.5/en/password-security-user.html
MySQL 5.6+
https://dev.mysql.com/doc/refman/8.0/en/mysql-config-editor.html
This worked for me, put it in a .bat file, and change the password with yours.
#echo OFF
setlocal EnableDelayedExpansion
C:
cd "C:\xampp\mysql\bin\"
mysql.exe --user="root" --password="the password"