MySQL full backup with mysqldump.exe - mysql

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.

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.

sqldump somehow looks for a table instead of full backup?

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%"

Copying mysql databases from one computer to another

I want to copy my mysql database from my computer to another computer. How can I do this?
How to copy Mysql database from one Computer to another / backup database using mysqldump
We can transfer a MySQL database from one PC to another PC using
mysqldump command.
We have to create dump file of database to transfer database from
one PC to another PC.
MySQL database is not portable database i.e. we cannot transfer it
from one PC to another PC by copying and pasting it.
We can use following method to transfer database.
Creating a dumpfile from database/ Taking backup of MySQL database:
Open command prompt.
Execute following commands to change directory
>c: “press enter”
>cd program files/MySQL/MySQL Server 5.1/ bin “press enter”
>mysqldump -u root -p database_name > database_name.sql “press enter”
Enter password: password of MySQL
Copy sql file and paste it in PC where you want to transfer database.
2. Dumping sql file into database:-
- Open MySQL command line client command prompt.
- Execute following command to create database.
create database database_name;
“press enter” Database name is must as that of your database_name.
Copy that sql file into location “c:/program files/MySQL/MySQL Server 5.1/bin”
*- Now open command prompt and execute following commands.*
>C: “press enter”
>cd program files/MySQL/MySQL Server5.1/bin “press enter”
>mysql –u root –p database_name < database_name.sql “press enter”
Your database is created on PC.
Now in MySQL command prompt check your database.
Another one:1
This best and the easy way is to use a db tools(SQLyog)
http://www.webyog.com/product/downloads
With this tools you can connect the 2 databases servers and just copy one database on server a to server b.
For more info
http://faq.webyog.com/content/12/32/en/mysql-5-objects-are-greyed-out-in-copy-db-to-other-host-dialogue.html
Another one:2
For a database named "lbry", try this:
mysqldump -u root -p lbry > dump-lbry.sql
Create a database of the same name ("lbry" in this example) on the computer to which you wish to copy the database contents
Then import it:
mysql -u root -p lbry < dump-lbry.sql
You can do by this process step-by-step using MySQL WorkBench.
Install MySQL Workbench
Connect to existing Database
Go to Navigator -> Management -> Data Export.
(this will dump queries of tables one by one in a separate folder, Workbench uses the same folder to import)
Create Database on target PC.
Connect to Target Database (would consist of 0 tables in DB)
Go to Navigator -> Management -> Data Import/Restore.
(this will use the dump folder and create tables in your target Database).
Hope this helps.
The only SAFE way to copy databases from one machine to another is to first quiesce the database (make sure no clients are modifying it), then use the mysqldump command to create a text representation of your schema and the contents of your tables. Then copy that text file over to the other machine and read it in by specifying it as the input to the mysql command.
Attempting to copy the actual mysql data directories over is asking for trouble, since they are dependent on the architecture of the machine that mysql is running on and likely on the version of mysql and whatever storage engine is in use.
This tutorial is in Ubuntu but will work on Redhat, Centos, Fedora, Suse
We can dump database, transfer it to another server, and restore it
It will show how to take care of things like modified credentials as a result and moving debain.cnf file
4 dump restore will slow down the serverHow it works
4.1 Run mysqldump on source server:this builds a MySQL executable script for the destination server.
During this time the MySQL server will queue queries
4.2 Copy dump file to the destination server
4.3 Empty destination server
4.4 Execute dump file on the destintion server
Server A(Source Server)
Server B (Destination Server)
Case 1:Server A
root#source$ mysql --defaults-file=/etc/mysql/debain.cnf
mysql>show databases;
mysql>use testdb;(The database to dump)
mysql>show tables;(To Check the tables)
mysql>^c
-- now dump the databses
root#surce$ mysql --defaults-file=/etc/mysql/debain.cnf --all-databses | gzip -c > dump.sql.gz
root#surce$ gzip -dc dump.sql.gz
To copy the files create a ssh key on the source server
root#surce$ ssh-keygen
root#surce$ cat /root/.ssh/id_rsa.pub
select and copy all the ssh key string
root#surce$ scp dump.sql.gz ubuntu#destination:
goto destination server
last step copy the contents of debain.cnf file
root#surce$ cat /etc/mysql/debain.cnf
[client]
host = localhost
user = debain-sys-maint
password = mysecret
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debain-sys-maint
password = mysecret
socket = /var/run/mysqld/mysqld.sock
basedir = /usr
select all and copy this file to detination server.
Note: The sockey path can be different in your machine .use locate command to find the exact path
Case 2. Server B
drop all databses
root#destination$ echo show databases | mysql --defaults-file=/etc/mysql/debian.cnf --skip-column-names | awk '{print "drop database "$1";"}'
if this command doesnot drop databses use it with -force option
root#destination$ echo show databases | mysql --defaults-file=/etc/mysql/debian.cnf --skip-column-names | awk '{print "drop database "$1";"}' | mysql --defaults-file=/etc/mysql/debian.cnf -f
copy the ssh key on the destination server
root#destination$ echo "paste the key here" >> /home/ubuntu/.ssh/authorised_keys
goto source Server and use scp command to move the dump on the destination server
(inject the file)
root#destination$ gzip -dc /home/ubuntu/dump.sql.gz | mysql --defaults-file=/etc/mysql/debain.cnf
root#destination$ > /etc/mysql/debain.cnf
root#destination$ nano /etc/mysql/debain.cnf
paste the contents of .cnf file from source server here and save the file
:x
root#destination$ mysql --defaults-file= /etc/mysql/debain.cnf
if you get the mysql prompt then everything should be working file
mysql>
I was able to restore a backup that was shared with me following this thread, specifically #jmail's answer, but, I thought that I could provide a bit more concise answer for future users. I received a dump file with a .sql extension, not a .dump extension as I would have expected.
I tried to place it in my project folder and restore it but I got error 22, referring to access privileges. I moved it to “c:/program files/MySQL/MySQL Server 5.1/bin” and then ran it by:
1) Starting MySQL in the command prompt.
2) Creating the new database that I wanted to restore to
3) Switching to the database
USE new_DB;
4) Running
source c:/program files/MySQL/MySQL Server 5.1/bin/backup.sql
I'm not sure how the backup.sql file was created but this worked for restoring it on my Windows 10 system.
mysqldump --databases dbname -hsource_server_ip -usource_server_userName -psource_server_passcode | mysql
-udest_server_user_name -pdest_server_user_passcode &
There are three general ways to invoke mysqldump:
shell> mysqldump [options] db_name [tbl_name ...]
shell> mysqldump [options] --databases db_name ...
shell> mysqldump [options] --all-databases
If you do not name any tables following db_name or if you use the --databases or --all-databases option, entire databases are dumped.
mysqldump does not dump the INFORMATION_SCHEMA database by default. MariaDB dumps the INFORMATION_SCHEMA if you name it explicitly on the command line, although currently you must also use the --skip-lock-tables option.
To see a list of the options your version of mysqldump supports, execute mysqldump --help.
I just summarize jmail's answer:
   Database to SQL file at computer 1:
   mysqldump --user <user name> --password <database> > <output file> for example mysqldump --user root --password movie > movie.sql
   SQL file to database at computer 2:
   mysql --user <user name> --password <database> < <output file> for example mysql --user root --password movie < movie.sql

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

MySQL Dump to Directory

I'm currently trying to make a copy of a site to run locally, and i'm having some difficulty in downloading a dump of the mySQL database using PhpMyAdmin. (In that it doesn't work). I was wondering (and I suspect) if there is an sql command I can execute that would dump out the database to a location that I can download it via FTP.
I've found the following:
mysqldump --opt --user=root --password password > /home/backup/db.sql
but the page says that this will only run via shell, and I don't have shel access to the server. Can anyone help?
mysqldump -u root -p -T/path/ database
Simple Three steps Export mysql DB
**step1 :** run windows command prompt
**step2 :** make where yours wamp mysql stored directory ex E:\wamp\bin\mysql\mysql5.5.24\bin
**step 3 :**
E:\wamp\bin\mysql\mysql5.5.24\bin> mysqldump -u UserName -p PassWord dbname >c:\dbname.sql
PERFORM MySQL DATABASE DUMP:
Having registered mysql.exe in the Paths of Environment Variables (On
Windows Os)
Open windows console
Type the following Query:
mysqldump [Your database Name] -u root -p > C:/[Your Destination Directory ]/[ new Backup Database Name.sql ]
You will be prompted for a database user password
Enter Password:*******
when the dump is over...the directory will go back to the current
user session in Windows console
For this to work well, replace all parameters in square braces "[ ]" with your own values without the square braces
Open the Command prompt from MySQL bin folder (Make sure you have Read/Write Access)
Use the below mysqldump command
mysqldump -h [hostname] -u [username] -p [your database] > [your destination path][your filename.extn]
Ex: mysqldump -h localhost -u root -p test_DB > D:\MySQL\Dumps\test_file.sql
***WARNING : In case, if your custom path has any spaces in between, the above query will throw an error.
Invalid Path Description
To avoid Invalid Path Description, Give your path in double-quotes.
Ex : mysqldump -h localhost -u root -p test_DB > D:\MySQL\"Dumps from ABC"\test_file.sql
What are you looking for is Sypex Dumper 2. It exports (backups) your database directly to disk, so you can download the backup via ftp. It is written on php, supports large databases and has a very nice interface.
You can go to the directory where you want store the file, then execute de mysqldump command.
~/Desktop$ sudo /Applications/XAMPP/xamppfiles/bin/mysqldump -u root -p publicaciones > publicaciones.sql
PD. Use sudo to grant full permissions.
PD. If you have your PATH setup, don`t need use the full route to your MySql commands.