Skip to content

Instantly share code, notes, and snippets.

@raducugheorghe
Created February 6, 2015 09:05
Show Gist options
  • Save raducugheorghe/c4b208cd2842c68a0c7d to your computer and use it in GitHub Desktop.
Save raducugheorghe/c4b208cd2842c68a0c7d to your computer and use it in GitHub Desktop.
[TransactSQL] Backup website and database from SQL job (uses winrar)
1. Map network drive
EXEC XP_CMDSHELL 'net use W: \\mainstore\Backup PASSWORD /user:USERNAME'
2. Backup Database
declare
@timestamp varchar(8),
@fileName varchar(1024)
SELECT @timestamp = CONVERT(VARCHAR(8), GETDATE(), 112)
select @fileName = N'W:\APPS\Databases\DBNAME_' + @timestamp +'.bak'
BACKUP DATABASE DBNAME
TO DISK =@filename
3. Backup Site
EXEC XP_CMDSHELL '"C:\Program Files\WinRAR\Rar.exe" a -ag W:\APPS\WebSites\SITENAME_.rar I:\WebSites\SITENAME'
4. Disconnect Network Drive
EXEC XP_CMDSHELL 'net use W: /DELETE'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment