Scheduled backup of your site

by: Jacob Khan

I wanted to make a tool that would help me backup my content. I decided to make a scheduled service that would export my content from EPiServer and put it with the code. I could then save it in the TFS with the code.

After having a little chat with Fredrik Tjärnberg I got some example code of how an export can be done through code.

Here is the Execute method in the scheduled job

   1: public static string Execute()
   2:       {
   3:           try
   4:           {
   5:               DataExporter exporter = new DataExporter();
   6: 
   7:               PageReference pf = new PageReference(MySettings.Instance.PageID);
   8:               PageData rootPageData = DataFactory.Instance.GetPage(pf, LanguageSelector.MasterLanguage());
   9: 
  10: 
  11: 
  12:               if (MySettings.Instance.Overwrite)
  13:               {
  14:                   CleanFolder(MySettings.Instance.Path);
  15:               }
  16: 
  17:               string path = MySettings.Instance.Path + "ScheduledExporterDump_" + DateTime.Now.ToLongTimeString().Replace(':', '_') + ".episerverdata";
  18: 
  19:               exporter.PageTypes.AddRange(PageType.List());
  20:               exporter.Frames.AddRange(Frame.List());
  21:               exporter.TabDefinitions.AddRange(TabDefinition.List());
  22:               exporter.DynamicPropertyDefinitions.AddRange(PageDefinition.ListDynamic());
  23:               exporter.Categories.AddRange(Category.GetRoot().Categories);
  24: 
  25:               ExportSource sourceRoot = new ExportSource(PageReference.RootPage, EPiServer.Core.Transfer.ExportSource.RecursiveLevelInfinity);
  26:               exporter.SourceRoots.Add(sourceRoot);
  27: 
  28:               exporter.Stream = new FileStream(path , FileMode.Create, FileAccess.Write | FileAccess.Read, FileShare.Read);
  29:               exporter.Export();
  30:               exporter.Close();
  31:               if (exporter.Log.Errors.Count > 0)
  32:               {
  33:                   throw new Exception("errors occured,try &debug=true");
  34:               }
  35: 
  36:               return "Success - finnished with no errors";
  37:           }
  38: 
  39:           catch (Exception err)
  40:           {
  41: 
  42:               return "error - " + err.Message;
  43:           }
  44: 
  45:       }

After that I hade the issue that I wanted some settings like start page and a checkbox saying if it should overwrite the content or not. I couldn't figure out how to override the scheduled job page in a nice way and instead made everything into an admin plugin and more or less just do my custom scheduled job settings.

Here is what I do when I save the page:

   1: 
   2:         public void save_click(object sender, EventArgs e)
   3:         {
   4:             _job = ScheduledJob.Load("Execute", "EPiServer.Export", "ScheduledExport");
   5: 
   6:             if (_job == null)
   7:             {
   8:                 _job = new ScheduledJob();
   9:             }
  10: 
  11:             _job.Name = "ExportContent";
  12: 
  13: 
  14:             // save job config
  15:             _job.IsEnabled = active.Checked;
  16: 
  17:             _job.IntervalType = (ScheduledIntervalType)int.Parse(everywhat.SelectedItem.Value);
  18:             _job.IntervalLength = int.Parse(runevery.Text);
  19: 
  20:             DateTime nextExecution;
  21: 
  22:             if (DateTime.MinValue != attachdate.Value)
  23:                 nextExecution = attachdate.Value;
  24:             else
  25:                 nextExecution = DateTime.Now.AddDays(1).Date.AddHours(2);
  26: 
  27:             _job.NextExecution = nextExecution;
  28: 
  29:             _job.MethodName = "Execute";
  30:             _job.IsStaticMethod = true;
  31:             _job.AssemblyName = "ScheduledExport";
  32:             _job.TypeName = "EPiServer.Export";
  33: 
  34: 
  35: 
  36:             //Save ettings
  37:             MySettings.Instance.Save(PageRoot.PageLink.ID.ToString(), path.Text, overwrite.Checked);
  38:             // save us to db
  39:             _job.Save();
  40:             unregister.Visible = true;
  41:             unregistered.Visible = false;
  42:
  43:             //Testing pruposes
  44:           //  Export.Execute();
  45:         }
  46: 

When a scheduled job is saved you need to set some variables such as what method to call, how often, when should it start and so on. It is then registered and called at the selected times. Another issue here was how do you uninstall this. Well first thing I did was make it into a single DLL with some help from Johan Olofsson. But even if you remove the DLL it is not uninstalled as the job is still registered. So I also added an uninstall button which more or less deletes the job and you can then remove the DLL.

image

There is also an option to overwrite old backups or to always save it as a new file. Remember that the folder you decide to save it to needs to have the correct access rights.

Enjoy

 

Filename

>> DLLs.zip 17Kb 2009-08-25
>> ScheduledExport.zip 57Kb 2009-08-25


Enter your information and an email is sent to you



Name  
Email  
Company  

/Jacob

25 August 2009

Tags:


    Comments

    1. Nice post. This scheduled task should be included in EPiServer
    2. An alternative is to implement the functionality as a workflow instead. With that approach you can both get/implement the scheduled functionality and a custom seetings page.
    3. Thanks Johan, great alternative. Although I would rather somehow have a settings tab in the scheduled job page.
    Post a comment    
    User verification Image for user verification  
    Jacob Khan

    About me

    I am a researcher at EPiServer based in Stockholm. I started working in the Research department in May 2008.
    I also blog on world.episerver.com
    See my profile card on world

     

    Syndications


    Archive


    Tag cloud

    EPiTrace logger