I need a c# code sample to do a db backup from control panel API

Discussion in 'Control Panel API' started by javier1981, Apr 6, 2009.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. 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(customerAPI_Sql2005CreateBackupCompleted);
    customerAPI.Sql2005CreateBackupAsync(ConfigurationManager.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("[email protected]");
    System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("[email protected]");
    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("[email protected]");
    System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("[email protected]");
    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("[email protected]");
    System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("[email protected]");
    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("[email protected]");
    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("[email protected]");
    System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("[email protected]");
    string subject = "Leonni-Application Error";
    Utils.SendMail(to, from, subject, err, new string[0], new string[0], true);
    return result;
    }
    return result;
    }
    }
     
  2. What exception error are you getting?
     
  3. The web method Sql2005CreateBackup have a bad implemented logic

    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
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page