2023-01-13 14:28:52 +01:00
using System.IO.Compression ;
2022-12-05 18:19:18 +01:00
public class Update {
2022-12-06 12:23:45 +01:00
public static string getHomePath ( ) {
string homePath = string . Empty ;
2022-12-05 18:19:18 +01:00
if ( Environment . OSVersion . Platform = = PlatformID . Unix ) {
homePath = Environment . GetEnvironmentVariable ( "HOME" ) ;
2022-12-06 12:23:45 +01:00
return homePath ;
2022-12-05 18:19:18 +01:00
} else {
2022-12-06 12:23:45 +01:00
throw new ApplicationException ( "This script doesn't support your operating system." ) ;
2022-12-05 18:19:18 +01:00
}
}
2023-01-11 04:47:58 +01:00
public static string copyEverthingBeforeUpdateToBackupLocation ( ) {
2023-01-13 14:28:52 +01:00
string targetPath = getHomePath ( ) + "/backup/uncompressed/" ;
2023-01-14 04:03:43 +01:00
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" ) ;
}
2023-01-11 04:47:58 +01:00
string [ ] systemFilesToCopy = { "/etc/fstab" , "/etc/makepkg.conf" } ;
List < string > filesToCopy = new List < string > ( systemFilesToCopy ) ;
string pacmanPackageListBeforeUpdate = getHomePath ( ) + "/pacman-pre.txt" ;
filesToCopy . Add ( pacmanPackageListBeforeUpdate ) ;
string flatpakListBeforeUpdate = getHomePath ( ) + "/flatpak-pre.txt" ;
filesToCopy . Add ( flatpakListBeforeUpdate ) ;
if ( ! Directory . Exists ( targetPath ) ) {
Directory . CreateDirectory ( targetPath ) ;
2023-01-14 04:03:43 +01:00
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 ( ) ;
2023-01-11 04:47:58 +01:00
} else {
foreach ( string file in filesToCopy ) {
FileInfo info = new FileInfo ( file ) ;
string destFile = Path . Combine ( targetPath , info . Name ) ;
File . Copy ( info . FullName , destFile , true ) ;
}
}
2022-12-05 18:19:18 +01:00
2023-01-11 04:47:58 +01:00
string copiedFiles = string . Join ( ", " , filesToCopy ) ;
2022-12-06 12:23:45 +01:00
2023-01-11 04:47:58 +01:00
Console . ForegroundColor = ConsoleColor . Green ;
return $"Copied {copiedFiles} to {targetPath}" ;
}
2022-12-06 12:23:45 +01:00
2023-01-12 22:38:29 +01:00
/// <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>
2023-01-14 04:03:43 +01:00
public static string copyEverthingFromBackupLocationToFinalDestination ( string finalBackupLocation ) {
string targetPath = finalBackupLocation ;
2022-12-06 12:23:45 +01:00
2023-01-11 04:47:58 +01:00
if ( ! Directory . Exists ( targetPath ) ) {
2023-01-12 22:38:29 +01:00
Console . ForegroundColor = ConsoleColor . Red ;
return $"Backup location does not exist! Please specify one in the config." ;
2023-01-11 04:47:58 +01:00
}
2022-12-06 16:53:10 +01:00
2023-01-12 22:38:29 +01:00
if ( targetPath is not null ) {
2023-01-14 04:03:43 +01:00
string sourcePath = getHomePath ( ) + "/backup/compressed/" ;
2023-01-12 22:38:29 +01:00
string [ ] intermediateBackupLocation = Directory . GetFiles ( sourcePath ) ;
2022-12-06 16:53:10 +01:00
2023-01-12 22:38:29 +01:00
if ( ! Directory . Exists ( targetPath ) ) {
throw new DirectoryNotFoundException ( "Target directory not found" ) ;
} else {
foreach ( string file in intermediateBackupLocation ) {
FileInfo info = new FileInfo ( file ) ;
string destFile = Path . Combine ( targetPath , info . Name ) ;
File . Copy ( info . FullName , destFile , true ) ;
}
}
Console . ForegroundColor = ConsoleColor . Green ;
return $"Copied everything successfully to {targetPath}" ;
} else {
Console . ForegroundColor = ConsoleColor . Red ;
return "You have not configured a backup location!" ;
}
}
2023-01-13 14:28:52 +01:00
2023-01-14 04:03:43 +01:00
public static bool zipAllContentInBackupLocation ( string finalZipName ) {
2023-01-13 14:28:52 +01:00
string targetPath = getHomePath ( ) + "/backup/compressed/" ;
Directory . CreateDirectory ( targetPath ) ;
2023-01-14 04:03:43 +01:00
string sourcePath = getHomePath ( ) + "/backup/uncompressed/" ; // Moved to /tmp
string targetZip = getHomePath ( ) + "/backup/compressed/" + finalZipName ;
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 ) ) {
return true ;
} else {
return false ;
}
}
2023-01-13 14:28:52 +01:00
2023-01-14 04:03:43 +01:00
public static bool zipPacmanDatabase ( ) {
2023-01-13 14:28:52 +01:00
string pacmanDatabaseLocation = "/var/lib/pacman/local/" ;
string pacmanDatabaseZip = getHomePath ( ) + "/backup/compressed/pacman_database.zip" ;
2023-01-14 04:03:43 +01:00
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 ) ) {
2023-01-13 14:28:52 +01:00
return true ;
} else {
return false ;
}
2023-01-14 04:03:43 +01:00
}
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 ;
2023-01-13 14:28:52 +01:00
}
2022-12-05 18:19:18 +01:00
}