Skip to content

Instantly share code, notes, and snippets.

@jaysonrowe
Created September 18, 2012 01:23
Show Gist options
  • Save jaysonrowe/3740737 to your computer and use it in GitHub Desktop.
Save jaysonrowe/3740737 to your computer and use it in GitHub Desktop.
ASRService.cs
using System;
using System.Configuration;
using System.IO;
using System.Security.Permissions;
using System.Data;
using System.Text;
using System.Diagnostics;
using System.ComponentModel;
using System.ServiceProcess;
using System.Collections.Generic;
using Microsoft.SqlServer.Management.Smo;
namespace AutoSQLRestore
{
public partial class ASR : ServiceBase
{
static string watcherpath = System.Configuration.ConfigurationManager.AppSettings["watcherpath"];
static string dbpath = System.Configuration.ConfigurationManager.AppSettings["dbpath"];
static string sqlserver = System.Configuration.ConfigurationManager.AppSettings["sqlserver"];
static string log = System.Configuration.ConfigurationManager.AppSettings["log"];
static StreamWriter str = new StreamWriter(log, true);
static FileSystemWatcher watcher = new FileSystemWatcher();
public ASR()
{
InitializeComponent();
if (!EventLog.SourceExists("ASR"))
{
EventLog.CreateEventSource("ASR", "ASR");
}
eventLog1.Source = "ASR";
eventLog1.Log = "";
}
private void eventLog1_EntryWritten(object sender, EntryWrittenEventArgs e)
{
}
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("ASR Service Started");
try
{
watcher.Path = watcherpath;
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = "*.bak";
watcher.Filter = "*.tmp";
watcher.Renamed += new RenamedEventHandler(restoredb);
watcher.EnableRaisingEvents = true;
}
catch (FailedOperationException)
{
eventLog1.WriteEntry("INFO: Aborted");
}
}
protected override void OnStop()
{
watcher.Dispose();
eventLog1.WriteEntry("ASR Service Stopped");
str.Close();
}
private static void restoredb(object source, RenamedEventArgs e)
{
Server svr = new Server(sqlserver);
Restore res = new Restore();
string dbname = DateTime.Now.Millisecond.ToString(); // only temporary until passing in site number as argument
res.Database = dbname;
res.Action = RestoreActionType.Database;
res.Devices.AddDevice(watcherpath + e.Name, DeviceType.File);
res.ReplaceDatabase = true;
DataTable dt;
DataRow[] foundrows;
dt = res.ReadFileList(svr);
foundrows = dt.Select();
string mdfname = foundrows[0]["LogicalName"].ToString();
string ldfname = foundrows[1]["LogicalName"].ToString();
res.RelocateFiles.Add(new RelocateFile(mdfname, dbpath + dbname + ".mdf"));
res.RelocateFiles.Add(new RelocateFile(ldfname, dbpath + dbname + "_log" + ".ldf"));
try
{
res.SqlRestore(svr);
}
catch (FailedOperationException fox)
{
str.WriteLine("{0} Exception caught.", fox.Message);
}
catch (Exception x)
{
str.WriteLine("{0} Exception caught.", x);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment