Yes it's Windows sorry.
I'm using mysqldump with the option -T which creates a sql and a txt file per table.
mysqldump -u user -ppass db -T path
I use that option to be able to restore easily one table.
Now I'd like to restore all the tables.
mysql -u user -ppass db < path/*.sql
Obvously doesn't work
Also, I don't know where do my funcs/procs go.
You could use a FOR loop with the file wildcard (*.sql) to process each one, like this:
FOR /R %F in (*.sql) DO (
mysql -u user -ppass database %F
)
(Note that if you're running this from a batch file, the variable should be shown as %%F instead of just %F.)
Related
Is there a way to back up MySQL database automatically at certain times of the day for designated servers or send an email with an attachment.. Which ever do you think is the best and safest way to achieve this?
Best way to do this would be
mysqldump.exe --user=YourUserName --password=YourPassword --host=localhost --port=3306 --result-file="Path\dump.sql" --databases "DatabaseName1" "Database2"
mysqldump.exe --user=root --password=root --host=localhost --port=3306 --result-file="c:\www\db\backup.%date:~10,4%%date:~7,2%%date:~4,2%.sql" --default-character-set=utf8 --single-transaction=TRUE --databases "dbtest1" "dbtest2"
The pattern backup.%date:~10,4%%date:~7,2%%date:~4,2%.sql will create a unique name (backup20131010.sql) each time it will run
Now you just need to call this command in your task scheduler. That's it. :)
I would use Windows Task Scehduler/cron (depending on your system) and mysqldump. Scroll down in the link, it includes some insights how to achieve what you want.
You can add one of these commands to Windows task scheduler:
mysqldump –-user [username] –-password=[password] [database name] > [dump file]
or in a compact way:
mysqldump –u[username] –p[password] [database name] > [dump file]
or:
mysqldump -u[user] -p[password] --result-file="c:\<path>\backup.%DATE:~0,3%.sql" [database]
databaseW.2016,06,29-22,31,48-15.sql
#echo off
rem Backup Database (Daily,via Task Scheduler)
rem databaseW
set filename="c:\xampp\dbk\databaseW.%date:~6,4%,%date:~0,2%,%date:~3,2%-%time:~0,2%,%time:~3,2%,%time:~6,2%-%time:~9,2%.sql"
c:\xampp\mysql\bin\mysqldump.exe --user=root --password=dell#root --host=localhost --port=3306 --result-file=%filename% --default-character-set=utf8 --single-transaction=TRUE --databases "databaseW"
To create file whose name is based on the date and time, use %date% and %time%.
Note that the 2 variables are based on locale and cmd shell version
open the cmd windows
input echo %time% and echo %date%
mine is 22:11:16.80,06/29/2016 Wed
substr the variable through %variable:~startpos,length%
I want the time delimited by comma, so the cmd goes
echo %time:~0,2%,%time:~3,2%,%time:~6,2%,%time:~9,2%
to get a filename like databaseW.2016,06,29-22,31,48-15.sql
use set filename="databaseW.%date:~6,4%,%date:~0,2%,%date:~3,2%-%time:~0,2%,%time:~3,2%,%time:~6,2%-%time:~9,2%.sql"
check the date and time in advance
use the --result-file option instead of >; According to the Mysql Manuel, the charset of file saved using ">" is UTF-16, while the --result-file follows the --default-character-set
save to file BackpDay-databaseW.cmd
add it to a new task Action and set a trigger (Windows Task Scheduler)
I did the work, similar to what other people explained through... but with little difference and extra work:
1) I made a batch file
2) Ran that batch file through windows scheduler
3) Made appropriate schedule for that task
4) Inside the batch file these steps are executed:
4-1) Prepared a file name based on current date
4-2) Got a backup by mysqldump.exe in the corresponding directory & file name
4-3) Made a compress file through 7-Zip app(install it) & then delete the uncompressed backup
4-4) Put a copy on our network File-Server
Here is a script(.bat) sample:
#echo off
set current=%date:~10,4%%date:~4,2%%date:~7,2%
set filename="E:\MySQL Backups\DBName-%current%.sql"
set filename2="E:\MySQL Backups\DBName-%current%.zip"
echo %filename%
cd "E:\MySQL Backups"
C:\"Program Files"\MySQL\"MySQL Server 5.5"\bin\mysqldump.exe db_name --user=root --password=rootpass --host="127.0.0.1" --port=instancePort --result-file=%filename% --default-character-set=utf8 --single-transaction=TRUE
echo backup-finished
if exist %filename% (
"C:\Program Files\7-Zip\7z.exe" a %filename2% %filename%
echo zip-finished
del %filename%
)
if exist %filename2% (
copy %filename2% "\\192.168.x.x\MySQL Backups"
echo copy-finished
)
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.
The sql file named:bag.sql in /var/www/html/web(/var/www/html/web/bag.sql)
the current directory is [localhost web]i used mysql -uusername -p databasename > bag.sql
then let me input the password. i wait for long time, it doesn't show ok. why? how to import the database to mysql under centos.
if you have already created database then use below steps to import data from .sql file
tell which database to use:
use databasename;
Now give the source file path
source /var/www/html/web/bag.sql;
You need mysql -uusername -p databasename < bag.sql.
< Means "get the program's input from this file"
> Means "write the program's output to this file"
Connect to mysql database server
$ mysql -uusername -ppassword
Check database exists are not
$ show databases;
If not create the same
mysql>create database mydb;
note: mydb is database name(Give your own).
Check again for the database follow step 2.
exit
Import data to mydb(your own) database
$ mysql -uusername -ppassword mydb < bag.sql
note: your bag.sql is in the current directory from where you are executing the above command
Check for imported data.
YES FOR LONG WAIT YOU NEED TO CHECK YOUR BAG.SQL FILE SIZE.
Symbol '$' is shell prompt
Let me offer up another alternative using pv
pv /path/to/file.sql | mysql -uUSERNAME -pPASSWORD -D DATABASE_NAME
This will show you a progress indicator for the import. If you have never used pv it is a piper viewer tool in linux.
you're using the wrong redirection operator. > is used for sending the output to a file, not taking input from a file. Use <.
mysql -u username -p -h localhost DATA-BASE-NAME < data.sql
source
I am attempting to restore my mysql database to my website, and all of the tables in my database get dropped into individual files, so I am trying to figure out how can I restore all of the database .sql files through SSH with a single (or easy command) instead of restoring all 100 tables individually.
cat *.sql > data.sql
mysql -u <username> -p < data.sql
It depends on how you created the individual files - if they have all the instructions for recreating the tables (i.e., "Drop if exists...", "Create ...", and "Insert into ..."), then you can either concatenate them into mysql:
cat *.sql | mysql -u xxx -pxxx dbname
or write a script to do it
#!/bin/sh
mysql -u xxx -pxxx dbname < file001.sql
mysql -u xxx -pxxx dbname < file002.sql
The second choice lets you more easily control the order of files processed.
Finally, you might want to create your backups in a more convenient way - check out mysqldump for how to dump a database (or several!) into one file (basically, "mysqldump -u xxx -pxxx dbname > dbname.sql", but there are some helpful flags you might want to add).
I have a trouble in restoring MySQL table back to the database from command line. Taking backup of a table is working with mysqldump.Taking backup and restoring of a database is also working properly. I have used:
mysql -uroot -p DatabaseName TableName < path\TableName.sql
Thanks in advance
Ah, I think I see the problem here.
Your backup script looks fine. tbl_name works correctly as the optional 2nd argument.
To restore, you should simply run
mysql -uroot -p DatabaseName < path\TableName.sql
Running man mysql would have shown you the correct arguments and options
mysql [options] db_name
As your backup script only contains one table, only that table will be restored into your database.
Taking backup
mysqldump -u -p mydatabase table1 > database_dump.sql
restoring from backup flie need not include table name
mysql -u -p mydatabase < database_dump.sql
Best way to restore your database:
open cmd at bin folder
login to mysql:
mysql -uroot -pyour_password
show databases;
use db_name;
now hit source and put the complete path from address bar where your sql file is stored and hit ;
for example :
source db_name.sql;
Copy your db.sql file to your Mysql Server if you are in a remote machine:
$rsync -Cravzp --progress db.sql user#192.168.10.1:/home/user
Now you can go to your remote server as:
$ssh -l user 192.168.10.1
In the Mysql Server you must to do this:
user#machine:~$mysql -h localhost -u root -p
Obs: The file db.sql must be in the same place (/home/user).
Now type this command in you Mysql Server:
mysql>'\'. db.sql + Enter. Obs: Remove all ' from this command to work