Run Sql Backups/Archive DB Backup File / Copy archived file to another directory or FTP Directory

Get All available Databases using Select name from sys.databases (in 2005) or Select name from sysdatabases (in 2000).

SqlCommand objCmd = new SqlCommand(@"
BACKUP DATABASE @dbName TO DISK = @backupPath;
exec xp_cmdshell @backupCmd;", objConn);


Backup Database command takes back up for given DB.

Exec xp_cmdshell command runs dos batch files with given command.

@backupCmd paramater has actual batch command, which will do archive the file and move the file to specified destination.

For Archiving I have used Rar command, which is a tiny exe available on Internet.

@backupCmd param value is :

rar a -s -df filename.rar dbbackupname.bak && xcopy /Y filename.rar & del /F/Q filename.rar;

Above command does Three different functions,

1) Archiving using rar
2) xcopy - Moving Archiving file into given destination (Real time it would be FTP Directory)
3) Delete Archived file from Source Directory

&& - Represents If rar command executes successfully, it goes to xcopy.
& - Even if Xcopy fails, del command deletes rar file on source directory.


You can specify Backup command value in Web.Config as follows:

Comments

Venu said…
add key="BackupCmd" value ="rar a -s -df "{1}\{0}.rar" "{1}\{0}" && xcopy /Y "{1}\{0}.rar" "{2}\" & del /F/Q "{1}\{0}.rar""

Popular posts from this blog

Windows Azure Package Build Error: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

Resource ID : 1. The request limit for the database is 180 and has been reached.

Update Excel Sheet using C#, Where Excel not Installed on Server