DB backup under SQL Express

SQL Express has no Jobs engine. And Shared SQL server as well. So Management->Maintenance Plans is not exists.
So there is a batch file that you can put in Windows Scheduler to create db backups:

@echo off

set databaseName=%1
echo %databaseName%

set backupFolder=%2
echo %backupFolder%

for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo %fullstamp%

set OutDir=%backupFolder%\%databaseName%_%fullstamp%.bak
echo %OutDir%

sqlcmd -S <serverName\instanceName> -U <user> -P <pass> -Q "BACKUP DATABASE [%databaseName%] TO disk='%OutDir%'"


References
This one was not working because #4 was not exists. But it led me to BACKUP DATABASE command MS Help and there where the involvement of the script 

Comments

Popular posts from this blog

Configuring IIS to allow CORS requests