Seperated Zip steps into seperate functions && Added file comparison && Now using /tmp for file operations && Experimental color output && Added extra file and directory logic
Seperated Pacman Database into it's own function so that when the function is called two times it doesn't actually do it. Added a way to compare two files by reading every byte and then checking if it's the same or not. For convenience sake the /tmp directory will be used for all temporary file operations like compressing. Color output is going great. Looks pretty nice. Before doing anything file related all functions check if the necessary directory exists.
This commit is contained in:
parent
8a0c93172c
commit
eb2b1edf29
21
Program.cs
21
Program.cs
@ -3,12 +3,25 @@
|
||||
string result = Update.copyEverthingBeforeUpdateToBackupLocation();
|
||||
Console.WriteLine(result);
|
||||
|
||||
//string result2 = Update.copyEverthingFromBackupLocationToFinalDestination();
|
||||
//Console.WriteLine(result2);
|
||||
bool pacmanDatabaseResult = Update.zipPacmanDatabase();
|
||||
Console.WriteLine(pacmanDatabaseResult);
|
||||
|
||||
//Console.WriteLine(args.Length);
|
||||
bool result2 = Update.zipAllContentInBackupLocation("pre-backup.zip");
|
||||
Console.WriteLine(result2);
|
||||
|
||||
bool result3 = Update.zipAllContentInBackupLocation();
|
||||
string result3 = Update.copyEverthingAfterUpdateToBackupLocation();
|
||||
Console.WriteLine(result3);
|
||||
|
||||
bool result4 = Update.zipAllContentInBackupLocation("after-backup.zip");
|
||||
if (result4) {
|
||||
Console.WriteLine(result4);
|
||||
} else {
|
||||
|
||||
}
|
||||
|
||||
string result5 = Update.copyEverthingFromBackupLocationToFinalDestination(args[0]); // "/artemis/test/"
|
||||
Console.WriteLine(result5);
|
||||
|
||||
//Console.WriteLine(args[0]);
|
||||
}
|
||||
}
|
@ -52,11 +52,11 @@ Run executable
|
||||
|
||||
- Figure out how to do options/arguments
|
||||
- Backup all necessary files
|
||||
- Shrink size of the executable
|
||||
- Hopefully shrink size of the executable
|
||||
- Potentially speed up file handling
|
||||
- Color output according to state (success = green, failure = red, info = yellow)
|
||||
- Backup pacman database
|
||||
- Compress all files to single archive
|
||||
- Option to change backup location (instead of home)
|
||||
- Keep backups for a configurable amount of days
|
||||
|
||||
## Sites I used to help make this project
|
||||
- [dotnetperls](https://dotnetperls.com)
|
||||
|
134
Update.cs
134
Update.cs
@ -13,6 +13,11 @@ public class Update {
|
||||
}
|
||||
public static string copyEverthingBeforeUpdateToBackupLocation() {
|
||||
string targetPath = getHomePath() + "/backup/uncompressed/";
|
||||
|
||||
if (File.Exists(targetPath + "/pacman-after.txt") && File.Exists(targetPath + "/flatpak-after.txt")) {
|
||||
File.Delete(targetPath + "/pacman-after.txt");
|
||||
File.Delete(targetPath + "/flatpak-after.txt");
|
||||
}
|
||||
|
||||
string[] systemFilesToCopy = {"/etc/fstab", "/etc/makepkg.conf"};
|
||||
List<string> filesToCopy = new List<string>(systemFilesToCopy);
|
||||
@ -24,6 +29,40 @@ public class Update {
|
||||
|
||||
if (!Directory.Exists(targetPath)) {
|
||||
Directory.CreateDirectory(targetPath);
|
||||
copyEverthingBeforeUpdateToBackupLocation();
|
||||
} else {
|
||||
foreach (string file in filesToCopy) {
|
||||
FileInfo info = new FileInfo(file);
|
||||
string destFile = Path.Combine(targetPath, info.Name);
|
||||
File.Copy(info.FullName, destFile, true);
|
||||
}
|
||||
}
|
||||
|
||||
string copiedFiles = string.Join(", ", filesToCopy);
|
||||
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
return $"Copied {copiedFiles} to {targetPath}";
|
||||
}
|
||||
|
||||
public static string copyEverthingAfterUpdateToBackupLocation() {
|
||||
string targetPath = getHomePath() + "/backup/uncompressed/"; // Use /tmp to zip and then move into /backup/compressed/
|
||||
|
||||
if (File.Exists(targetPath + "/pacman-pre.txt") && File.Exists(targetPath + "/flatpak-pre.txt")) {
|
||||
File.Delete(targetPath + "/pacman-pre.txt");
|
||||
File.Delete(targetPath + "/flatpak-pre.txt");
|
||||
File.Delete(targetPath + "/fstab");
|
||||
File.Delete(targetPath + "/makepkg.conf");
|
||||
}
|
||||
|
||||
List<string> filesToCopy = new List<string>();
|
||||
string pacmanPackageListBeforeUpdate = getHomePath() + "/pacman-after.txt";
|
||||
filesToCopy.Add(pacmanPackageListBeforeUpdate);
|
||||
string flatpakListBeforeUpdate = getHomePath() + "/flatpak-after.txt";
|
||||
filesToCopy.Add(flatpakListBeforeUpdate);
|
||||
|
||||
if (!Directory.Exists(targetPath)) {
|
||||
Directory.CreateDirectory(targetPath);
|
||||
copyEverthingAfterUpdateToBackupLocation();
|
||||
} else {
|
||||
foreach (string file in filesToCopy) {
|
||||
FileInfo info = new FileInfo(file);
|
||||
@ -41,8 +80,8 @@ public class Update {
|
||||
/// <summary>
|
||||
/// Method <c>copyEverthingFromBackupLocationToFinalDestination</c> copies everything to second Backup Location which should be a external drive or a network share. Offsite/Second Backup.
|
||||
/// </summary>
|
||||
public static string copyEverthingFromBackupLocationToFinalDestination() {
|
||||
string targetPath = "/artemis/test/";
|
||||
public static string copyEverthingFromBackupLocationToFinalDestination(string finalBackupLocation) {
|
||||
string targetPath = finalBackupLocation;
|
||||
|
||||
if (!Directory.Exists(targetPath)) {
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
@ -50,7 +89,7 @@ public class Update {
|
||||
}
|
||||
|
||||
if (targetPath is not null) {
|
||||
string sourcePath = getHomePath() + "/backup/";
|
||||
string sourcePath = getHomePath() + "/backup/compressed/";
|
||||
string[] intermediateBackupLocation = Directory.GetFiles(sourcePath);
|
||||
|
||||
if (!Directory.Exists(targetPath)) {
|
||||
@ -70,23 +109,96 @@ public class Update {
|
||||
}
|
||||
}
|
||||
|
||||
public static bool zipAllContentInBackupLocation() {
|
||||
public static bool zipAllContentInBackupLocation(string finalZipName) {
|
||||
string targetPath = getHomePath() + "/backup/compressed/";
|
||||
Directory.CreateDirectory(targetPath);
|
||||
|
||||
string sourcePath = getHomePath() + "/backup/uncompressed/";
|
||||
string targetZip = getHomePath() + "/backup/compressed/backup.zip";
|
||||
ZipFile.CreateFromDirectory(sourcePath, targetZip);
|
||||
string sourcePath = getHomePath() + "/backup/uncompressed/"; // Moved to /tmp
|
||||
string targetZip = getHomePath() + "/backup/compressed/" + finalZipName;
|
||||
|
||||
string pacmanDatabaseLocation = "/var/lib/pacman/local/";
|
||||
string pacmanDatabaseZip = getHomePath() + "/backup/compressed/pacman_database.zip";
|
||||
ZipFile.CreateFromDirectory(pacmanDatabaseLocation, pacmanDatabaseZip);
|
||||
if (!Directory.Exists("/tmp/backup/")) {
|
||||
Directory.CreateDirectory("/tmp/backup/");
|
||||
}
|
||||
string newFinalZip = "/tmp/backup/" + finalZipName;
|
||||
File.Delete(newFinalZip); // Delete residual zip's in tmp
|
||||
ZipFile.CreateFromDirectory(sourcePath, newFinalZip);
|
||||
|
||||
if (File.Exists(targetZip)) {
|
||||
// ToDo verify if current zip matched old zip. If yes don't create the zip and leave the old one. If it doesn't match delete the old one.
|
||||
if (!checkForIdenticalFile(targetZip, newFinalZip)) {
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine($"{finalZipName} is outdated");
|
||||
File.Delete(targetZip);
|
||||
if (File.Exists(newFinalZip)) {
|
||||
File.Delete(newFinalZip);
|
||||
zipAllContentInBackupLocation(finalZipName);
|
||||
} else {
|
||||
File.Move(newFinalZip, targetZip);
|
||||
}
|
||||
} else {
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine($"{finalZipName} is up to date");
|
||||
}
|
||||
} else {
|
||||
ZipFile.CreateFromDirectory(sourcePath, targetZip);
|
||||
}
|
||||
|
||||
if (File.Exists(targetZip) && File.Exists(pacmanDatabaseZip)) {
|
||||
if (File.Exists(targetZip)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool zipPacmanDatabase() {
|
||||
string pacmanDatabaseLocation = "/var/lib/pacman/local/";
|
||||
string pacmanDatabaseZip = getHomePath() + "/backup/compressed/pacman_database.zip";
|
||||
|
||||
if (!Directory.Exists("/tmp/backup/")) {
|
||||
Directory.CreateDirectory("/tmp/backup/");
|
||||
}
|
||||
string newPacmanDatabaseZip = "/tmp/backup/pacman_database.zip";
|
||||
File.Delete(newPacmanDatabaseZip); // Delete residual Pacman Database in tmp
|
||||
ZipFile.CreateFromDirectory(pacmanDatabaseLocation, newPacmanDatabaseZip);
|
||||
|
||||
if (File.Exists(pacmanDatabaseZip)) {
|
||||
if (!checkForIdenticalFile(pacmanDatabaseZip, newPacmanDatabaseZip)) {
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine("Pacman Database is outdated");
|
||||
File.Delete(pacmanDatabaseZip);
|
||||
if (File.Exists(newPacmanDatabaseZip)) {
|
||||
File.Delete(newPacmanDatabaseZip);
|
||||
zipPacmanDatabase();
|
||||
} else {
|
||||
File.Move(newPacmanDatabaseZip, pacmanDatabaseZip);
|
||||
}
|
||||
} else {
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine("Pacman Database is up to date");
|
||||
}
|
||||
} else {
|
||||
ZipFile.CreateFromDirectory(pacmanDatabaseLocation, pacmanDatabaseZip);
|
||||
}
|
||||
|
||||
if (File.Exists(pacmanDatabaseZip)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool checkForIdenticalFile(string existingFilePath, string newFilePath) {
|
||||
byte[] existingFile = File.ReadAllBytes(existingFilePath);
|
||||
byte[] newFile = File.ReadAllBytes(newFilePath);
|
||||
|
||||
if (existingFile.Length == newFile.Length) {
|
||||
for (int i=0; i < existingFile.Length; i++) {
|
||||
if (existingFile[i] != newFile[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user