MySQL数据库备份从服务器A上的一个数据库到服务器b上的另一个数据库

时间:2016-11-18 11:31:23

标签: c# mysql .net import export

我正在尝试导出服务器A上的所有数据库,并将其放入其他数据库(尚未创建)到服务器B上。

我正在尝试使用MySqlBackup .NET,因为我试图在C#中创建自己的Windows服务以在预定时间运行,因为没有时间让每天有人点击按钮来备份数据库并导出到另一个数据库中。

到目前为止,我有点难过:

public partial class BackUpService : ServiceBase
{
    System.Timers.Timer _timer = null;
    bool backUpRunning = false;
    public BackUpService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        BackUpLog.WriteEntry("Database Backup has begun.");
        _timer = new System.Timers.Timer { Interval = 3600000 };
        _timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
        _timer.Start();

    }

    protected override void OnStop()
    {
        _timer.Stop();
        BackUpLog.WriteEntry("Database Backup has finished.");
    }

    public void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        TimeSpan start = new TimeSpan(3, 0, 0);
        TimeSpan end = new TimeSpan(5, 0, 0);
        TimeSpan now = DateTime.Now.TimeOfDay;

        BackUpLog.WriteEntry("Checking if it's time to run the archive service and checking if the service is already running");


        if (backUpRunning)
            BackUpLog.WriteEntry("Service is already running");

        if ((now > start) && (now < end) && !backUpRunning)
        {
            BackUpLog.WriteEntry("Service is not already running and the time of day is valid to run the service.");
            try
            {
                backUpRunning = true;
                BackUpLog.WriteEntry("Initialising archive service");

            }
            catch (Exception ex)
            {
                logArchiveServiceError("The archive service failed. It will return to step 1 and try again: " + ex.Message);
            }
            finally
            {
                backUpRunning = false;
            }
        }

    }
    private void logArchiveServiceError(string errorMessage)
    {
        BackUpLog.WriteEntry(errorMessage, EventLogEntryType.Error);
        Emailer.SendErrorAlertEmail("Archive Service Error", errorMessage, null, null);
    }

    private void logArchiveServiceInfo(string info)
    {
        BackUpLog.WriteEntry(info, EventLogEntryType.Information);
    }

    //EXPORT DATABASES
    private void Backup()
    {
        string sqlDumpFile;
        string con ;
        MySqlBackup backup = new MySqlBackup(con);
        backup.Export(sqlDumpFile);

    }

    //IMPORT DATABASES
    private void Restore()
    {
        string sqlDumpFile;
        string con;
        MySqlBackup backup = new MySqlBackup(con);
        backup.Import(sqlDumpFile);
    }

}

然后我需要处理任何错误或通知,以便他们将自动电子邮件发送到工作电子邮件。

0 个答案:

没有答案