I'm trying to create a batchfile that does a mysqldump with a timestamp filename.
I have tried:
mysqldump -uuser -ppassword database > C:\backup\%DATE%.sql
I get error:
C:\backup\06/18/13.sql
Which gives me an error:
The system cannot find the path specified.
Which I assume is because of the forward slashes. I've tried using set to set DATE and then invoke it in the mysqldump line, but then batch file crashes.
Try (mysqlbackup.bat):
#echo off
for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c%%a%%b)
for /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
mysqldump -uuser -ppassword database > C:\backup\%mydate%_%mytime%.sql
Courtesy to https://stackoverflow.com/a/203116/1920232
Related
I'm using MySQL Server 5.5 and need to schedule a daily database backup.
Am currently doing the following in a batch file:
set currdate=%date:~4%
Set FileDate=%currdate:/=_%
mysqldump -u root-proot db > "C:\backup\database\db%FileDate%.sql"
It exports all tables in a single file. I want to export one file per table.
The following outputs all table names to a temporary file first and then iterates through them, dumping each one to an appropriately named file:
#echo off
set currdate=%date:~4%
set filedate=%currdate:/=_%
mysql --skip-column-names -u root -proot db -e "show tables;" > tables.tmp
for /f "skip=3 delims=|" %%t in (tables.tmp) do (
mysqldump -u root -proot db %%t > "C:\backup\database\db_%%table_%filedate%.sql"
)
del tables.tmp
As you have not indicated what is wrong with the answer provided, here's a similar answer, (please note that this is completely untested):
#Echo Off
Rem modify as necessary
Set "buDest=C:\backup\database"
Set "dbName=db"
Set "dbPass=root"
Set "dbUser=root"
Rem If binaries are not in %CD% or %PATH% modify the Set line below
Rem Example: Set "sqlBin=C:\Program Files\MySQL\MySQL Server 5.5\bin"
Set "sqlBin=."
Set "dStamp="
For /F "Tokens=1-3 Delims=/ " %%A In ('RoboCopy/NJH /L "\|" Null') Do If Not Defined dStamp Set "dStamp=%%A_%%B_%%C"
If Not Exist "%buDest%\" MD "%buDest%" 2>Nul || Exit /B
"%sqlBin%\mysql" --user=%dbUser% --password=%dbPass% -D %dbName% -Bse "Show Tables";|^
For /F "Skip=3 Delims=| " %%A In ('FindStr "^|"') Do (
mysqldump --user=%dbuser% --password=%dbpass% %%A >"%buDest%\%dbName%-%%A-%dStamp%.sql")
Just ensure that the value data on lines 3, 4, 5, 6 and possibly 9 is modified as necessary
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
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
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
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