mysql backup using batch file and scheduled with windows task scheduler - batch-file

I have created the below batch file for backing up mysql individual tables, procedures and triggers from a database and then compress it using 7zip. when I run this in command prompt of windows it works perfectly and creates a file with name 20192004-1859.zip file size is few hundred mb But when I schedule it using the windows task scheduler it creates a file by name 20192004-.7z and the file size is just 1kb and empty too.Please find the code below and let me know how to make it work.
#ECHO Off
Echo Avoid Any MYSQL Transcations until backup completes
set TIMESTAMP=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%-%TIME:~0,2%%TIME:~3,2%
md E:\backup\%TIMESTAMP%\Adatabase
cd D:\wamp\bin\mysql\mysql5.7.9\bin\
mysql -s -e "SHOW TABLES FROM Adatabase" --user=root --password= --skip-column-names > E:\backup\tables.txt
for /f %%A in (e:\backup\tables.txt) DO (mysqldump --user=XXX --password=XXX Adatabase %%A > E:\backup\%TIMESTAMP%\Adatabase\%%A.sql)
mysqldump --user=root --password= --no-create-db --no-create-info --no-data --routines Adatabase > E:\backup\%TIMESTAMP%\Adatabase\procNtrig.sql
E:
cd E:\backup\
C:\7zip\7za a %TIMESTAMP%.zip e:\backup\%TIMESTAMP%\*
RMDIR /S /q e:\backup\%TIMESTAMP%

Related

Multi-file script execution from command line

I have multiple .sql script files and want to execute them one by one in batch file with proper logging mechanism.
I wrote below batch command to solve the problem
#ECHO OFF
SET SQLCMD="C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\SQLCMD.EXE"
SET PATH="C:\Users\sql_scripts\"
SET SERVER="server_name"
SET DB="database"
SET LOGIN="user_name"
SET PASSWORD="password"
SET OUTPUT="C:\Users\sql_scripts\output\OutputLog.txt"
CD %PATH%
ECHO %date% %time% > %OUTPUT%
for %%f in (*.sql) do (
%SQLCMD% -S %SERVER% -d %DB% -U %LOGIN% -P %PASSWORD% -i %%~f -b >> %OUTPUT%
)
But it has few problems that I want to solve as listed below :
I want to create dynamic out put file names based on input file name. Example if input sql file
name is "test.sql" then I want to create output file in "output" folder with name "test.sql".
Currently It writes all the log to one file i.e. OutputLog.txt
I want to show output log with script of execution to identify which script produced what
output. Let's say in one script file 5 scripts present then in output file I want to show the
script and its output log.

Batch file script for sqlcmd

I'm looking for a way to make the below commands a bit more automated. I would like to be able to use just a simple batch file if possible.
sqlcmd /S localhost\SQLEXPRESS /U [dbusername] /P [dbpassword]
1> drop database datastore
2> go
Have you tried this:
sqlcmd /S localhost\SQLEXPRESS /U [dbusername] /P [dbpassword] <cmds.txt
where cmds.txt is a file containing your commands (the drop and the go commands).

Write a Batch file for automatically delete the OLD IIS log file

Whenever i want to delete the old IIS logs files on my Shared WebServer i run this command on CMD:
for /R C:\HostingSpaces %f in (u_ex*.log) do del /q "%~ff"
It searches for IIS log files and will delete all of them. For sure it can't delete the current day's log files since they are already opened in IIS and can't be deleted.
It shows me the progress of deleting log files one by one in command prompt console.
i need to set a schedule task to run a BAT file to do this automatically each day.
I have made a bat file and pasted this command within it:
for /R C:\HostingSpaces %f in (u_ex*.log) do del /q "%~ff"
But when i run the batch file nothing happens and i don't see any result and action.
How to write a batch file for running this Command?
Your help is much appreciate.
Thank you
You need to use %%A in batch files for For loops rather than %A at the command prompt.
del C:\HostingSpaces\u_ex*.log /s
is easier.
I strongly suggest to use the forfiles command for the task:
forfiles -p C:\inetpub\logs\LogFiles\ -s -m *.log -d -180 -c "cmd /C DEL #File"
Explanation of the switches:
-s or /S : recurse into all subfolders
-p or /P : path
-m or /M : file mask
-d or /D : number of days (-180 = older than 180 days)
-c or /C : command to execute
You can then put it into a Scheduled Task and have it run daily.
For a decent Powershell alternative, see this other answer: for other suggestions on how to properly reduce the IIS LogFiles folder, check out this post that I wrote on the topic.
The reason that you get error is because that the current day's logs are opened in IIS.
IIS keep the current day's log open and write on it constantly. So you will not be able to delete the current day's logs.
This is the command that worked for me. My websites are all placed in a folder named HOSTINGSPACES and i do this to delete all of the log files (only the current day logs will remain at the end)
# for /R C:\HostingSpaces %f in (u_ex*.log) do del /q "%~ff"
u_ex*.log is the syntax of my logs, your logs may have another syntax, so take care of this.

How to "compile" or "link" a Windows batch file with other script files?

I have a batch file that runs multiple SQLCMD commands which run queries supplied by external SQL script files. The scripts themselves are fairly trivial, but I'm bugged by all the file dependencies required to run the batch. I've been googling for ways to link all these files together without luck.
Are there any techniques I can apply to encapsulate the batch script and SQL scripts within a single file, such that I could move the resulting file to other machines without dragging the individual sql files along? For the sake of organization/readability/information hiding, I would really prefer to avoid embedding the sql directly in the batch file.
Consider using 7-Zip. It is free and here
You could encapsulate all your BAT files and SQL commands into a zip like this:
C:\> 7za a -t7z archive.7z *.BAT *.SQL
Then you can extract like this:
C:\> 7z e archive.7z
Alternatively you could make a self-extracting executable with it - which has the benefit you don't need any tools to extract it when you arrive at another server, but you may not be able to Email that. By the way, if you change the command to the following, Windows can read the archive itself natively so you don't need to install extraction software wherever you go with the file:
C:\> 7za a -tzip archive.zip *.BAT *.SQL
Or you could use the Microsoft CAB tool that will be present everywhere documentation.
That would look something like this if you wanted to gather *.BAT and *.CMD into a "cabinet" called BATplusSQL.cab:
dir /b *.BAT *.CMD >files.txt
makecab /d "CabinetName1=BATplusSQL.cab" /f files.txt
del /q /f files.txt
Here is an example of what I mean.
Create a text file with one SQL query per line similar to this:
My Queries
Select * from table1 where something = somethingelse
Select * from table2
delete from table3
update table1 set something=nothing
Make sure the first line is not a query. It can be anything else. Tweak the settings in %sql% as necessary.
Run the following batch file:
#echo off
setlocal
set "sql=sqlcmd -S MySERVER -E -q "
set cnt=1
for /f "tokens=* skip=%cnt%" %%a in (sqlQueries.txt) do (
echo %sql% "%%a"
set /a cnt+=1
)
Remove the echo to run the queries.

how to execute all .sql file of current and sub folder using xp_cmdshell

I am using sql server 2008 , I am developing script that get all .sql file of given path ( also serch in subfolder recursively). Thanks in advance.
You could use a batch file like this. Call it ashwin.bat (or whatever you like) and it will look for all the files in C:\tmp\so\ashwin that have a .sql extension and then invokes sqlcmd against all of those files against a named instance database of localhost\localsqla and runs them in the master database.
#echo off
For /R "C:\tmp\so\ashwin\" %%i in (*.sql) DO CALL sqlcmd.exe -E -S localhost\localSQLA -d master -i %%i
A litle enhancement for logging purposes:
#echo off
For /R "C:\Deploy\SQL" %%i in (*.sql) DO CALL echo %%i && sqlcmd.exe -E -S DB_IP -d DATABASE -i %%i -j

Resources