Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions src/SIM.Pipelines/Backup/Backup9Args.cs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic of this class is similar to BackupArgs. Can we extend BackupArgs instead of creating this new class or inherit it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not use regions (#region and #endregion) since they are considered as anti-patterns.
I would suggest removing them from each class.

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
namespace SIM.Pipelines.Backup
{
using SIM.Instances;
using SIM.Pipelines.Processors;
using Sitecore.Diagnostics.Base;
using JetBrains.Annotations;
using SIM.Extensions;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;

public class Backup9Args : ProcessorArgs
{
#region Fields

public bool BackupClient { get; }

public bool BackupDatabases { get; }

public bool BackupFiles { get; }

public string BackupName { get; }

[NotNull]
public string Folder { get; }

public Instance Instance { get; }
private string _instanceName { get; }
public string _WebRootPath;
public SqlConnectionStringBuilder ConnectionString { get; }

public readonly ICollection<string> _SelectedDatabases;

#endregion

#region Constructors

public Backup9Args([NotNull] Instance instance, SqlConnectionStringBuilder connectionString,
string backupName = null,
bool backupFiles = false, bool backupClient = false,
bool backupDatabases = false, IEnumerable<string> selectedDatabases = null)
{
Assert.ArgumentNotNull(instance, nameof(instance));
Assert.ArgumentNotNull(connectionString, nameof(connectionString));

Instance = instance;
_instanceName = Instance.Name;

_WebRootPath = instance.WebRootPath;
BackupName = backupName;
Folder = BackupName != null
? FileSystem.FileSystem.Local.Directory.Ensure(instance.GetBackupFolder(BackupName))
: string.Empty;

BackupFiles = backupFiles;
BackupClient = backupClient;

ConnectionString = connectionString;
BackupDatabases = backupDatabases;
_SelectedDatabases = selectedDatabases.With(x => x.Select(y => y.ToLower()).ToArray());
}

#endregion

#region Public properties

public string InstanceName
{
get
{
return _instanceName;
}
}

#endregion
}
}
54 changes: 54 additions & 0 deletions src/SIM.Pipelines/Backup/Backup9Processor.cs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic of this class is same as in BackupProcessor, but only type of args is different. Can we update BackupProcessor to use an interface for args?

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
namespace SIM.Pipelines.Backup
{
using SIM.Pipelines.Processors;
using Sitecore.Diagnostics.Base;
using JetBrains.Annotations;

public abstract class Backup9Processor : Processor
{
#region Methods

#region Public methods

public override sealed long EvaluateStepsCount(ProcessorArgs args)
{
return EvaluateStepsCount((BackupArgs)args);
}

public override bool IsRequireProcessing(ProcessorArgs args)
{
Assert.ArgumentNotNull(args, nameof(args));

return IsRequireProcessing((Backup9Args)args);
}

#endregion

#region Protected methods

protected virtual long EvaluateStepsCount(Backup9Args args)
{
return 1;
}

protected virtual bool IsRequireProcessing([NotNull] Backup9Args args)
{
Assert.ArgumentNotNull(args, nameof(args));

return true;
}

protected override void Process(ProcessorArgs args)
{
Assert.ArgumentNotNull(args, nameof(args));

Process((Backup9Args)args);
}

protected abstract void Process([NotNull] Backup9Args args);

#endregion

#endregion
}
}
75 changes: 75 additions & 0 deletions src/SIM.Pipelines/Backup/BackupDatabases9.cs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic of this class is similar to BackupDatabases. Can we extend BackupDatabases instead of creating this new class or inherit it?

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
namespace SIM.Pipelines.Backup
{
using System.IO;
using System.Linq;
using SIM.Adapters.SqlServer;
using Sitecore.Diagnostics.Base;
using JetBrains.Annotations;
using Sitecore.Diagnostics.Logging;
using System.Data.SqlClient;

[UsedImplicitly]
public class BackupDatabases9 : Backup9Processor
{
#region Methods

#region Protected methods

protected override long EvaluateStepsCount(Backup9Args args)
{
Assert.ArgumentNotNull(args, nameof(args));

return args.Instance.AttachedDatabases.Count();
}

protected override bool IsRequireProcessing(Backup9Args args)
{
Assert.ArgumentNotNull(args, nameof(args));

return args.BackupDatabases;
}

protected override void Process([NotNull] Backup9Args args)
{
Assert.ArgumentNotNull(args, nameof(args));

var selectedDatabases = args._SelectedDatabases;
var attachedDatabases = args.Instance.AttachedDatabases;
var backupDatabasesFolder = FileSystem.FileSystem.Local.Directory.Ensure(Path.Combine(args.Folder, "Databases"));

foreach (var database in attachedDatabases.Where(database => selectedDatabases.Contains(database.Name.ToLower())))
{
Backup(database, backupDatabasesFolder, args.ConnectionString);
IncrementProgress();
}
}

#endregion

#region Private methods

private void Backup([NotNull] Database database, [NotNull] string folder, [NotNull] SqlConnectionStringBuilder connectionString)
{
Assert.ArgumentNotNull(database, nameof(database));
Assert.ArgumentNotNull(folder, nameof(folder));
Assert.ArgumentNotNull(connectionString, nameof(connectionString));

using (var connection = SqlServerManager.Instance.OpenConnection(connectionString, true))
{
var databaseName = database.RealName;
var fileName = Path.Combine(folder, database.BackupFilename);
Log.Info($"Backing up the '{databaseName}' database to the '{fileName}' file");

var command = $"BACKUP DATABASE [{databaseName}] TO " +
$"DISK = N\'{fileName}\' WITH NOFORMAT, NOINIT, " +
$"NAME = N\'{databaseName} initial backup\', SKIP, NOREWIND, NOUNLOAD, STATS = 10";

SqlServerManager.Instance.Execute(connection, command);
}
}

#endregion

#endregion
}
}
69 changes: 69 additions & 0 deletions src/SIM.Pipelines/Backup/BackupFiles9.cs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic of this class is same as in BackupFiles, but only type of args is different. Can we update BackupFiles to use an interface for args?

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
namespace SIM.Pipelines.Backup
{
using System.IO;
using Sitecore.Diagnostics.Base;
using JetBrains.Annotations;

#region

#endregion

[UsedImplicitly]
public class BackupFiles9 : Backup9Processor
{
#region Protected methods

protected override long EvaluateStepsCount(Backup9Args args)
{
Assert.ArgumentNotNull(args, nameof(args));

return 2;
}

protected override bool IsRequireProcessing(Backup9Args args)
{
Assert.ArgumentNotNull(args, nameof(args));

return args.BackupFiles;
}

protected override void Process([NotNull] Backup9Args args)
{
Assert.ArgumentNotNull(args, nameof(args));

var instance = args.Instance;
var webRootPath = instance.WebRootPath;
if (args.BackupClient)
{
BackupFolder(args, webRootPath, "WebRoot.zip");
}
else
{
BackupFolder(args, webRootPath, "WebRootNoClient.zip", "sitecore");
}

IncrementProgress();
BackupFolder(args, instance.DataFolderPath, "Data.zip");
}


#endregion

#region Private methods

private void BackupFolder([NotNull] Backup9Args args, [NotNull] string path, [NotNull] string fileName, string ignore = null)
{
Assert.ArgumentNotNull(args, nameof(args));
Assert.ArgumentNotNull(path, nameof(path));
Assert.ArgumentNotNull(fileName, nameof(fileName));

if (FileSystem.FileSystem.Local.Directory.Exists(path))
{
var backupfile = Path.Combine(args.Folder, fileName);
FileSystem.FileSystem.Local.Zip.CreateZip(path, backupfile, ignore);
}
}

#endregion
}
}
55 changes: 55 additions & 0 deletions src/SIM.Pipelines/ClearBackups/ClearBackupsArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
namespace SIM.Pipelines.ClearBackups
{
#region

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using SIM.Adapters.MongoDb;
using SIM.Adapters.SqlServer;
using SIM.Instances;
using SIM.Pipelines.Processors;
using Sitecore.Diagnostics.Base;
using JetBrains.Annotations;

#endregion

public class ClearBackupsArgs : ProcessorArgs
{
#region Fields

public Instance Instance { get; }
public IEnumerable<string> SelectedBackups { get; }
#endregion

#region Constructors

public ClearBackupsArgs([NotNull] Instance instance, IEnumerable<string> backups=null)
{
SelectedBackups = backups;
Instance = instance;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove additional empty line


public void Initialize()
{
InstanceID = Instance.ID;
InstanceBackupsFolder = Instance.BackupsFolder;
}

#endregion

#region Properties
public string InstanceBackupsFolder { get; set; }

#endregion

#region Public properties

public string InstanceName { get; private set; }

public long InstanceID { get; private set; }

#endregion
}
}
Loading