I have the following batch code, the code executes fine when I double click on the batch but when I try to run it through windows task scheduler, it only runs partially. I am unable to understand why.
Basically the code deletes all the rows ergo, runs until mysql -e "DELETE FROM software_it.hardware" -u root and does not execute further. Any help would be great.
#ECHO OFF
setlocal enabledelayedexpansion
mysql -e "DELETE FROM software_it.hardware" -u root
FOR %%f IN ("*.csv") DO (
set old=%%~dpnxf
set new=!old:\=\\!
mysql -e "load data local infile '"!new!"' into table software_it.hardware COLUMNS TERMINATED BY ',' IGNORE 1 ROWS" -u root
echo %%~nxf DONE
)
When run from task scheduler it is likely that the 'current directory' will be different from that used when you run the script directly. I would therefore suggest adding the following third line:
IF /I "%CD%\" NEQ "%~dp0" PUSHD "%~dp0"
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 initially labeled this something different but then figured out my problem, but a different problem came up so now I'm editing this some.
First off let me start. I'm not a "coder" and everything I'm doing is all from research. What I'm trying to accomplish is running a batch file on a loop. It connects to MySQL on my VPS just fine. It will then save the return of the MySQL command to text file when information is there. The problem is when there isn't new information in the database, it just saves it as a blank text file. I would rather have it not save to text at all if there's no information on the DB. This is what I have so far. I'm also running this on Windows Server 2008.
#echo off
:loop
timeout /T 5
mysql --host=111.11.111.11 --port=3306 --user=user_name --password=password --database=db_name --execute="SELECT * FROM tablename LIMIT 1" > results.txt
if exist c:\users\administrator\desktop\results.txt goto end
goto loop
:END
start program.exe
exit
So what I need is something that saves to text if the MySQL command returns information, but if the MySQL command returns empty it continually does the loop until something is there.
Have you tried to save the result to a variable and do a check?
#echo off
:loop
timeout /T 5
Myans = mysql --host=111.11.111.11 --port=3306 --user=user_name --password=password --database=db_name -- execute="SELECT * FROM tablename LIMIT 1"
if not %Myans%=="" ( Myans >> results.txt )
if exist c:\users\administrator\desktop\results.txt goto end
goto loop
:END
start program.exe
exit
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