BackupHelper Documentation
The BackupHelper class provides comprehensive backup and data protection features.
Features
- Automated backups
- File versioning
- Cloud sync
- Compression
- Encryption
- Recovery options
- Differential backups
- Scheduled backups
Dependencies
Methods
CreateBackup
public static async Task CreateBackup(
string sourcePath,
string destinationPath,
BackupOptions options = null)
Creates a backup.
Parameters:
sourcePath: Source directory/filedestinationPath: Backup destinationoptions: Backup options
Returns:
- Backup results
Example:
var result = await BackupHelper.CreateBackup(
@"C:\MyData",
@"D:\Backups",
new BackupOptions
{
Compress = true,
Encrypt = true,
IncludeVersioning = true,
Password = "secure-password"
}
);
SyncToCloud
public static async Task SyncToCloud(
string localPath,
CloudProvider provider,
CloudSyncOptions options)
Syncs to cloud storage.
Parameters:
localPath: Local directoryprovider: Cloud provideroptions: Sync options
Returns:
- Sync results
Example:
var result = await BackupHelper.SyncToCloud(
@"C:\MyData",
CloudProvider.AWS,
new CloudSyncOptions
{
BucketName = "my-backups",
Region = "us-west-2",
Credentials = awsCredentials,
AutoSync = true
}
);
RestoreBackup
public static async Task RestoreBackup(
string backupPath,
string restorePath,
RestoreOptions options = null)
Restores from backup.
Parameters:
backupPath: Backup locationrestorePath: Restore destinationoptions: Restore options
Returns:
- Restore results
Example:
var result = await BackupHelper.RestoreBackup(
@"D:\Backups\MyData_2025-02-05",
@"C:\Restored",
new RestoreOptions
{
Password = "secure-password",
OverwriteExisting = false,
ValidateChecksum = true
}
);
ScheduleBackup
public static async Task ScheduleBackup(
BackupSchedule schedule,
BackupOptions options = null)
Schedules automated backup.
Parameters:
schedule: Backup scheduleoptions: Backup options
Returns:
- Success status
Example:
await BackupHelper.ScheduleBackup(
new BackupSchedule
{
Frequency = BackupFrequency.Daily,
Time = "02:00",
RetentionDays = 30
},
new BackupOptions
{
Compress = true,
Encrypt = true
}
);
Supporting Types
BackupOptions
public class BackupOptions
{
public bool Compress { get; set; }
public bool Encrypt { get; set; }
public bool IncludeVersioning { get; set; }
public string Password { get; set; }
public CompressionLevel Compression { get; set; }
public EncryptionAlgorithm Encryption { get; set; }
public bool IncludeMetadata { get; set; }
}
CloudSyncOptions
public class CloudSyncOptions
{
public string BucketName { get; set; }
public string Region { get; set; }
public ICredentials Credentials { get; set; }
public bool AutoSync { get; set; }
public SyncDirection Direction { get; set; }
public List ExcludePatterns { get; set; }
}
BackupSchedule
public class BackupSchedule
{
public BackupFrequency Frequency { get; set; }
public string Time { get; set; }
public int RetentionDays { get; set; }
public List Days { get; set; }
public bool SkipIfBusy { get; set; }
}
Common Use Cases
- Automated Backup System
public class AutomatedBackup
{
private readonly ILogger _logger;
private readonly BackupConfig _config;
public async Task SetupAutomatedBackup()
{
// Schedule daily backups
await BackupHelper.ScheduleBackup(
new BackupSchedule
{
Frequency = BackupFrequency.Daily,
Time = "02:00",
RetentionDays = 30,
SkipIfBusy = true
},
new BackupOptions
{
Compress = true,
Encrypt = true,
IncludeVersioning = true,
Password = _config.BackupPassword
}
);
// Schedule cloud sync
await BackupHelper.ScheduleCloudSync(
new CloudSyncSchedule
{
Provider = CloudProvider.AWS,
Frequency = SyncFrequency.Daily,
Time = "04:00"
},
new CloudSyncOptions
{
BucketName = _config.BucketName,
Region = _config.Region,
Credentials = _config.AwsCredentials
}
);
}
}
- Version Control System
public class VersionManager
{
private readonly string _backupRoot;
private readonly int _maxVersions;
public async Task CreateNewVersion(
string filePath)
{
var versionInfo = await BackupHelper.CreateBackup(
filePath,
_backupRoot,
new BackupOptions
{
IncludeVersioning = true,
IncludeMetadata = true,
Compress = true
}
);
await CleanupOldVersions(filePath);
return versionInfo.VersionId;
}
public async Task RestoreVersion(
string filePath,
string versionId)
{
return await BackupHelper.RestoreVersion(
filePath,
versionId,
new RestoreOptions
{
ValidateChecksum = true,
RestoreMetadata = true
}
);
}
}
- Multi-Cloud Backup
public class MultiCloudBackup
{
private readonly Dictionary _configs;
public async Task BackupToMultipleProviders(
string sourcePath)
{
var tasks = _configs.Select(async config =>
{
try
{
return await BackupHelper.SyncToCloud(
sourcePath,
config.Key,
new CloudSyncOptions
{
BucketName = config.Value.Bucket,
Region = config.Value.Region,
Credentials = config.Value.Credentials,
AutoSync = true
}
);
}
catch (Exception ex)
{
await LogFailure(config.Key, ex);
return null;
}
});
var results = await Task.WhenAll(tasks);
await ValidateBackups(results);
}
}
Performance Considerations
- Uses parallel processing
- Implements delta compression
- Optimizes network usage
- Uses efficient encryption
- Implements caching
Error Handling
- Validates input paths
- Handles network issues
- Provides retry logic
- Implements checksums
- Includes error logging
Best Practices
- Secure Backup Management
public class SecureBackupManager
{
private readonly ISecretStore _secretStore;
private readonly IEncryptionService _encryption;
public async Task CreateSecureBackup(
string sourcePath)
{
var password = await _secretStore.GetSecret(
"backup-key"
);
var options = new BackupOptions
{
Encrypt = true,
Password = password,
Encryption = EncryptionAlgorithm.AES256,
IncludeMetadata = true
};
var result = await BackupHelper.CreateBackup(
sourcePath,
GetSecureBackupPath(),
options
);
await _encryption.SecureDelete(password);
return result;
}
}
- Intelligent Backup Scheduling
public class BackupScheduler
{
private readonly ISystemMonitor _monitor;
private readonly ILogger _logger;
public async Task ScheduleOptimalBackup()
{
var systemLoad = await _monitor.GetSystemLoad();
var networkBandwidth = await _monitor.GetNetworkBandwidth();
var schedule = new BackupSchedule
{
Frequency = BackupFrequency.Daily,
Time = await FindOptimalTime(systemLoad),
RetentionDays = CalculateRetention(
await GetStorageUsage()
),
SkipIfBusy = true
};
await BackupHelper.ScheduleBackup(
schedule,
GetOptimalOptions(networkBandwidth)
);
}
}
- Recovery Testing
public class RecoveryTester
{
private readonly string _testRestorePath;
private readonly IValidator _validator;
public async Task ValidateBackup(string backupPath)
{
// Perform test restore
var result = await BackupHelper.RestoreBackup(
backupPath,
_testRestorePath,
new RestoreOptions
{
ValidateChecksum = true,
TestOnly = true
}
);
// Validate restored files
foreach (var file in result.RestoredFiles)
{
await _validator.ValidateFile(
file,
ValidationLevel.Strict
);
}
// Clean up test restore
await BackupHelper.SecureCleanup(
_testRestorePath
);
}
}
Legal Disclaimer
This documentation and associated helper scripts are provided "as is" without warranty of any kind, either express or implied.
- The code examples and helper functions are for illustrative purposes only.
- Users should thoroughly test any implementation in their specific environment.
- The authors are not responsible for any issues or damages arising from the use of these scripts.
- Always follow security best practices and your organization's coding guidelines.