Example #1
0
        protected void btnBackup_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    using (ConnectorBase conn = ConnectorBase.NewInstance())
                    {
                        Response.Clear();
                        Response.AddHeader(@"content-disposition", @"attachment;filename=MySqlBackup_" + DateTime.UtcNow.ToString(@"yyyy_MM_dd_HH_mm_ss") + (chkBackupToGzip.Checked ? @".gz" : ".sql"));
                        Response.Charset         = @"UTF-8";
                        Response.ContentEncoding = System.Text.Encoding.UTF8;
                        Response.Cache.SetCacheability(HttpCacheability.NoCache);
                        if (chkBackupToGzip.Checked)
                        {
                            Response.ContentType = @"application/x-gzip";
                        }
                        else
                        {
                            Response.ContentType = @"application/octet-stream";
                        }

                        MySqlBackup.BackupOptions options = new MySqlBackup.BackupOptions();
                        options.BOM = true;
                        options.WrapInTransaction = true;
                        options.ExportTableCreate = true;
                        options.ExportTableDrop   = true;
                        options.ExportTableData   = true;
                        options.ExportRoutines    = true;
                        options.ExportTriggers    = true;

                        if (chkBackupToGzip.Checked)
                        {
                            using (GZipStream gzipStream = new GZipStream(Response.OutputStream, CompressionMode.Compress))
                            {
                                MySqlBackup.GenerateBackup((MySqlConnector)conn, gzipStream, options);
                            }
                        }
                        else
                        {
                            MySqlBackup.GenerateBackup((MySqlConnector)conn, Response.OutputStream, options);
                        }

                        Response.End();
                    }
                }
                catch (DbException ex)
                {
                    Response.ContentType = @"text/html";
                    try
                    {
                        Response.Headers.Remove(@"content-disposition");
                    }
                    catch
                    {
                        Response.ClearHeaders();
                    }
                    Response.Clear();
                    Master.MessageCenter.DisplayErrorMessage(ex.Message);
                }
            }
        }