How to create mysqldump batch file? - mysql

I tried to create a batch file that can backup all my databases. My systems details:
OS: Server -> Windows Server 2003, Testing/local machine -> Windows Vista
Databases: MySql 5.XX
Batch file:
#echo off
START C:\wamp\bin\mysql\mysql5.1.33\bin\mysqldump.exe --opt -h localhost -uroot -psecret testdb | gzip > dump.sql");
In my code, i try to dump the "testdb" database into dumb.sql file. Can I set the name into such like : "dbname_date_time.sql"??
I answered my own question. Whoever that has the privilleges, please close/delete this question. Thanks in advance :)

I will assume you have no problem with mysqldump command.
So, to manipulate date/time in bat file, you can use combinations of followings
echo %date% which gives me "木 2010/01/14" in my pc
echo %time% which gives me 4:02:15.28
for /f "tokens=1-5 delims=/" %%d in ("%date%") do echo %%e %%f gives me "01 14"
for /f "tokens=1-5 delims=:" %%d in ("%time%") do echo %%d %%e gives me "4 02" H/M
Build file name and rename your dump.sql to new filename

since you have mysql in your system, you can use the date/time functions that comes with mysql
execute it using the mysql client and use a for loop to get the result. Then pluck the return result into your dump file variable name

Related

Mysqldump' is not recognized as an internal or external command operable program or batch file

I have been trying to create a batch to backup MySQL database using task scheduler..
FOR /F "tokens=1-4 DELIMS=/ " %%F IN ('date /T') DO (set v_date=%%F%%G%%H)
FOR /F "tokens=1-4 DELIMS=: " %%F IN ('time /T') DO (set v_time=%%F%%G%%H)
set fname=database_backup_%v_date%_%v_time%.sql
echo %v_time%
echo %fname%
set desfolder=C:\backup
set desfolder1=D:\backup
echo %desfolder%
echo %desfolder1%
mysqldump --add-drop-table -u root -pxxxx xxxx>%desfolder%\%fname%
mysqldump --add-drop-table -u root -pxxxx xxxx>%desfolder1%\%fname%
pause
This particular code worked fine for XP but when coming to Windows task schedule I get the following error:
mysqldump' is not recognized as an internal or external command operable program or batch file
but if operate it manually it works fine
Do you have mysqldump in your path?
Either
add it to the path, see http://dev.mysql.com/doc/mysql-windows-excerpt/5.1/en/mysql-installation-windows-path.html
Add the full paths to mysqldump within the script

Windows Batch file to create MySQL dump adding variable

I have created the following windows batch file backup.bat:
cls
mysqldump -u root -p my_database_name > BACKUP.sql
My problem is that batch create the same file in the same path that it has been run from. I want to add some timestamp or date to the file name BACKUP.sql to be something like BACKUP_2330255555588.sql to make different files in the same place.
Another side: for restore I need a batch file that prompt input for the file name.
You can try following snippets:
Backup file:
SET backupPath="C:\backups\"
FOR /F "TOKENS=2-4 DELIMS=/ " %A IN ('DATE /T') DO SET date=%%C-%%A-%%B
For /f "tokens=1-4 delims=/: " %a in ('echo %time%') do (set mytime=%a-%b-%c-%d)
SET timestamp=%date%_%mytime%
mysqldump -u ROOT --password=PASS my_database_name > "%backupPath%BACKUP%timestamp%.sql"
Recovery file with prompt:
#echo off
echo "Running backup script"
set /p BackupFilePath= Enter the recovery file path?
#echo on
mysql -u ROOT --password=PASS my_database_name<%BackupFilePath%
Bru

Automatic mysql backup on xampp windows 8?

I recently got in problem, my hard disk got crashed!
ibdata1 file at "F:\xampp\mysql\data\ibdata1" in my hard disk got corrupt!
The file size was 8 gb but when I was trying to copy after 6 gb it was showing redundancy check error!
I recently installed new xampp installation, just wondering if there is any way that it create automatic backup of files like webhosting service provider does.
Any help will be much appreciated.
To extend answer you could use script below (for XAMPP or other mysql configuration). After creating bat file it can be set up in Windows Scheduler as weekly task for example.
This script would create files like C:\backups\db1-2014-03-27.sql depending on date
mysqldumper.bat
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET date=%yyyy%-%mm%-%dd%
C:\xampp\mysql\bin\mysqldump.exe db1 -h localhost -u user1 -pPassWord1 > C:\backups\db1-%date%.sql
C:\xampp\mysql\bin\mysqldump.exe db2 -h localhost -u user2 -pPassWord2 > C:\backups\db2-%date%.sql
I use a batch file that calls mysqldump and is executed on a schedule by Windows Task Scheduler.
First you need to create a batch file. Open a new text file and inlclude something similar to this. Save it as a .BAT. You have to update the location of your MySQL bin. on the first line. This should be in your xamppfiles directory.Then include the proper mysql user name, database name and password in the mysqldump command
REM Export all data from this database
cd C:\Program Files\MySQL\MySQL Server 5.6\bin
REM To export to file (structure only)
mysqldump --no-data [DATABASENAME] -h localhost -u [USER] -p[PASSWORD] > C:\databackup\database_ddl_backup.sql
mysqldump --no-create-info --no-create-db [DATABASENAME] -h localhost -u [USER] -p[PASSWORD] > C:\databackup\database_data.sql
Then you just have to schedule this batch file using Windows Task Scheduler. Here is an article that explains this for W7 and W8:
http://www.thewindowsclub.com/how-to-schedule-batch-file-run-automatically-windows-7
Just to extend a little more the given answers in case anyone wants to add time to the generated backup filenames.
Using this: https://stackoverflow.com/a/19741875
We can build the following batch file:
FOR /f %%a IN ('WMIC OS GET LocalDateTime ^| FIND "."') DO SET DTS=%%a
SET date=%DTS:~0,8%-%DTS:~8,6%
C:\xampp\mysql\bin\mysqldump.exe db1 -h localhost -u user1 -pPassWord1 > C:\backups\db1-%date%.sql
C:\xampp\mysql\bin\mysqldump.exe db2 -h localhost -u user2 -pPassWord2 > C:\backups\db2-%date%.sql

MySQL Database backup automatically on a windows server

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
)

How to easily import multiple sql files into a MySQL database?

I have several sql files and I want to import all of them at once into a MySQL database.
I go to PHPMyAdmin, access the database, click import, select a file and import it. When I have more than a couple of files it takes a long time.
I would like to know if there is a better way to import multiple files, something like one file which will import the other files or similar.
I'm using WAMP and I would like a solution that does not require installing additional programs on my computer.
In Windows, open a terminal, go to the content folder and write:
copy /b *.sql all_files.sql
This concate all files in only one, making it really quick to import with PhpMyAdmin.
In Linux and macOS, as #BlackCharly pointed out, this will do the trick:
cat *.sql > .all_files.sql
Important Note: Doing it directly should go well, but it could end up with you stuck in a loop with a massive output file getting bigger and bigger due to the system adding the file to itself. To avoid it, two possible solutions.
A) Put the result in a separate directory to be safe (Thanks #mosh):
mkdir concatSql
cat *.sql > ./concatSql/all_files.sql
B) Concat them in a file with a different extension and then change it the name. (Thanks #William Turrell)
cat *.sql > all_files.sql1
mv all_files.sql1 all_files.sql
This is the easiest way that I have found.
In Windows (powershell):
cat *.sql | C:\wamp64\bin\mysql\mysql5.7.21\bin\mysql.exe -u user -p database
You will need to insert the path to your WAMP - MySQL above, I have used my systems path.
In Linux (Bash):
cat *.sql | mysql -u user -p database
Goto cmd
Type in command prompt
C:\users\Usersname>cd [.sql tables folder path ]
Press Enter
Ex: C:\users\Usersname>cd E:\project\database
Type command prompt
C:\users\Usersname>[.sql folder's drive (directory)name]
Press Enter
Ex: C:\users\Usersname>E:
Type command prompt for marge all .sql file(table) in a single file
copy /b *.sql newdatabase.sql
Press Enter
EX: E:\project\database>copy /b *.sql newdatabase.sql
You can see Merge Multiple .sql(file) tables Files Into A Single File in your directory folder
Ex: E:\project\database
I know it's been a little over two years... but I was looking for a way to do this, and wasn't overly happy with the solution posted (it works fine, but I wanted a little more information as the import happens). When combining all the SQL files in to one, you don't get any sort of progress updates.
So I kept digging for an answer and thought this might be a good place to post what I found for future people looking for the same answer. Here's a command line in Windows that will import multiple SQL files from a folder. You run this from the command line while in the directory where mysql.exe is located.
for /f %f in ('dir /b <dir>\<mask>') do mysql --user=<user> --password=<password> <dbname> < <dir>\%f
With some assumed values (as an example):
for /f %f in ('dir /b c:\sqlbackup\*.sql') do mysql --user=mylogin --password=mypass mydb < c:\sqlbackup\%f
If you had two sets of SQL backups in the folder, you could change the *.sql to something more specific (like mydb_*.sql).
just type:
cat *.sql |mysql -uroot -p
and mysql will import all the sql file in sequence
Enter the mysql shell like this.
mysql --host=localhost --user=username --password --database=db
Then use the source command and a semicolon to seperate the commands.
source file1.sql; source file2; source file3;
You could also a for loop to do so:
#!/bin/bash
for i in *.sql
do
echo "Importing: $i"
mysql your_db_name < $i
wait
done
Source
Save this file as .bat and run it , change variables inside parenthesis ...
#echo off
title Mysql Import Script
cd (Folder Name)
for %%a in (*) do (
echo Importing File : %%a
mysql -u(username) -p(password) %%~na < %%a
)
pause
if it's only one database modify (%%~na) with the database name .
The easiest solution is to copy/paste every sql files in one.
You can't add some sql markup for file importation (the imported files will be in your computer, not in the server, and I don't think MySQL manage some import markup for external sql files).
in windows open windows powershell and go to the folder where sql files are then run this command
cat *.sql | C:\xampp\mysql\bin\mysql.exe -u username -p databasename
Just type below command on your command prompt & it will bind all sql file into single sql file,
c:/xampp/mysql/bin/sql/ type *.sql > OneFile.sql;
Import From multiple SQL files into one Database.
Step 1: Goto to the folder and create file 'import-script.sh' with execute permission
(give Permission to file is chmod u+x import-script.sh )
#!/bin/bash
for i in *.sql
do
echo "Importing: $i"
mysql -u USERNAME -pPASSWORD DBNAME < $i
wait
done
The main thing is -p and PASSWORD didn't add any space.
Step 2: then in your terminal run this command ./import-script.sh
for windows users,
You can select the database in the phpMyadmin interface on the left, drag and drop all your files from your windows folder onto the web UI of phpMyadmin.