SQL Server database backup using a Batch file
Using the code
Test.bat file
SET curr_dir=%cd%
cd /D E:
IF EXIST TEST (
cd TEST
mkdir ScriptTEST DataTEST
cd ScriptTEST
) ELSE (
mkdir TEST
cd TEST
mkdir ScriptTEST DataTEST
cd ScriptTEST
)
SET SRC="%~dp0TestBackUpSQL.sql"
SET DEST="TestBackUpSQL.sql"
if not exist %DEST% copy /V %SRC% %DEST%
sqlcmd -i "E:\TEST\ScriptTEST\TestBackUpSQL.sql"
2. In here you need to enter the database name and the back up set will be generated with name+date+time of server.
TestBackUpSQL.sql file
DECLARE
@pathName NVARCHAR(512),
@databaseName NVARCHAR(512)
SET @databaseName = 'Enter Your DataBase Name Here'
SET @pathName = 'E:\TEST\DataTEST\Enter Your DB Back Up Name Here_' +
Convert(varchar(8), GETDATE(), 112) + '_' +
Replace((Convert(varchar(8), GETDATE(), 108)),':','-')+ '.bak'
BACKUP DATABASE @databaseName TO DISK = @pathName WITH NOFORMAT,
NOINIT, NAME = N'', SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO
Comments