update/backup.go

214 lines
3.5 KiB
Go
Raw Normal View History

2024-09-03 03:38:43 +02:00
package main
import (
2024-09-08 00:23:20 +02:00
"archive/zip"
2024-09-03 03:38:43 +02:00
"bufio"
"fmt"
2024-09-08 00:23:20 +02:00
"io"
2024-09-03 03:38:43 +02:00
"os"
"os/exec"
2024-09-08 00:23:20 +02:00
"path/filepath"
2024-09-03 03:38:43 +02:00
"strings"
2024-09-08 00:23:20 +02:00
"time"
"github.com/pelletier/go-toml"
2024-09-03 03:38:43 +02:00
)
2024-09-08 00:23:20 +02:00
var backup_locations []string
2024-09-03 03:38:43 +02:00
2024-09-08 00:23:20 +02:00
func parsePacmanConf() (string, error) {
var err error
2024-09-03 03:38:43 +02:00
var path string
file, err := os.Open("/etc/pacman.conf")
if err != nil {
2024-09-08 00:23:20 +02:00
return "", err
2024-09-03 03:38:43 +02:00
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if !strings.Contains(line, "DBPath") {
continue
}
path = strings.TrimSpace(strings.Split(line, "=")[1])
break
}
2024-09-08 00:23:20 +02:00
err = file.Close()
if err != nil {
return "", err
2024-09-03 03:38:43 +02:00
}
2024-09-08 00:23:20 +02:00
return fmt.Sprint(path, "local"), nil
2024-09-03 03:38:43 +02:00
}
2024-09-08 00:23:20 +02:00
func zipIt(pathToZip string, pathToFiles string) error {
var err error
2024-09-03 03:38:43 +02:00
2024-09-08 00:23:20 +02:00
zipFile, err := os.Create(pathToZip)
2024-09-03 03:38:43 +02:00
if err != nil {
2024-09-08 00:23:20 +02:00
return err
2024-09-03 03:38:43 +02:00
}
2024-09-08 00:23:20 +02:00
zipWriter := zip.NewWriter(zipFile)
walker := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
f, err := zipWriter.Create(path[len(pathToFiles):])
if err != nil {
return err
}
_, err = io.Copy(f, file)
if err != nil {
return err
}
return nil
}
err = filepath.Walk(pathToFiles, walker)
if err != nil {
return err
}
2024-09-03 03:38:43 +02:00
2024-09-08 00:23:20 +02:00
err = zipWriter.Close()
2024-09-03 03:38:43 +02:00
if err != nil {
2024-09-08 00:23:20 +02:00
return err
2024-09-03 03:38:43 +02:00
}
2024-09-08 00:23:20 +02:00
err = zipFile.Close()
if err != nil {
return err
}
return nil
}
2024-09-03 03:38:43 +02:00
2024-09-12 19:01:16 +02:00
func _CopyDir(source string, destination string) error {
srcDir, err := os.Stat(source)
if err != nil {
return err
}
if !srcDir.IsDir() {
return fmt.Errorf("source is not a directory")
}
dstDir, err := os.Stat(destination)
if err != nil {
return err
}
if !dstDir.IsDir() {
return fmt.Errorf("destination is not a directory")
}
return nil
}
func _CopyFile(source string, destination string) error {
srcFile, err := os.Open(source)
dstFile, err := os.Create(destination)
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return err
}
err = dstFile.Sync()
if err != nil {
return err
}
err = srcFile.Close()
if err != nil {
return err
}
err = dstFile.Close()
if err != nil {
return err
}
return nil
}
2024-09-08 00:23:20 +02:00
func backup(when string) error {
var err error
tmpPath := fmt.Sprint("/tmp/update/", when, "_backup")
pacmanDb, err := parsePacmanConf()
if err != nil {
return err
}
err = os.MkdirAll(tmpPath, 0755)
if err != nil {
return err
}
2024-09-03 03:38:43 +02:00
defer func() {
2024-09-08 00:23:20 +02:00
os.RemoveAll("/tmp/update")
2024-09-03 03:38:43 +02:00
}()
2024-09-08 00:23:20 +02:00
output, err := exec.Command("pacman", "--verbose", "--query").Output()
2024-09-03 03:38:43 +02:00
if err != nil {
2024-09-08 00:23:20 +02:00
return err
2024-09-03 03:38:43 +02:00
}
2024-09-12 19:01:16 +02:00
err = os.WriteFile(fmt.Sprint(tmpPath, "/", when, "_pacman.txt"), output, 0666)
2024-09-08 00:23:20 +02:00
if err != nil {
return err
}
2024-09-12 19:01:16 +02:00
if strings.Compare(when, "post") != 0 {
2024-09-08 00:23:20 +02:00
err = CopyDir(pacmanDb, fmt.Sprint(tmpPath, "/", pacmanDb))
if err != nil {
return err
}
}
current_time := time.Now().Format(time.RFC3339)
pathToZip := fmt.Sprint(tmpPath, "_", current_time, ".zip")
err = zipIt(pathToZip, tmpPath)
if err != nil {
return err
}
for _, backup_location := range backup_locations {
2024-09-12 19:01:16 +02:00
err = _CopyFile(pathToZip, fmt.Sprint(backup_location, "/", when, "_backup_", current_time, ".zip"))
2024-09-08 00:23:20 +02:00
if err != nil {
return err
}
}
return nil
2024-09-03 03:38:43 +02:00
}
func main() {
2024-09-08 00:23:20 +02:00
os.RemoveAll("/tmp/update")
tree, err := toml.LoadFile("/etc/update.toml")
if err != nil {
fmt.Println("backup failed for the following reason: ", err)
os.Exit(1)
}
backup_locations = tree.GetArray("backup.locations").([]string)
err = backup(os.Args[1])
if err != nil {
fmt.Println("backup failed for the following reason: ", err)
os.Exit(1)
2024-09-03 03:38:43 +02:00
}
}