The work you do is important but the most important part is to save or backup it from any unseen disaster. The backups of database can be taken automatically by many ways but my requirement was some different. I had to deal with the massive database and had to move it to some other server via ftp so I tried various things and at last I ended up by creating my own application that can be customized anytime according to the requirement.
I started by using .Net inbuilt classes to create backup of Sql server database then compress it and FTP it to some other server afterwards. But there was a problem occured in it, that .Net wont be able to handle bigger file sizes i.e > 4 GB. So it lets me think of another soultions but lets start with the .Net classes itself because not all guys will have to deal with the files larger than 4 GB.
Below is the code that creats backup, compress it and then ftp it to the provided server.
First of all you need to include following namespaces.
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.Configuration;
using System.IO.Compression;
using System.IO;
using System.Net;
Then the following is the code that do rest of the thing
private void Form1_Load(object sender, EventArgs e)
{
//creating backup
string sFilePathAndName = "c:\Masterbackup.bak";
ServerConnection srvConn = new ServerConnection("localhost");
srvConn.LoginSecure = false;
srvConn.Login = "sa";
srvConn.Password = "password";
Server srvSql = new Server(srvConn);
if (srvSql != null)
{
Backup bkpDatabase = new Backup();
bkpDatabase.Action = BackupActionType.Database;
bkpDatabase.Database = "Master";
BackupDeviceItem bkpDevice = new BackupDeviceItem(sFilePathAndName, DeviceType.File);
bkpDatabase.Devices.Add(bkpDevice);
bkpDatabase.SqlBackup(srvSql);
}
//compressing
if(System.IO.File.Exists(sFilePathAndName))
{
FileStream sourceFile = File.OpenRead(sFilePathAndName);
byte[] buffer = new byte[sourceFile.Length];
FileStream destinationFile = File.Create("c:\Masterbackup.gzip");
GZipStream zip = null;
try
{
sourceFile.Read(buffer, 0, buffer.Length);
zip = new GZipStream(destinationFile, CompressionMode.Compress);
zip.Write(buffer, 0, buffer.Length);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
zip.Close();
sourceFile.Close();
destinationFile.Close();
System.IO.File.Delete(sFilePathAndName);
}
//ftping
try
{
FtpWebRequest oFtpWebRequest = (FtpWebRequest)WebRequest.Create("ftp://server.com/MasterBackup.gzip");
oFtpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
oFtpWebRequest.Credentials = new NetworkCredential("ftpUserName", "ftpPassword");
FileStream oFileStream = File.OpenRead("c:\MasterBackup.gzip");
buffer = new byte[oFileStream.Length];
oFileStream.Read(buffer, 0, buffer.Length);
oFileStream.Close();
oFileStream = null;
oFtpWebRequest.GetRequestStream().Write(buffer, 0, buffer.Length);
oFtpWebRequest = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
System.IO.File.Delete("c:\MasterBackup.gzip");
}
}
}
|