Compare commits
45 Commits
2950e11654
...
main
Author | SHA1 | Date | |
---|---|---|---|
d101e186c4
|
|||
2d881a0d34
|
|||
cb735a01a1
|
|||
e3c9924fbc
|
|||
3645f7f30c
|
|||
66ca74441d
|
|||
7bca34cc89
|
|||
e4ab48fb22
|
|||
67c7f48cd7
|
|||
e2d4253050
|
|||
81b07991aa
|
|||
452b0e92e2
|
|||
f89e7112a4
|
|||
b28009f83b
|
|||
0cf266dd87
|
|||
6fce70ba3c
|
|||
b13189e7de
|
|||
92777bbbf8
|
|||
91eb6be7c6
|
|||
c28fbf309e
|
|||
fe9d5936e7
|
|||
30462c50c8
|
|||
6f9ee34a94
|
|||
3198a02081
|
|||
6593f087c4
|
|||
8b5704f397
|
|||
3c2ecc72ae
|
|||
e86a004a28
|
|||
a8eee49762
|
|||
e4e6f51bdb
|
|||
0db312e8a0
|
|||
2fff3ce2a2
|
|||
b7a2dc89be
|
|||
afab665ca6
|
|||
bbec26f438
|
|||
624371e8f1 | |||
355abbf36b | |||
ea112d4ceb
|
|||
92669692f6
|
|||
5d3cefb4ac | |||
767fda2d93 | |||
87e4d172ca | |||
e790f0196b | |||
5b8e9af05a | |||
cf69e69377 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
backup
|
50
README.md
50
README.md
@ -1,49 +1,33 @@
|
|||||||
# Update
|
# Update
|
||||||
|
|
||||||
Update script written in Bash for Arch Linux only. Keeps all your pacman and aur packages as well as your flatpaks up to date with one simple script. It also backups a list of all your pacman and aur packages and flatpaks.
|
Alpm hooks for the pacman package manager.<br>
|
||||||
|
This project consists of two hooks, one for pre-transaction and the other for
|
||||||
## Requirements
|
post-transaction.
|
||||||
|
|
||||||
Here is what is required.
|
|
||||||
```bash
|
|
||||||
pacman -S just
|
|
||||||
```
|
|
||||||
It also technically requires sudo, but if you use something else, then just create a softlink for this script to work.<br/>
|
|
||||||
Doing that would look like this.
|
|
||||||
```bash
|
|
||||||
ln -s /usr/bin/your_program /usr/bin/sudo
|
|
||||||
```
|
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git clone https://gitea.hopeless-cloud.xyz/AustrianToast/update.git && cd update
|
git clone https://gitea.hopeless-cloud.xyz/AustrianToast/update.git
|
||||||
|
cd update
|
||||||
```
|
```
|
||||||
|
|
||||||
Before installing, please edit the config and configure it to your liking.<br/>
|
Before installing, please edit the config and configure it to your liking.
|
||||||
|
|
||||||
Then install using
|
Then install using
|
||||||
```bash
|
```bash
|
||||||
just install
|
make install
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
If your require the previous functionality of being able to update flatpak and
|
||||||
|
having the ability to preview the updateable packages and flatpaks.<br>
|
||||||
|
This functionality has been re-packaged into `update.zsh`.<br>
|
||||||
|
If you use oh-my-zsh, then you just throw this file into your `$ZSH_CUSTOM`.<br>
|
||||||
|
This path is by default `~/.oh-my-zsh/custom`. If you use only plain zsh or any other
|
||||||
|
shell, then you can just take the contained functions and put them inside the
|
||||||
|
according shell config file. For example your `.bashrc`.
|
||||||
|
|
||||||
```
|
The function `flatpak-update` contains a variable called `BACKUP_LOCATION`.<br>
|
||||||
Usage: update [OPTION]
|
Please change this path to your desired backup location.
|
||||||
|
|
||||||
options:
|
|
||||||
no flag same as -p
|
|
||||||
-f updates using flatpak update only
|
|
||||||
-p updates using a pacman-wrapper only
|
|
||||||
-a updates using flatpak update and a pacman-wrapper
|
|
||||||
-g shutdowns the computer afterwards
|
|
||||||
-r reboots the computer afterwards
|
|
||||||
--help displays this message
|
|
||||||
--preview shows a preview of which pkg's will be updates
|
|
||||||
--version prints out the version number
|
|
||||||
--backup just does the before-backup without updating
|
|
||||||
```
|
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
Contributions are always welcome!
|
Contributions are always welcome!
|
||||||
|
189
backup.go
Normal file
189
backup.go
Normal file
@ -0,0 +1,189 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"archive/zip"
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/otiai10/copy"
|
||||||
|
"github.com/pelletier/go-toml"
|
||||||
|
)
|
||||||
|
|
||||||
|
var backup_locations []string
|
||||||
|
|
||||||
|
func parsePacmanConf() (string, error) {
|
||||||
|
var err error
|
||||||
|
var path string
|
||||||
|
|
||||||
|
file, err := os.Open("/etc/pacman.conf")
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
if !strings.Contains(line, "DBPath") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
path = strings.TrimSpace(strings.Split(line, "=")[1])
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
err = file.Close()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprint(path, "local"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func zipIt(pathToZip string, pathToFiles string) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
zipFile, err := os.Create(pathToZip)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
err = zipWriter.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = zipFile.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func backup(when string) error {
|
||||||
|
var err error
|
||||||
|
tmpPath := fmt.Sprint("/tmp/update/", when, "_backup")
|
||||||
|
log.Printf("tmpPath is: %s\n", tmpPath)
|
||||||
|
pacmanDb, err := parsePacmanConf()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
log.Printf("pacmanDB is at: %s\n", pacmanDb)
|
||||||
|
|
||||||
|
err = os.MkdirAll(tmpPath, 0755)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
os.RemoveAll("/tmp/update")
|
||||||
|
}()
|
||||||
|
|
||||||
|
output, err := exec.Command("pacman", "--verbose", "--query").Output()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.WriteFile(fmt.Sprint(tmpPath, "/", when, "_pacman.txt"), output, 0666)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.Compare(when, "post") != 0 {
|
||||||
|
err = copy.Copy(pacmanDb, fmt.Sprint(tmpPath, "/", pacmanDb))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
current_time := time.Now().Format(time.RFC3339)
|
||||||
|
pathToZip := fmt.Sprint(tmpPath, "_", current_time, ".zip")
|
||||||
|
log.Printf("pathToZip is: %s\n", pathToZip)
|
||||||
|
err = zipIt(pathToZip, tmpPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, backup_location := range backup_locations {
|
||||||
|
backup_path := fmt.Sprintf("%s/%s/%s_backup_%s.zip", backup_location, when, when, current_time)
|
||||||
|
log.Printf("backup_path is: %s\n", backup_path)
|
||||||
|
err = copy.Copy(pathToZip, backup_path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if strings.Compare(os.Args[1], "debug") != 0 {
|
||||||
|
log.SetOutput(io.Discard)
|
||||||
|
} else {
|
||||||
|
logFile, err := os.OpenFile("/tmp/update.txt", os.O_CREATE | os.O_APPEND | os.O_RDWR, 0666)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer logFile.Close()
|
||||||
|
|
||||||
|
mw := io.MultiWriter(os.Stdout, logFile)
|
||||||
|
log.SetOutput(mw)
|
||||||
|
|
||||||
|
fmt.Println("Logfile is /tmp/update.txt")
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Cmdline argument is: %s\n", os.Args[1])
|
||||||
|
|
||||||
|
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)
|
||||||
|
log.Printf("backup_locations are: %s\n", backup_locations)
|
||||||
|
|
||||||
|
err = backup(os.Args[1])
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("backup failed for the following reason: ", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
14
go.mod
Normal file
14
go.mod
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
module backup
|
||||||
|
|
||||||
|
go 1.23.0
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/otiai10/copy v1.14.1
|
||||||
|
github.com/pelletier/go-toml v1.9.5
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/otiai10/mint v1.6.3 // indirect
|
||||||
|
golang.org/x/sync v0.12.0 // indirect
|
||||||
|
golang.org/x/sys v0.31.0 // indirect
|
||||||
|
)
|
10
go.sum
Normal file
10
go.sum
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
github.com/otiai10/copy v1.14.1 h1:5/7E6qsUMBaH5AnQ0sSLzzTg1oTECmcCmT6lvF45Na8=
|
||||||
|
github.com/otiai10/copy v1.14.1/go.mod h1:oQwrEDDOci3IM8dJF0d8+jnbfPDllW6vUjNc3DoZm9I=
|
||||||
|
github.com/otiai10/mint v1.6.3 h1:87qsV/aw1F5as1eH1zS/yqHY85ANKVMgkDrf9rcxbQs=
|
||||||
|
github.com/otiai10/mint v1.6.3/go.mod h1:MJm72SBthJjz8qhefc4z1PYEieWmy8Bku7CjcAqyUSM=
|
||||||
|
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
|
||||||
|
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
|
||||||
|
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
|
||||||
|
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||||
|
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
|
||||||
|
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
28
legacy/README.md
Normal file
28
legacy/README.md
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
# Update
|
||||||
|
|
||||||
|
Update script written in Bash for Arch Linux only. Keeps all your official, aur packages and your flatpaks up to date with one simple script. It also backups a list of all your installed packages and flatpaks.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git clone https://gitea.hopeless-cloud.xyz/AustrianToast/update.git && cd update
|
||||||
|
```
|
||||||
|
|
||||||
|
Before installing, please edit the config and configure it to your liking.
|
||||||
|
Then install using
|
||||||
|
```bash
|
||||||
|
make
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```
|
||||||
|
Usage: update [OPTION]
|
||||||
|
options:
|
||||||
|
--help displays this message
|
||||||
|
--preview shows a preview of which pkg's will be updates
|
||||||
|
--version prints out the version number
|
||||||
|
```
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
Contributions are always welcome!
|
153
legacy/update
Executable file
153
legacy/update
Executable file
@ -0,0 +1,153 @@
|
|||||||
|
#!/usr/bin/bash
|
||||||
|
|
||||||
|
VERSION="3.2"
|
||||||
|
IGREEN="\033[0;92m" # Intense Green
|
||||||
|
IYELLOW="\033[0;93m" # Intense Yellow
|
||||||
|
IRED='\033[0;91m' # Red
|
||||||
|
NO_COLOR="\033[0m" # Text Reset
|
||||||
|
TMP="/tmp/update"
|
||||||
|
DATE="$(date +"%Y-%m-%dT%H:%M:%S%:z")" # RFC 3339 date-time https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
|
||||||
|
|
||||||
|
trap interrupt_function INT
|
||||||
|
|
||||||
|
interrupt_function() {
|
||||||
|
echo -e "${IRED}\nInterrupt has been detected${NO_COLOR}"
|
||||||
|
if [ -f /var/lib/pacman/db.lck ]; then
|
||||||
|
echo -e "${IRED}Trying to unlock the pacman db. Please enter your password.${NO_COLOR}"
|
||||||
|
su --command="rm --force /var/lib/pacman/db.lck"
|
||||||
|
fi
|
||||||
|
[[ -d $TMP ]] && rm --recursive --force "$TMP"
|
||||||
|
[[ -f "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst.new ]] && rm --force "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst.new
|
||||||
|
[[ -f "$BACKUP_LOCATION"/after-backup_"$DATE".tar.zst.new ]] && rm --force "$BACKUP_LOCATION"/after-backup_"$DATE".tar.zst.new
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
help() {
|
||||||
|
echo "Usage: update [OPTION]"
|
||||||
|
echo "options:"
|
||||||
|
echo "--help displays this message"
|
||||||
|
echo "--preview shows a preview of pkg's and flatpaks which can be updated"
|
||||||
|
echo "--version prints out the version number"
|
||||||
|
}
|
||||||
|
|
||||||
|
source "$HOME"/.config/update.conf || echo -e "${IRED}No config found${NO_COLOR}" || exit 1
|
||||||
|
|
||||||
|
if [[ ${1:0:2} == \-\- ]]; then
|
||||||
|
case "${1:2}" in
|
||||||
|
help)
|
||||||
|
help ;;
|
||||||
|
preview)
|
||||||
|
"$PACMAN_WRAPPER" -Syy
|
||||||
|
"$PACMAN_WRAPPER" --query --upgrades
|
||||||
|
[[ -x /usr/bin/flatpak ]] && flatpak remote-ls --updates ;;
|
||||||
|
version)
|
||||||
|
echo "$VERSION" ;;
|
||||||
|
?)
|
||||||
|
help; exit 1;;
|
||||||
|
esac
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
check_for_valid_backup_location() {
|
||||||
|
if [ ! -d "$1" ]; then
|
||||||
|
echo -e "${IYELLOW}$1 doesn't exist${NO_COLOR}"
|
||||||
|
read -p "Do you want to create the path and continue? [y/N]" input
|
||||||
|
case $input in
|
||||||
|
[Yy]) mkdir --parents "$1" ;;
|
||||||
|
[Nn]) exit 0;;
|
||||||
|
* ) exit 0 ;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
check_nfs_mount() {
|
||||||
|
if ! findmnt -rno SOURCE,TARGET,FSTYPE "$1" | grep -q nfs; then
|
||||||
|
echo -e "${IYELLOW}$1 is not an active NFS mount.${NO_COLOR}"
|
||||||
|
read -p "Do you want to continue without an active NFS mount? [y/N] " input
|
||||||
|
case $input in
|
||||||
|
[Yy]) return 0 ;;
|
||||||
|
[Nn]|*) exit 1 ;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
if [ "$BACKUP_LOCATION" ]; then
|
||||||
|
check_for_valid_backup_location "$BACKUP_LOCATION"
|
||||||
|
check_nfs_mount "$BACKUP_LOCATION"
|
||||||
|
else
|
||||||
|
echo -e "${IRED}No BACKUP_LOCATION in $HOME/.config/update.conf specified${NO_COLOR}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
[[ $SECONDARY_BACKUP_LOCATION ]] && check_for_valid_backup_location "$SECONDARY_BACKUP_LOCATION"
|
||||||
|
|
||||||
|
lock_pacman_db() {
|
||||||
|
if [ -f /var/lib/pacman/db.lck ]; then
|
||||||
|
echo -e "${IYELLOW}->${NO_COLOR} /var/lib/pacman/db.lck exists"
|
||||||
|
echo -e "${IYELLOW}->${NO_COLOR} there might be another instance of pacman running. exiting..."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "${IYELLOW}Trying to lock the pacman db. Please enter your password.${NO_COLOR}"
|
||||||
|
su --command="touch /var/lib/pacman/db.lck"
|
||||||
|
}
|
||||||
|
|
||||||
|
unlock_pacman_db() {
|
||||||
|
echo -e "${IYELLOW}Trying to unlock the pacman db. Please enter your password.${NO_COLOR}"
|
||||||
|
su --command="rm --force /var/lib/pacman/db.lck"
|
||||||
|
}
|
||||||
|
|
||||||
|
delete_oldest_backup() {
|
||||||
|
if [[ $(find $BACKUP_LOCATION -name '*$1*' -exec printf %c {} + | wc -c) -ge $BACKUP_AMOUNT ]]; then
|
||||||
|
rm --force $(find "$BACKUP_LOCATION" -name '*$1*' | sort -rn | head -1)
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
before_backup() {
|
||||||
|
lock_pacman_db
|
||||||
|
|
||||||
|
[[ -d $TMP ]] && rm --recursive --force "$TMP"
|
||||||
|
mkdir --parents "$TMP"/before-backup_"$DATE" "$TMP"/after-backup_"$DATE"
|
||||||
|
|
||||||
|
pacman --verbose --query > "$TMP"/before-backup_"$DATE"/pacman-before.txt
|
||||||
|
[[ -x /usr/bin/flatpak ]] && flatpak list --all --show-details > "$TMP"/before-backup_"$DATE"/flatpak-before.txt
|
||||||
|
|
||||||
|
tar --create --zstd --file "$TMP"/before-backup_"$DATE".tar.zst.new "$TMP"/before-backup_"$DATE" /var/lib/pacman/local
|
||||||
|
cp "$TMP"/before-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"
|
||||||
|
[[ $SECONDARY_BACKUP_LOCATION ]] && cp "$TMP"/before-backup_"$DATE".tar.zst.new "$SECONDARY_BACKUP_LOCATION"
|
||||||
|
|
||||||
|
unlock_pacman_db
|
||||||
|
}
|
||||||
|
|
||||||
|
after_backup() {
|
||||||
|
lock_pacman_db
|
||||||
|
|
||||||
|
pacman --verbose --query > "$TMP"/after-backup_"$DATE"/pacman-after.txt
|
||||||
|
[[ -x /usr/bin/flatpak ]] && flatpak list --all --show-details > "$TMP"/after-backup_"$DATE"/flatpak-after.txt
|
||||||
|
tar --create --zstd --file "$TMP"/after-backup_"$DATE".tar.zst.new "$TMP"/after-backup_"$DATE"
|
||||||
|
|
||||||
|
cp "$TMP"/after-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"
|
||||||
|
[[ $SECONDARY_BACKUP_LOCATION ]] && cp "$TMP"/after-backup_"$DATE".tar.zst.new "$SECONDARY_BACKUP_LOCATION"
|
||||||
|
|
||||||
|
delete_oldest_backup after-backup
|
||||||
|
mv "$BACKUP_LOCATION"/after-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"/after-backup_"$DATE".tar.zst
|
||||||
|
[[ $SECONDARY_BACKUP_LOCATION ]] && mv "$SECONDARY_BACKUP_LOCATION"/after-backup_"$DATE".tar.zst.new "$SECONDARY_BACKUP_LOCATION"/after-backup_"$DATE".tar.zst
|
||||||
|
|
||||||
|
delete_oldest_backup before-backup
|
||||||
|
mv "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst
|
||||||
|
[[ $SECONDARY_BACKUP_LOCATION ]] && mv "$SECONDARY_BACKUP_LOCATION"/before-backup_"$DATE".tar.zst.new "$SECONDARY_BACKUP_LOCATION"/before-backup_"$DATE".tar.zst
|
||||||
|
|
||||||
|
unlock_pacman_db
|
||||||
|
rm --recursive --force "$TMP"
|
||||||
|
}
|
||||||
|
|
||||||
|
update() {
|
||||||
|
"$PACMAN_WRAPPER"
|
||||||
|
if [[ -x /usr/bin/flatpak ]]; then
|
||||||
|
flatpak update --assumeyes
|
||||||
|
flatpak list --all --show-details > "$TMP"/after-backup_"$DATE"/flatpak-after.txt
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
before_backup && echo -e "${IGREEN}before-backup complete${NO_COLOR}"
|
||||||
|
update
|
||||||
|
after_backup && echo -e "${IGREEN}after-backup complete${NO_COLOR}"
|
11
legacy/update.conf
Normal file
11
legacy/update.conf
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# backup to this folder
|
||||||
|
#BACKUP_LOCATION=~
|
||||||
|
|
||||||
|
# optional secondary backup loaction
|
||||||
|
#SECONDARY_BACKUP_LOCATION=~
|
||||||
|
|
||||||
|
# use this pacman-wrapper
|
||||||
|
PACMAN_WRAPPER=yay
|
||||||
|
|
||||||
|
# how many different backups to keep
|
||||||
|
BACKUP_AMOUNT=10
|
32
makefile
Normal file
32
makefile
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
IGREEN := \033[0;92m
|
||||||
|
NO_COLOR := \033[0m
|
||||||
|
|
||||||
|
help:
|
||||||
|
@echo "Usage: make [OPTION]"
|
||||||
|
@echo "Available options are:"
|
||||||
|
@echo "help"
|
||||||
|
@echo "compile"
|
||||||
|
@echo "install This will also compile"
|
||||||
|
@echo "uninstall"
|
||||||
|
|
||||||
|
compile:
|
||||||
|
@echo -e "$(IGREEN)==> Compiling backup$(NO_COLOR)"
|
||||||
|
go build .
|
||||||
|
|
||||||
|
install: compile
|
||||||
|
@echo -e "$(IGREEN)==> Copying the hooks into /etc/pacman.d/hooks$(NO_COLOR)"
|
||||||
|
[[ -d /etc/pacman.d/hooks ]] || sudo mkdir /etc/pacman.d/hooks
|
||||||
|
sudo cp pre_backup.hook post_backup.hook /etc/pacman.d/hooks
|
||||||
|
@echo -e "$(IGREEN)==> Copying backup into /usr/local/bin$(NO_COLOR)"
|
||||||
|
sudo install -Dm755 backup /usr/local/bin/backup
|
||||||
|
@echo -e "$(IGREEN)==> Copying the config into /etc$(NO_COLOR)"
|
||||||
|
[[ -f /etc/update.toml ]] || sudo cp update.toml /etc
|
||||||
|
@echo -e "$(IGREEN)==> Finished.$(NO_COLOR)"
|
||||||
|
|
||||||
|
uninstall:
|
||||||
|
@echo -e "$(IGREEN)==> Removing the hooks from /etc/pacman.d/hooks$(NO_COLOR)"
|
||||||
|
sudo rm /etc/pacman.d/hooks/pre_backup.hook /etc/pacman.d/hooks/post_backup.hook
|
||||||
|
@echo -e "$(IGREEN)==> Removing backup into /usr/local/bin$(NO_COLOR)"
|
||||||
|
sudo rm /usr/local/bin/backup
|
||||||
|
@echo -e "$(IGREEN)==> /etc/update.toml will remain$(NO_COLOR)"
|
||||||
|
@echo -e "$(IGREEN)==> Finished.$(NO_COLOR)"
|
11
post_backup.hook
Normal file
11
post_backup.hook
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
[Trigger]
|
||||||
|
Operation = Install
|
||||||
|
Operation = Upgrade
|
||||||
|
Operation = Remove
|
||||||
|
Type = Package
|
||||||
|
Target = *
|
||||||
|
|
||||||
|
[Action]
|
||||||
|
Description = Backing up a list of all packages...
|
||||||
|
When = PostTransaction
|
||||||
|
Exec = /usr/local/bin/backup post
|
12
pre_backup.hook
Normal file
12
pre_backup.hook
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
[Trigger]
|
||||||
|
Operation = Install
|
||||||
|
Operation = Upgrade
|
||||||
|
Operation = Remove
|
||||||
|
Type = Package
|
||||||
|
Target = *
|
||||||
|
|
||||||
|
[Action]
|
||||||
|
Description = Backing up the pacman db...
|
||||||
|
When = PreTransaction
|
||||||
|
Exec = /usr/local/bin/backup pre
|
||||||
|
AbortOnFail
|
127
update
127
update
@ -1,127 +0,0 @@
|
|||||||
#!/usr/bin/bash
|
|
||||||
|
|
||||||
source "$HOME"/.config/update.conf || exit 1
|
|
||||||
[[ ! -d $BACKUP_LOCATION ]] && mkdir --parents "$BACKUP_LOCATION"
|
|
||||||
|
|
||||||
VERSION="2.0.9"
|
|
||||||
IGREEN="\033[0;92m" # Intense Green
|
|
||||||
IYELLOW="\033[0;93m" # Intense Yellow
|
|
||||||
NO_COLOR="\033[0m" # Text Reset
|
|
||||||
TMP="/tmp/update"
|
|
||||||
DATE="$(date +"%Y-%m-%dT%H:%M:%S%:z")" # RFC 3339 date-time https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
|
|
||||||
FINAL_COMMAND=""
|
|
||||||
|
|
||||||
trap interrupt_function INT
|
|
||||||
|
|
||||||
interrupt_function() {
|
|
||||||
echo "Interrupt has been detected"
|
|
||||||
[[ -f /var/lib/pacman/db.lck ]] && sudo rm /var/lib/pacman/db.lck > /dev/null 2>&1
|
|
||||||
rm --recursive "$TMP" > /dev/null 2>&1
|
|
||||||
exit 1
|
|
||||||
}
|
|
||||||
|
|
||||||
help() {
|
|
||||||
echo "Usage: update [OPTION]"
|
|
||||||
echo
|
|
||||||
echo "options:"
|
|
||||||
echo "no flag same as -p"
|
|
||||||
echo "-f updates using flatpak update only"
|
|
||||||
echo "-p updates using a pacman-wrapper only"
|
|
||||||
echo "-a updates using flatpak update and a pacman-wrapper"
|
|
||||||
echo "-g shutdowns the computer afterwards"
|
|
||||||
echo "-r reboots the computer afterwards"
|
|
||||||
echo "--help displays this message"
|
|
||||||
echo "--preview shows a preview of which pkg's can be updated"
|
|
||||||
echo "--version prints out the version number"
|
|
||||||
echo "--backup just does the before-backup without updating"
|
|
||||||
}
|
|
||||||
|
|
||||||
lock_pacman_db() {
|
|
||||||
if [ -f /var/lib/pacman/db.lck ]; then
|
|
||||||
echo -e "${IYELLOW}->${NO_COLOR} /var/lib/pacman/db.lck exists"
|
|
||||||
echo -e "${IYELLOW}->${NO_COLOR} there might be another instance of pacman running. exiting..."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
sudo touch /var/lib/pacman/db.lck
|
|
||||||
}
|
|
||||||
|
|
||||||
delete_oldest_backup() {
|
|
||||||
if [[ $(ls -Ub "$BACKUP_LOCATION" | grep -c $1) -ge $BACKUP_AMOUNT ]]; then
|
|
||||||
OLDEST_FILE="$(ls -rt1 $BACKUP_LOCATION | grep $1 | head -1)" # sorts by time (oldes first) and gets the first item
|
|
||||||
rm "$BACKUP_LOCATION"/"$OLDEST_FILE"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
before_backup() {
|
|
||||||
lock_pacman_db
|
|
||||||
delete_oldest_backup before-backup
|
|
||||||
[[ -d $TMP ]] && rm -r "$TMP"
|
|
||||||
mkdir --parents "$TMP"/before-backup_"$DATE" "$TMP"/after-backup_"$DATE"
|
|
||||||
pacman -Q > "$TMP"/before-backup_"$DATE"/pacman-before.txt
|
|
||||||
[[ -f /usr/bin/flatpak ]] && flatpak list --all --show-details > "$TMP"/before-backup_"$DATE"/flatpak-before.txt
|
|
||||||
tar --create --xz --file "$TMP"/before-backup_"$DATE".tar.xz.new "$TMP"/before-backup_"$DATE" /var/lib/pacman/local > /dev/null 2>&1 # for some reason it needs the output suppresion
|
|
||||||
sudo rm /var/lib/pacman/db.lck
|
|
||||||
mv "$TMP"/before-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION"
|
|
||||||
mv "$BACKUP_LOCATION"/before-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION"/before-backup_"$DATE".tar.xz
|
|
||||||
}
|
|
||||||
|
|
||||||
after_backup() {
|
|
||||||
delete_oldest_backup after-backup
|
|
||||||
tar --create --xz --file "$TMP"/after-backup_"$DATE".tar.xz.new "$TMP"/after-backup_"$DATE" > /dev/null 2>&1
|
|
||||||
mv "$TMP"/after-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION"
|
|
||||||
mv "$BACKUP_LOCATION"/after-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION"/after-backup_"$DATE".tar.xz
|
|
||||||
rm --recursive "$TMP"
|
|
||||||
}
|
|
||||||
|
|
||||||
update_with_pacman_wrapper() {
|
|
||||||
"$PACMAN_WRAPPER"
|
|
||||||
pacman -Q > "$TMP"/after-backup_"$DATE"/pacman-after.txt
|
|
||||||
}
|
|
||||||
|
|
||||||
update_with_flatpak() {
|
|
||||||
flatpak update --assumeyes
|
|
||||||
flatpak list --all --show-details > "$TMP"/after-backup_"$DATE"/flatpak-after.txt
|
|
||||||
}
|
|
||||||
|
|
||||||
if [[ ${1:0:2} = -- ]]; then
|
|
||||||
case "${1:2}" in
|
|
||||||
help)
|
|
||||||
help ;;
|
|
||||||
preview)
|
|
||||||
"$PACMAN_WRAPPER" -Sy
|
|
||||||
"$PACMAN_WRAPPER" -Qu ;;
|
|
||||||
version)
|
|
||||||
echo "$VERSON" ;;
|
|
||||||
backup)
|
|
||||||
before_backup
|
|
||||||
rm --recursive "$TMP" ;;
|
|
||||||
?)
|
|
||||||
help; exit 1;;
|
|
||||||
esac
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
before_backup && echo -e "${IGREEN}pre-backup complete${NO_COLOR}"
|
|
||||||
|
|
||||||
[[ $1 ]] || update_with_pacman_wrapper
|
|
||||||
while getopts 'fpagr' OPTIONS; do
|
|
||||||
case $OPTIONS in
|
|
||||||
f)
|
|
||||||
update_with_flatpak ;;
|
|
||||||
p)
|
|
||||||
update_with_pacman_wrapper ;;
|
|
||||||
a)
|
|
||||||
update_with_pacman_wrapper; update_with_flatpak ;;
|
|
||||||
g)
|
|
||||||
[[ $1 = -g ]] && update_with_pacman_wrapper
|
|
||||||
FINAL_COMMAND="systemctl --check-inhibitors=yes poweroff" ;;
|
|
||||||
r)
|
|
||||||
[[ $1 = -r ]] && update_with_pacman_wrapper
|
|
||||||
FINAL_COMMAND="systemctl --check-inhibitors=yes reboot" ;;
|
|
||||||
?)
|
|
||||||
help; exit 1;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
|
|
||||||
after_backup && echo -e "${IGREEN}after-backup complete${NO_COLOR}"
|
|
||||||
$FINAL_COMMAND; exit
|
|
@ -1,8 +0,0 @@
|
|||||||
# Backup to this folder
|
|
||||||
BACKUP_LOCATION=~/
|
|
||||||
|
|
||||||
# use this pacman-wrapper
|
|
||||||
PACMAN_WRAPPER=yay
|
|
||||||
|
|
||||||
# how many different backups to keep
|
|
||||||
BACKUP_AMOUNT=2
|
|
6
update.toml
Normal file
6
update.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
# Config for update
|
||||||
|
|
||||||
|
[backup]
|
||||||
|
# All locations need to be an absolute path
|
||||||
|
# Don't specify a path multiple times, else the zip will likely be broken
|
||||||
|
locations = ['/opt']
|
30
update.zsh
Normal file
30
update.zsh
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
preview() {
|
||||||
|
yay -Syy && yay -Qu
|
||||||
|
[[ -x /usr/bin/flatpak ]] && flatpak remote-ls --updates
|
||||||
|
}
|
||||||
|
|
||||||
|
flatpak-update() {
|
||||||
|
TMP="/tmp/update"
|
||||||
|
DATE="$(date +"%Y-%m-%dT%H:%M:%S%:z")"
|
||||||
|
BACKUP_LOCATION="/opt"
|
||||||
|
|
||||||
|
[[ -d $TMP ]] && rm --recursive --force "$TMP"
|
||||||
|
mkdir --parents "$TMP"/before-backup_"$DATE" "$TMP"/after-backup_"$DATE"
|
||||||
|
|
||||||
|
flatpak list --all --show-details > "$TMP"/before-backup_"$DATE"/flatpak-before.txt
|
||||||
|
tar --create --zstd --file "$TMP"/before-backup_"$DATE".tar.zst "$TMP"/before-backup_"$DATE"
|
||||||
|
|
||||||
|
flatpak update --assumeyes
|
||||||
|
|
||||||
|
flatpak list --all --show-details > "$TMP"/after-backup_"$DATE"/flatpak-after.txt
|
||||||
|
tar --create --zstd --file "$TMP"/after-backup_"$DATE".tar.zst "$TMP"/after-backup_"$DATE"
|
||||||
|
|
||||||
|
rsync -a "$TMP"/before-backup_"$DATE".tar.zst "$TMP"/after-backup_"$DATE".tar.zst "$BACKUP_LOCATION"
|
||||||
|
|
||||||
|
rm --recursive --force "$TMP"
|
||||||
|
}
|
||||||
|
|
||||||
|
update () {
|
||||||
|
yay
|
||||||
|
flatpak-update
|
||||||
|
}
|
Reference in New Issue
Block a user