0% found this document useful (0 votes)
54 views

SQL Backup How To Geek

This batch script backs up all SQL Server databases on a server except for the system databases to files named with the current date prepended. It gets the date, builds a list of databases, then loops through each one using SqlCmd to perform a backup to a file in the backup folder with the date and database name.

Uploaded by

Shreeram Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

SQL Backup How To Geek

This batch script backs up all SQL Server databases on a server except for the system databases to files named with the current date prepended. It gets the date, builds a list of databases, then loops through each one using SqlCmd to perform a backup to a file in the backup folder with the date and database name.

Uploaded by

Shreeram Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Batch Script to Backup All Your SQL Server Databases

@ECHO OFF SETLOCAL REM Get date in format YYYY-MM-DD (assumes the locale is the United States) FOR /F tokens=1,2,3,4 delims=/ %%A IN ( Date /T ) DO SET NowDate=%%D-%%B-%%C REM Build a list of databases to backup SET DBList=%SystemDrive%SQLDBList.txt SqlCmd -E -S MyServer -h-1 -W -Q SET NoCount ON; SELECT Name FROM master.dbo. sysDatabases WHERE [Name] NOT IN ( master ,'model ,'msdb ,'tempdb ) > %DBList% REM Backup each database, prepending the date to the filename FOR /F tokens=* %%I IN (%DBList%) DO ( ECHO Backing up database: %%I SqlCmd -E -S MyServer -Q BACKUP DATABASE [%%I] TO Disk= D:Backup%NowDate%_%%I.b ak ECHO. ) REM Clean up the temp file IF EXIST %DBList% DEL /F /Q ENDLOCAL ================================================================================ ======================== Backup and Restore Your SQL Server Database from the Command Line %DBList%

You might also like