mysqldump batch script to output individual tables for selective restore - batch-file

We currently have a DOS batch script that performs a MySQL dump on the entire database in a single file. What I would like to do is breakup the database by tables, so that within each 'date' folder, there would be a 'db' folder containing individual table dumps. This allows us to restore selected tables rather than restoring an entire db.
#echo off
md C:\backups\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%
cd C:\Program Files\MySQL\MySQL Workbench 5.2 CE\
mysqldump -h -u -p --databases db0 > c:\backups\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%\db0.sql
mysqldump -h -u -p --databases db1 > c:\backups\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%\db1.sql
I found a possible solution using shell: https://stackoverflow.com/a/134296/679449
This appears that it would allow me to export into individual tables, however I'm not sure how to/if it's possible to write in batch. Any help with that is appreciated.

My solution:
#echo off
md C:\backups\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%\db0
md C:\backups\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%\db1
cd C:\Program Files\MySQL\MySQL Workbench 5.2 CE\
mysql -s -e "SHOW TABLES FROM db0" -u -p --skip-column-names > C:\backups\tables.txt
for /f %%A in (C:\backups\tables.txt) DO (mysqldump -h -u -p db0 %%A > c:\backups\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%\db0\%%A.sql)
mysql -s -e "SHOW TABLES FROM db1" -u -p --skip-column-names > C:\backups\tables.txt
for /f %%A in (C:\backups\tables.txt) DO (mysqldump -h -u -p db1 %%A > c:\backups\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%\db1\%%A.sql)

Related

Using sqlcmd in a batch script to run multiple scripts with output files

I need to run about 50 scripts in a folder using sqlcmd from a batch file. Each script's query results need to be sent to its own output file. I have a working batch file that just runs each from a separate line:
sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P "%PASSWORD%" -i "%SCRIPTFOLDER%\master_departments.sql" -s "|" -o "%OUTPUTFOLDER%\master_departments.csv" -W
sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P "%PASSWORD%" -i "%SCRIPTFOLDER%\master_companies.sql" -s "|" -o "%OUTPUTFOLDER%\master_companies.csv" -W
sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P %PASSWORD% -i "%SCRIPTFOLDER%\bill_history.sql" -s "|" -o "%OUTPUTFOLDER%\bill_history.csv" -W
sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P %PASSWORD% -i "%SCRIPTFOLDER%\episodes.sql" -s "|" -o "%OUTPUTFOLDER%\episodes.csv" -W
Is there any way to run this in some kind of loop? I've seen examples that run a loop of all SQL scripts in a folder, but nothing that I've seen does it with an output file set.
Per #LotPings' suggestion I used the below code:
set INSTANCE=<someinstance>
set DATABASE=<somedb>
set USERNAME=<someuser>
set PASSWORD=<somepassword>
set "SCRIPTFOLDER=D:\<pathToScripts>\"
set "OUTPUTFOLDER=D:\<pathForOutput>\"
#Echo off
For /F "tokens=*" %%S in ('Dir /B "%SCRIPTFOLDER%*.sql" '
) do echo sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P "%PASSWORD%" -i "%%~fS" -s "|" -o "%%~dpnS.csv" -W
#pause
I ran that in a batch file and when it paused, the last line said, "The system cannot find the file specified."
Thinking it was perhaps the backslashes in my paths, I removed them and put a slash before the .sql in the for line, but I got the same results.
Removing the backslash altogether resulted in a "File not found" message when I ran it like that.
In case your output file name matches the script name (without extension)
and your parameters are the same for all scripts
#Echo off
For /F "tokens=*" %%S in ('Dir /B "%SCRIPTFOLDER%*.sql" '
) do echo sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P "%PASSWORD%" -i "%%~fS" -s "|" -o "%%~dpnS.csv" -W
The echo in front of sqlcmd prevents execution and allows to review the output. If all looks OK, remove the echo.
The for variable behaviour can be changed with ~ modifiers, see For /? or visit ss64.com/nt/for.html / syntax-args
To pass a folder to the batch you can input via set /P or hand over via command line arguments.

How to use a shrinkDBs.bat file to truncate all log.ldf files in nested folders?

I would like to be able to shrink all log files in nested folders. I have used this same script to modify databases but I am having problems with the shrink command.
When I run this script, I get an error stating it can not find the database in the sys.database_files. So I'm guessing my database is going by a different name in sys.database_files or simply is not added. Can you help?
for /r /d %%i in (*) do (
ECHO %%i\MY_DB.mdf
osql -S LOCALHOST\SQLEXPRESS -U johan -P johan_j-d master -Q "EXEC sp_attach_db 'MY_DB', '%%i\MY_DB.mdf', '%%i\MY_DB_log.ldf';"
osql -S LOCALHOST\SQLEXPRESS -U johan -P johan_j -d MY_DB -Q "DBCC SHRINKFILE (N'MY_DB_log',0,TRUNCATEONLY);"
osql -S LOCALHOST\SQLEXPRESS -U johan -P johan_j -d master -Q "EXEC sp_detach_db 'MY_DB';"
)
Problem solved. I should have been using the logical file name not the physical file name.

running a bunch of SQL files in a folder

I have a bunch of scripts in a folder which I have to run in order. I just open it up in SSMS and hit execute one by one. I have to wait for one of them to complete then run the second one.
Is there a way for me to create a batch script or a script in SSMS (if its possible) that will go through the files one by one? This would save me time as I can do something else while the scripts are running and prevent error (most of them rely on temp tables created in succession).
I think a batch script with sqlcmd is what I am looking for but no idea how to accomplish this task.
Thank you.
Very easy to put together a batch file for this. Put something like the following into a text file but end the file with .bat, eg 'ExecuteMyScripts.bat'
Sqlcmd takes various arguments but the main ones are -S for server instance, -d for database, -i for input file ie your SQL file, -U for user, -P for password, and -o for output file. Watch the case of the letters.
So open notepad and add the following:
sqlcmd -S mysqlserver -d mydb -U sa -P pass1 -i "c:\script1.sql" -o "c:\script1log.txt"
IF ERRORLEVEL 1 goto :eof
sqlcmd -S mysqlserver -d mydb -U sa -P pass1 -i "c:\script2.sql" -o "c:\script2log.txt"
IF ERRORLEVEL 1 goto :eof
sqlcmd -S mysqlserver -d mydb -U sa -P pass1 -i "c:\scripr3.sql" -o "c:\script3log.txt"
Save the file as ExecuteMyScripts.bat.
Double click the file and both script will be executed.
Adding 'IF ERRORLEVEL 1 goto :eof' after each sqlcmd call will jump to the end of the script if error found.
Full usage found here, http://msdn.microsoft.com/en-us/library/ms162773.aspx

How can I skip one file in a filesets?

Below is my code in batchfile:
for %%f in (%~dp0*.sql) do (
sqlcmd -S %SName% -U %UName% -P %Pwd% -d %DbName% -I -i "%%f" >>TsDeploy.txt 2>&1
)
the question is there is one file in that set must be NOT Run as first one.(cause the others is script file about create Table,that one is to insert data into table ).
How Should I do to achieve the goal in ONLY ONE batchfile?
You could rename the sql file that must not run first so that it sorts to the end (prefix with z?).
Or you could do something like
for %%f in (%~dp0*.sql) do (
if "%~nxf" neq "fileNameNotToRunFirst.sql" (
sqlcmd -S %SName% -U %UName% -P %Pwd% -d %DbName% -I -i "%%f" >>TsDeploy.txt 2>&1
)
)
sqlcmd -S %SName% -U %UName% -P %Pwd% -d %DbName% -I -i "%~dp0fileNameNotToRunFirst.sql" >>TsDeploy.txt 2>&1
Or you could create a master sql script that calls each of the others in the proper order, as described in How to Run a Series of T-SQL Scripts in a Specific Order

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