My simple backup util for SharePoint

 

1: <configuration>

   2:        <appSettings>
   3:          <add key="DiffBackupDays" value="0,1,2,3,4,6"/>
   4:          <add key="FullBackupDays" value="5"/>
   5:          <add key="MonthsToHoldAllBackups" value="3"/>
   6:          <add key="MonthsToHoldLastMonthlyBackups" value="3"/>
   7:          <add key="ArchivalPath" value="\archiveBackups"/>
   8:          <add key="InitalBackupPath" value="\initbackupSHAREPOINT"/>
   9:      </appSettings>
  10:  </configuration>
   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.IO;
   6:  using System.Collections.Specialized;
   7:  using System.Configuration;
   8:  using System.Diagnostics;
   9:     
  10:      public enum BackupMethod {
  11:          Full,
  12:          Differential
  13:      }
  14:   
  15:   
  16:      static class Backup
  17:      {
  18:          
  19:          public static void Main()
  20:          {
  21:              
  22:              NameValueCollection appSettings = ConfigurationManager.AppSettings;
  23:              
  24:              try
  25:              {
  26:                  DirectoryInfo ArchivalPath = new DirectoryInfo(appSettings["ArchivalPath"]);
  27:   
  28:                  DirectoryInfo InitalBackupPath = new DirectoryInfo(appSettings["InitalBackupPath"]);
  29:   
  30:                  DayOfWeek[] diffBackupDays =
  31:                      Array.ConvertAll(
  32:                      appSettings["DiffBackupDays"].Split(','),
  33:                      s => (System.DayOfWeek)Convert.ToInt32(s));
  34:   
  35:                  DayOfWeek[] fullBackupDays =
  36:                      Array.ConvertAll(
  37:                      appSettings["FullBackupDays"].Split(','),
  38:                      s => (System.DayOfWeek)Convert.ToInt32(s));
  39:   
  40:                  var isFullBackup = ((from fbd in fullBackupDays where fbd 
  41:   System.DateTime.Now.DayOfWeek select fbd).Count() > 0);
  42:                  
  43:                  var isDiffBackup = ((from dbd in diffBackupDays where dbd == 
  44:  System.DateTime.Now.DayOfWeek select dbd).Count() > 0);
  45:                  
  46:                  int MonthsToHoldAllBackups = 
  47:  int.Parse(appSettings["MonthsToHoldAllBackups"]);
  48:                  
  49:                  int MonthsToHoldLastMonthlyBackups = 
  50:  int.Parse(appSettings["MonthsToHoldLastMonthlyBackups"]);
  51:   
  52:                  if (isFullBackup && isDiffBackup)
  53:                  {
  54:                      throw new ArgumentOutOfRangeException(
  55:  "You cannot perform both a full backup and a diff backup onthe same day!");
  56:                  }
  57:                  if (isFullBackup || isDiffBackup)
  58:                  {
  59:                      if (isFullBackup)
  60:                      {
  61:                          Cleanup(MonthsToHoldAllBackups, MonthsToHoldLastMonthlyBackups,
  62:   ArchivalPath, InitalBackupPath);
  63:                          Backup(InitalBackupPath.FullName, BackupMethod.Full);
  64:                      }
  65:                      else
  66:                      {
  67:                          Backup(InitalBackupPath.FullName, BackupMethod.Differential);
  68:                      }
  69:       
  70:                  }
  71:   
  72:              }
  73:              catch (Exception ex)
  74:              {
  75:                  
  76:              }
  77:          }
  78:   
  79:          static void Cleanup(int MonthsToHoldAllBackups, int MonthsToHoldLastMonthlyBackups, 
  80:  DirectoryInfo ArchivalPath, DirectoryInfo InitalBackupPath)
  81:          {
  82:              DateTime comp = DateTime.Now;
  83:              //First get all directories from archive path
  84:              DirectoryInfo[] di = ArchivalPath.GetDirectories();
  85:              foreach (var d in di)
  86:              {
  87:                  DateTime dt = DateTime.Parse(d.Name);
  88:                  if (dt.AddMonths(MonthsToHoldAllBackups + MonthsToHoldLastMonthlyBackups) < comp)
  89:                  {
  90:       
  91:                      d.DelTree();
  92:   
  93:                      continue;
  94:                  }
  95:                  if (dt.AddMonths(MonthsToHoldAllBackups) < comp)
  96:                  {
  97:                      if (dt.AddDays(7).Month == dt.Month)
  98:                      {
  99:       
 100:                          d.DelTree();
 101:   
 102:                          continue;
 103:   
 104:                      }
 105:                      else
 106:                      {
 107:   
 108:                          continue;
 109:                      }
 110:   
 111:   
 112:                  }
 113:   
 114:   
 115:              }
 116:   
 117:              //copy all files from current backup to new folder in archive
 118:              var nuDir = Path.Combine(ArchivalPath.Name  , DateTime.Now.ToString("yyyy-MM-dd"));
 119:       
 120:              InitalBackupPath.MoveTo(nuDir);
 121:   
 122:          }
 123:   
 124:          static void Backup(string destinationPath, BackupMethod backupmethod)
 125:          {
 126:              string arguments = string.Format("-o backup -directory "{0}" -backupmethod {1}", 
 127:  destinationPath,   backupmethod);
 128:              ProcessStartInfo startInfo = new ProcessStartInfo(
 129:  @"c:Program FilesCommon FilesMicrosoft Sharedweb server extensions12BINstsadm.exe",
 130:                      arguments);
 131:              startInfo.UseShellExecute = false;
 132:              startInfo.RedirectStandardError = true;
 133:       
 134:              Process stsadm = Process.Start(startInfo);
 135:             
 136:              stsadm.WaitForExit();
 137:       
 138:              if (stsadm.ExitCode != 0)
 139:              {
 140:                  string errorMessage = stsadm.StandardError.ReadToEnd();
 141:                  throw new Exception(errorMessage);
 142:              }
 143:          }
 144:   
 145:          
 146:   
 147:          public static void DelTree(this DirectoryInfo directoryInfo)
 148:          {
 149:              foreach (FileInfo file in directoryInfo.GetFiles())
 150:              {
 151:                  file.Delete();
 152:              }
 153:              foreach (DirectoryInfo subDirectory in directoryInfo.GetDirectories())
 154:              {
 155:                  DelTree(subDirectory);
 156:              }
 157:              directoryInfo.Delete();
 158:          }
 159:   
 160:   
 161:   
 162:      }
 163:   
 164:   
 165:   
 166:   
 167:   

Nothing too fancy.  Just better then any powerscript I was able to come up with.   

This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a comment