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
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
This is the way to restore a mysql database from command line:
mysql -u username -pPASSWORD database_name < file-20140410.sql
But imagine that I don't know the name of the file, just I know that starts with "file-" and the extension is ".sql"
but this doesn't work:
//this doesn't work
mysql -u username -pPASSWORD database_name < file-*.sql
I need dump I database knowing only the begin of the filename.
thanks in advance !
If you can't find the file, search in a CMD-window. Go to each drive, change to root directory and search for the file, i.e:
REM change drive letter
C:
REM change current directory to root directory
CD \
REM search your file in all subdirectories
DIR file-*.sql /s
At the end thanks to "esac" it is SOLVED, but with some small changes. This is the final script:
forfiles /m file-*.sql /c "cmd /c mysql -u USERNAME -pPASSWORD database_name < #file"
Use forfiles command to use wildcards and perform an action on each file.
c:\test>forfiles /m file-*.sql /c "mysql -u username -pPASSWORD database_name ^< #file"
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'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