implement backup system

This commit is contained in:
HikariKnight 2023-04-10 18:33:48 +02:00
parent 7eb7e2fa1c
commit 300ca653cc
6 changed files with 88 additions and 6 deletions

View file

@ -29,4 +29,7 @@ func Set_Dracut() {
// Add to our kernel arguments file that vfio_pci should load early (dracut does this using kernel arguments)
fileio.AppendContent(" rd.driver.pre=vfio_pci", config.Path.CMDLINE)
// Make a backup of dracutConf if there is one there
backupFile(strings.Replace(dracutConf, "config", "", 1))
}

View file

@ -60,4 +60,7 @@ func Set_Modprobe(gpu_IDs []string) {
content,
conffile,
)
// Make a backup of dracutConf if there is one there
backupFile(strings.Replace(conffile, "config", "", 1))
}

View file

@ -95,6 +95,9 @@ func InitConfigs() {
confpath,
)
// Make a backup directory
makeBackupDir(syspath)
// Create the directories for our configs
err := os.MkdirAll(confpath, os.ModePerm)
errorcheck.ErrorCheck(err)
@ -128,6 +131,9 @@ func InitConfigs() {
errorcheck.ErrorCheck(err)
// Close the file so we can edit it
file.Close()
// Backup the sysfile if we do not have a backup
backupFile(sysfile)
}
// If we now have a config that exists
@ -179,3 +185,36 @@ func vfio_modules() []string {
// Return the modules
return modules
}
func backupFile(source string) {
// Make a destination path
dest := fmt.Sprintf("backup%s", source)
// If the file exists in the config but not on the system it is a file we make
if fileio.FileExist(fmt.Sprintf("config%s", source)) && !fileio.FileExist(source) {
// Create the blank file so that a copy of the backup folder to /etc
file, err := os.Create(dest)
errorcheck.ErrorCheck(err, "Error creating file %s", dest)
file.Close()
} else if !fileio.FileExist(dest) {
// If a backup of the file does not exist
// Write to the logger
logger.Printf("No first time backup of %s detected.\nCreating a backup at %s", source, dest)
// Copy the file
fileio.FileCopy(source, dest)
}
}
func makeBackupDir(dest string) {
// If a backup directory does not exist
if !fileio.FileExist("backup/") {
// Write to the logger
logger.Printf("Backup directory does not exist!\nCreating backup directory for first run backup")
}
// Make the empty directories
err := os.MkdirAll(fmt.Sprintf("backup/%s", dest), os.ModePerm)
errorcheck.ErrorCheck(err, "Error making backup/ folder")
}