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)