We can take SQL Server database backup in various processes. This is one method which will take database backup daily with time basis automatically.
To achieve above the functionality we have to follow some steps.
#1. Create a procedure in database with below code for taking database backup.
CREATE PROCEDURE usp_BackupDatabase @databaseName VARCHAR(100) AS BEGIN SET NOCOUNT ON; DECLARE @sqlCommand VARCHAR(1000); DECLARE @dateTime VARCHAR(20); SELECT @dateTime = REPLACE(CONVERT(VARCHAR, GETDATE(),111),'/','') + REPLACE(CONVERT(VARCHAR, GETDATE(),108),':','') SET @sqlCommand = 'BACKUP DATABASE ' + @databaseName + ' TO DISK = ''F:\SQLBackup\' + @databaseName + '_Full_' + @dateTime + '.BAK'''; EXECUTE sp_executesql @sqlCommand; END
#2. Create a sql file(e.g. backup.sql) with below code for calling procedure and passing parameter for database name for which we want to take backup. usp_BackupDatabase ‘MFTest’ Go
QUIT
#3. Create a scheduled task(Start menu -> Control Panel -> Administrative Tools -> Task Scheduler -> Create task) in windows. Add this script to the task. sqlcmd -S [SERVER NAME] -E -i [SQL file path for calling procedure]
Example: sqlcmd -S MILU-PC\SQLEXPRESS -E -i F:\backup.sql
Schedule it for daily backup and time also. Save it. Then scheduler will execute back.sql file and procedure will executed.
Database backup will take automatically by time set.