private void SpinUpBackupDrive() { try { var available = options.IsBackupDestinationAvailable(); if (available) { var fi = new FileInfo(options.GetBackupDestination()); lastBackupDrive = fi.Directory.Root.Name; } else if (lastBackupDrive != null) { var folder = Path.Combine(lastBackupDrive, options.BackupFolder); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); log.LogInformation($"{folder} created"); } var filename = Path.Combine(folder, "probe.txt"); if (File.Exists(filename)) { File.Delete(filename); } File.CreateText(filename); File.Delete(filename); } } catch (Exception) { } }
public Task <IActionResult> GetBackupDestination() { string volumeLabel = this.serviceOptions.BackupDriveLabel; string destinationFolder = string.Empty; var r = serviceOptions.IsBackupDestinationAvailable(); if (r) { destinationFolder = serviceOptions.GetBackupDestination(); } return(Task.FromResult(SuccessDataResult(new { volumeLabel = volumeLabel, available = r, destination = destinationFolder }))); }
private async Task <ITaskState> DoTask(ITaskState taskState, ScheduleMode mode, CancellationToken cancellationToken) { if (options.IsBackupDestinationAvailable()) { using (db = dbf.GetWebDbContext <ServiceDb>()) { var sf = await db.SourceFolders .Include(x => x.Backups) .SingleAsync(x => x.Id == sourceFolderId); //log.LogInformation($"Backup task for source {sf.DisplayName}"); var destinationFolder = Path.Combine(options.GetBackupDestination(), sf.DisplayName); if (!Directory.Exists(destinationFolder)) { Directory.CreateDirectory(destinationFolder); log.LogInformation($"{destinationFolder} created"); } var isPending = await IsBackupPending(sf); if (isPending.result) { //log.LogInformation($"Backup of {sf.GetFullname()} to {destinationFolder} is required"); var backup = isPending.backup; var namePart = sf.DisplayName; var datePart = $"{(backup.ScheduledOn.ToString("yyyy.MM.dd"))}"; var backupFileName = $"{namePart}.{datePart}.zip"; backup.FullPath = Path.Combine(destinationFolder, backupFileName); backup.State = BackupState.Started; var now = DateTimeOffset.Now; var todaysScheduledTime = new DateTimeOffset(now.Year, now.Month, now.Day, sf.ScheduledTime, 0, 0, now.Offset); log.LogInformation($"Backup of {sf.DisplayName} to {destinationFolder} started ({(todaysScheduledTime.ToString("ddMMMyyyy HH:mm:ss"))})"); if (sf.Type == SourceType.Website) { TakeSiteOffline(sf); } await db.SaveChangesAsync(); try { if (File.Exists(backup.FullPath)) { File.Delete(backup.FullPath); log.LogWarning($"{backup.FullPath} deleted"); } zip(sf.FullPath, backup.FullPath); backup.State = BackupState.Finished; backup.BackedUpOn = DateTimeOffset.Now; await db.SaveChangesAsync(); log.LogInformation($"Backup of {sf.DisplayName} to {backup.FullPath} completed"); } catch (Exception xe) { log.LogError(xe, $"backup failed {sf.DisplayName} to {backup.FullPath}"); backup.State = BackupState.Failed; backup.BackedUpOn = DateTimeOffset.Now; await db.SaveChangesAsync(); //throw; } finally { if (sf.Type == SourceType.Website) { BringSiteOnline(sf); } } await PurgeBackups(sf); } else { log.LogInformation($"Backup of {sf.DisplayName} is not pending"); } } } else { foreach (var d in DriveInfo.GetDrives()) { log.LogInformation($"Found drive {d.Name}, label {d.VolumeLabel}, ready = {d.IsReady}"); } log.LogWarning($"Backup destination not available - no disk with volume label {options.BackupDriveLabel} found"); } return(null); }