/// <summary> /// Do the backing up of the discovered files /// </summary> /// <param name="archiveDir">A directory to backup to</param> static public void CopyFiles(string archiveDir = null) { var settings = SettingsManager.GetSettings(); var tempDir = GetTempDirectory(); var filesName = Path.Combine(tempDir, DiscoveredFileName); if (!File.Exists(filesName)) { BackupWarning?.Invoke("No new or modified files were discovered"); return; // Nothing to do } var files = File.ReadAllLines(filesName); if (files.Length == 0) { BackupWarning?.Invoke("No new or modified files were discovered"); return; // Nothing to do } if (archiveDir == null) { archiveDir = settings.FileSystemDirectory; } // Has user set an archive folder? if (String.IsNullOrEmpty(archiveDir)) { archiveDir = Path.Combine(tempDir, ArchiveFolder); } // Make the archive var fileSource = DoCopy(archiveDir, files); // Upload the zip archive if (settings.IsS3BucketEnabled) { UploadS3Archive(settings, fileSource.ZipFileArchive); BackupSuccess?.Invoke($"Backup uploaded to S3 Bucket {settings.AWSS3Bucket}"); // Clean up if the user didn't want the File System option if (!settings.IsFileSystemEnabled) { File.Delete(fileSource.ZipFileArchive); } } if (settings.IsGlacierEnabled) { UploadGlacierArchive(settings, fileSource.ZipFileArchive); BackupSuccess?.Invoke($"Backup uploaded to Glacier Vault {settings.AWSGlacierVault}"); // Clean up if the user didn't want the File System option if (!settings.IsFileSystemEnabled) { File.Delete(fileSource.ZipFileArchive); } } try { // Clean up source directory created for zip archive DeleteZipSource(fileSource.UniqueArchiveDir); } catch (Exception ex) { Debug.WriteLine(ex.Message); BackupWarning?.Invoke($"An error occurred removing the temporary archive files: {fileSource.UniqueArchiveDir}"); } }
/// <summary> /// Copy files to the given directory and return the zip file name /// </summary> private static FileSource DoCopy(string archiveDir, string[] files) { var fileSource = new FileSource { UniqueArchiveDir = GetArchiveUniqueName(archiveDir) }; foreach (var line in files) { try { // Copy file and append to processing file var filepath = (from f in line.Split(',') select f.Trim()).First(); var subpath = (from f in line.Split(',') select f.Trim()).Last(); // Create the subpath in archive var splits = subpath.Split('\\'); var subpathDir = ""; for (int i = 0; i < splits.Length - 1; i++) { subpathDir += "\\" + splits[i]; } var newDir = Path.Combine(fileSource.UniqueArchiveDir, subpathDir.TrimStart('\\')); var di = Directory.CreateDirectory(newDir); //Debug.Write(di); try { var dest = Path.Combine(fileSource.UniqueArchiveDir, subpath.TrimStart('\\')); File.Copy(filepath, dest, true); File.SetAttributes(dest, FileAttributes.Normal); } catch (UnauthorizedAccessException ex) { Debug.WriteLine("UnauthorizedAccessException: " + ex.Message); BackupWarning?.Invoke($"Unauthorized access error for {filepath}. File will be skipped."); } catch (IOException io) { Debug.WriteLine("IOException: " + io.Message); BackupWarning?.Invoke($"IO Error for {filepath}. File will be skipped."); } fileSource.FileCount++; } catch (Exception ex) { Debug.WriteLine(ex.Message); throw ex; } } if (fileSource.FileCount > 0) { try { ZipFile.CreateFromDirectory(fileSource.UniqueArchiveDir, fileSource.ZipFileArchive, CompressionLevel.Optimal, false); // Clean up folders used to make the zip archive //DeleteZipSource(fileSource.UniqueArchiveDir); BackupSuccess?.Invoke($"{fileSource.FileCount} file {(fileSource.FileCount == 1 ? String.Empty : "s")} copied to {fileSource.ZipFileArchive}"); return(fileSource); } catch (Exception ex) { Debug.WriteLine(ex.Message); throw ex; } } return(null); }