2023-02-01 03:17:45 +01:00
using System.Diagnostics ;
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 ;
2023-02-01 03:17:45 +01:00
if ( Environment . OSVersion . Platform = = PlatformID . Unix ) {
2022-12-05 18:19:18 +01:00
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-02-01 03:17:45 +01:00
string targetPath = "/tmp/backup/uncompressed/" ;
2023-01-14 04:03:43 +01:00
2023-02-01 03:17:45 +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-14 04:03:43 +01:00
}
2023-01-11 04:47:58 +01:00
string [ ] systemFilesToCopy = { "/etc/fstab" , "/etc/makepkg.conf" } ;
List < string > filesToCopy = new List < string > ( systemFilesToCopy ) ;
2023-02-01 03:17:45 +01:00
string pacmanPackageListBeforeUpdate = targetPath + "pacman-pre.txt" ;
2023-01-11 04:47:58 +01:00
filesToCopy . Add ( pacmanPackageListBeforeUpdate ) ;
2023-02-01 03:17:45 +01:00
string flatpakListBeforeUpdate = targetPath + "flatpak-pre.txt" ;
2023-01-11 04:47:58 +01:00
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 ( ) {
2023-02-01 03:17:45 +01:00
string targetPath = "/tmp/backup/uncompressed/" ; // Use /tmp to zip and then move into /backup/compressed/
2023-01-14 04:03:43 +01:00
2023-02-01 03:17:45 +01:00
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" ) ;
2023-01-14 04:03:43 +01:00
}
List < string > filesToCopy = new List < string > ( ) ;
2023-02-01 03:17:45 +01:00
string pacmanPackageListBeforeUpdate = targetPath + "pacman-after.txt" ;
2023-01-14 04:03:43 +01:00
filesToCopy . Add ( pacmanPackageListBeforeUpdate ) ;
2023-02-01 03:17:45 +01:00
string flatpakListBeforeUpdate = targetPath + "flatpak-after.txt" ;
2023-01-14 04:03:43 +01:00
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/" ;
2023-01-14 04:49:39 +01:00
if ( ! Directory . Exists ( targetPath ) ) {
Directory . CreateDirectory ( targetPath ) ;
}
2023-01-13 14:28:52 +01:00
2023-02-01 03:17:45 +01:00
string sourcePath = "/tmp/backup/uncompressed/" ;
2023-01-14 04:03:43 +01:00
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 ) ) {
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/" ;
2023-02-01 03:17:45 +01:00
string oldPacmanDatabaseZip = getHomePath ( ) + "/backup/compressed/pacman-database.zip" ;
string newPacmanDatabaseZip = "/tmp/backup/compressed/pacman-database.zip" ;
2023-01-14 04:03:43 +01:00
2023-02-01 03:17:45 +01:00
if ( ! Directory . Exists ( "/tmp/backup/compressed/" ) ) {
Directory . CreateDirectory ( "/tmp/backup/compressed/" ) ;
}
try {
if ( checkForLckFile ( "/var/lib/pacman/" ) = = false ) { // Only creates the zip if the db.lck doesn't exist
makeDatabaseLock ( ) ;
if ( File . Exists ( newPacmanDatabaseZip ) ) { // Delete residual pacman database in tmp
2023-01-14 04:03:43 +01:00
File . Delete ( newPacmanDatabaseZip ) ;
2023-02-01 03:17:45 +01:00
deleteDatabaseLock ( ) ; // Delete previous created database lock
} else {
ZipFile . CreateFromDirectory ( pacmanDatabaseLocation , newPacmanDatabaseZip ) ; // If no zip exists create one
}
if ( File . Exists ( oldPacmanDatabaseZip ) ) {
if ( ! checkForIdenticalFile ( oldPacmanDatabaseZip , newPacmanDatabaseZip ) ) {
Console . ForegroundColor = ConsoleColor . Red ;
Console . WriteLine ( "Pacman Database is outdated" ) ;
File . Delete ( oldPacmanDatabaseZip ) ;
File . Move ( newPacmanDatabaseZip , oldPacmanDatabaseZip ) ;
} else {
Console . ForegroundColor = ConsoleColor . Green ;
Console . WriteLine ( "Pacman Database is up to date" ) ;
File . Delete ( newPacmanDatabaseZip ) ;
}
2023-01-14 04:03:43 +01:00
} else {
2023-02-01 03:17:45 +01:00
if ( ! File . Exists ( newPacmanDatabaseZip ) ) {
ZipFile . CreateFromDirectory ( pacmanDatabaseLocation , newPacmanDatabaseZip ) ; // Create the zip in tmp
}
File . Move ( newPacmanDatabaseZip , oldPacmanDatabaseZip ) ;
2023-01-14 04:03:43 +01:00
}
} else {
2023-02-01 03:17:45 +01:00
throw new ApplicationException ( "db.lck exists. Please try again later." ) ;
2023-01-14 04:03:43 +01:00
}
2023-02-01 03:17:45 +01:00
} catch ( Exception e ) {
//deleteDatabaseLock(); // Bad practice. Only for debug purpose!
Console . ForegroundColor = ConsoleColor . Red ;
Console . WriteLine ( e . Message ) ;
2023-01-14 04:03:43 +01:00
}
2023-02-01 03:17:45 +01:00
if ( File . Exists ( oldPacmanDatabaseZip ) ) {
deleteDatabaseLock ( ) ;
2023-01-13 14:28:52 +01:00
return true ;
} else {
return false ;
}
2023-02-01 03:17:45 +01:00
2023-01-14 04:03:43 +01:00
}
2023-02-01 03:17:45 +01:00
private static bool checkForIdenticalFile ( string existingFilePath , string newFilePath ) {
2023-01-14 04:03:43 +01:00
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
}
2023-02-01 03:17:45 +01:00
private static bool checkForLckFile ( string folderToCheck ) {
if ( Directory . GetFiles ( folderToCheck , "*.lck" ) . Length = = 1 ) {
return true ; // lck file exists
} else {
return false ; // lck file doesn't exists
}
}
private static void makeDatabaseLock ( ) {
var psi = new ProcessStartInfo
{
FileName = "/bin/bash" ,
UseShellExecute = false ,
RedirectStandardOutput = true ,
Arguments = string . Format ( "-c \"cd /var/lib/pacman/ && sudo touch db.lck && sudo chmod 000 db.lck\"" )
} ;
using ( var p = Process . Start ( psi ) )
{
if ( p ! = null )
{
var strOutput = p . StandardOutput . ReadToEnd ( ) ;
p . WaitForExit ( ) ;
}
}
}
private static void deleteDatabaseLock ( ) {
var psi = new ProcessStartInfo
{
FileName = "/bin/bash" ,
UseShellExecute = false ,
RedirectStandardOutput = true ,
Arguments = string . Format ( "-c \"cd /var/lib/pacman/ && sudo rm -f db.lck\"" )
} ;
using ( var p = Process . Start ( psi ) )
{
if ( p ! = null )
{
var strOutput = p . StandardOutput . ReadToEnd ( ) ;
p . WaitForExit ( ) ;
}
}
}
2022-12-05 18:19:18 +01:00
}