BlogEngine.net Simple Backup and Download Widget

by Jeff 17. May 2009 03:22

Updated

I just found ttis and will not have to create a backup utilitiy read the full BlogEngine.NET Backup Manager to learn more

I did modify the backup manager to create a website related file name instead of the quid file names it was using. I used the website name and the date to make it destinct.

I'm thinking moving the backup function to a web service, making it a shared function and run it on it's own thread, moving the code wouldn't take long and would make this feature less likely to timeout during backups. Zipping the entire website does take time that would be safer run on a separet thread.

I also excluded the log directory that godaddy adds to websites, I use google analytics so don't need it 

I did make a couple changes to the BlogEngine.net Backup Manager Code view the following example code to see my changes

using System;
using System.Collections.Generic;
using System.Web;
using System.Collections;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using BlogEngine.Core.Providers;
using BlogEngine.Core;

public class BackupManager : IHttpHandler
{
    public BackupManager()
    {
    }

    public static void Backup()
    {
        String InputPath = HttpContext.Current.Server.MapPath("~/");
        String OutputPath = HttpContext.Current.Server.MapPath(BlogService.GetStorageLocation()) + "Backups";

        ////////////////////////////////////////////////////////////////
        /// Modified the following to create a relevant file name
        ////////////////////////////////////////////////////////////////
        OutputPath = Path.Combine(OutputPath, BlogSettings.Instance.Name + "-" + DateTime.Now.ToUniversalTime().ToString("yyyy-MM-dd") + ".zip");

        ZipFiles(InputPath, OutputPath, null);
    }

    #region Private Static Methods

    private static void ZipFiles(string inputFolderPath, string outputPathAndFile, string password)
    {
        ArrayList ar = GenerateFileList(inputFolderPath);
        int TrimLength = (Directory.GetParent(inputFolderPath)).ToString().Length;
        TrimLength += 1;
        FileStream ostream;
        byte[] obuffer;
        ZipOutputStream oZipStream = new ZipOutputStream(File.Create(outputPathAndFile));
        if (password != null && password != String.Empty)
            oZipStream.Password = password;
        oZipStream.SetLevel(9);
        ZipEntry oZipEntry;
        foreach (string Fil in ar)
        {
            oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength));
            oZipStream.PutNextEntry(oZipEntry);

            if (!Fil.EndsWith(@"/"))
            {
                ostream = File.OpenRead(Fil);
                obuffer = new byte[ostream.Length];
                ostream.Read(obuffer, 0, obuffer.Length);
                oZipStream.Write(obuffer, 0, obuffer.Length);
            }
        }
        oZipStream.Finish();
        oZipStream.Close();
    }
    private static ArrayList GenerateFileList(string Dir)
    {
        ArrayList fils = new ArrayList();
        bool Empty = true;
        foreach (string file in Directory.GetFiles(Dir))
        {
            fils.Add(file);
            Empty = false;
        }

        if (Empty)
        {
            if (Directory.GetDirectories(Dir).Length == 0)
            {
                fils.Add(Dir + @"/");
            }
        }

        foreach (string dirs in Directory.GetDirectories(Dir))
        {
            if (dirs.ToUpperInvariant().EndsWith("STATS\\LOGS"))
                continue;

            if (dirs.ToUpperInvariant().Contains("BACKUPS"))
                continue;
           
            foreach (object obj in GenerateFileList(dirs))
            {
                fils.Add(obj);
            }
        }
        return fils;
    }

    #endregion

    #region IHttpHandler Members

    public bool IsReusable
    {
        get { return false; }
    }

    public void ProcessRequest(HttpContext context)
    {
        HttpResponse Response = HttpContext.Current.Response;
        HttpRequest Request = HttpContext.Current.Request;

        Response.ClearContent();

        // added the following security check
        if (!context.User.Identity.IsAuthenticated)
        {
            Response.ContentType = "text/plain";
            Response.Write("Error 1");

            return;
        }

        Response.ContentType = "application/zip";

        if (!String.IsNullOrEmpty(Request.QueryString["file"]))
        {
            String OutputPath = HttpContext.Current.Server.MapPath(BlogService.GetStorageLocation()) + "Backups";
            String BackupFilePath = OutputPath + "\\" + Request.QueryString["file"];
            if (File.Exists(BackupFilePath))
            {
                Response.AddHeader("
Content-Disposition", "attachment; filename=" + Path.GetFileName(BackupFilePath));
                Response.TransmitFile(BackupFilePath);
            }
        }
    }

    #endregion
}

 

Old section

What I would like is a simple backup widget that would only be displayed when an authenticated user is logged in. The widget would be very simple just archive the data and once complete insert a download link.

Compression References

How to compress folders and multiple files with GZipStream and C# (System.IO.Compression.GZipStream)

Bookmark and Share

Tags:

Asp.net | BlogEngine.net | Widgets

Comments

9/9/2009 2:34:26 PM #

Travel Agent Indonesia

Great share and nice website thanks

Travel Agent Indonesia United States

9/15/2009 8:16:45 AM #

R M

I need a help to create a database backup using asp.net like the manner above but when i tried to apply it in my site i faced some problems:

- I cannot understand these namespaces from which pages ???
{
   using ICSharpCode.SharpZipLib.Zip;
   using BlogEngine.Core.Providers;
   using BlogEngine.Core;
}

- and all related code to these namespaces

Thank You

R M Egypt

9/16/2009 2:31:04 AM #

reborn babies dolls

thanks for a wonderful share. Your article has proved  your hard work and experience you have got in this field. Brilliant .i love it reading.

reborn babies dolls United States

9/25/2009 1:51:21 PM #

Christian Faith

Nice information, many thanks to the author. It is incomprehensible to me now, but in general, the usefulness and significance is overwhelming. Thanks again and good luck!

Christian Faith United States

9/30/2009 1:02:22 AM #

gator host review

.thanks a ton for a awesome share. I can realize that to achieve this you need plenty of potential and hard work .it gets revealed from your article.

gator host review United States

10/3/2009 8:21:15 AM #

Oes Tsetnoc

Nice article... and this post should be bookmarked Laughing

Oes Tsetnoc United Kingdom

10/9/2009 4:35:09 AM #

manunited1991

Well done for you making it possible,you are adding on to your list of program and make it more well managed and a lot of assisting program like this here.It certainly proved to be a great help to those that do not back up their data and will prevent the loss of data on the internet.

manunited1991 Singapore

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET

Code Research Center

©2009 CodeResearchCenter.com. All Rights Reserved

About Me

I'm a 30 year old browser based software developer who has just started to research the various ways to make money online. My current interests are software development, online marketing, social networking and blogging.

Disclaimer

These postings are provided "AS IS" with no warranties, use at your own risk

Page List

Poll

What blog platform do you use?



Show Results