-
Notifications
You must be signed in to change notification settings - Fork 71
#775 | Add the backup functionality for Sitecore 9 and later #793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| } | ||
| } |
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| } | ||
| } |
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| } | ||
| } |
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| } | ||
| } |
| 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; | ||
| } | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.