PDA

View Full Version : I need a c# code sample to do a db backup from control panel API


javier1981
04-06-2009, 10:04 AM
Hi everybody,

Could i get a prerequisites to do a db backup from control panel API, and the c# sample code??
My c# code not working...

public static void BackupDataBase(string dbName, string backuppath, string datapath, MailAddress toSend)
{

try
{
CustomerApi customerAPI = new CustomerApi();
customerAPI.Sql2005CreateBackupCompleted += new Sql2005CreateBackupCompletedEventHandler(customerA PI_Sql2005CreateBackupCompleted);
customerAPI.Sql2005CreateBackupAsync(Configuration Manager.AppSettings["appikey"], dbName,new Object[4] {dbName,backuppath,datapath,toSend});
}
catch (Exception ex)
{
string err = ex.Message;
System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress("ae.solutions.inc@gmail.com");
System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("support@leonni.com");
string subject = "Leonni-Application Error";
Utils.SendMail(to, from, subject, err, new string[0], new string[0], true);

}

}
static void customerAPI_Sql2005CreateBackupCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
if (!e.Cancelled)
{
try
{
Object[] userState = (e.UserState as Object[]);
string dbName = Convert.ToString(userState[0]);
string backuppath = Convert.ToString(userState[1]);
string datapath = Convert.ToString(userState[2]);
MailAddress toSend = (userState[3] as MailAddress);

string filesource = String.Format("{0}{1}.bak",backuppath,dbName);
string fileDest = String.Format("{0}{1}.bak", datapath, DateTime.Today.ToString("yyyy-MM-dd"));
System.IO.File.Move(filesource,fileDest);

if (File.Exists(fileDest))
Utils.ZipFile(fileDest,toSend);
}
catch (Exception ex)
{
string err = ex.Message;
System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress("ae.solutions.inc@gmail.com");
System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("support@leonni.com");
string subject = "Leonni-Application Error";
Utils.SendMail(to, from, subject, err, new string[0], new string[0], true);
}
}
else
{
string err = e.Error.Message;
System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress("ae.solutions.inc@gmail.com");
System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("support@leonni.com");
string subject = "Leonni-Application Error";
Utils.SendMail(to, from, subject, err, new string[0], new string[0], true);
}
}

public static bool ZipFile(string fileToZip, MailAddress toSend)
{
bool result = false;

try
{
string zipname = String.Format("{0}.zip", fileToZip);
if (File.Exists(zipname))
File.Delete(zipname);

using (ZipFile zip = new ZipFile())
{
zip.AddFile(fileToZip,"backup");
zip.Save(zipname);
}

if (!Object.Equals(toSend, null))
{
MailAddress to = toSend;
MailAddress from = new MailAddress("support@leonni.com");
string subject = String.Format("{0}-{1}", "Backup de Base de Datos", DateTime.Today.ToString("yyyy/MM/dd"));
string body = String.Empty;
Utils.SendMail(to, from, subject, body, new string[0] { }, new string[0] { }, zipname);
string filepath = fileToZip.Substring(0,fileToZip.LastIndexOf('\\')+ 1);
string filebackyesterday = String.Format("{0}{1}.bak", filepath, DateTime.Today.AddDays(-1).ToString("yyyy-MM-dd"));
string filezipyesterday = String.Format("{0}{1}.bak.zip", filepath, DateTime.Today.AddDays(-1).ToString("yyyy-MM-dd"));
if (File.Exists(filebackyesterday))
File.Delete(filebackyesterday);
if (File.Exists(filezipyesterday))
File.Delete(filezipyesterday);

}
result = true;
}
catch (Exception ex)
{

string err = ex.Message;
System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress("ae.solutions.inc@gmail.com");
System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("support@leonni.com");
string subject = "Leonni-Application Error";
Utils.SendMail(to, from, subject, err, new string[0], new string[0], true);
return result;
}
return result;
}
}

Aristotle
04-06-2009, 05:34 PM
What exception error are you getting?

javier1981
04-07-2009, 06:49 AM
I can see that this web method store the time of last call(succeeded or failed) when it would store the time of last succeeded call, i suggest add a new web method that return (the remaining time or bool value) to do a successful call to the web method Sql2005CreateBackup.
Kind Regards,
javier1981