Compare commits
12 Commits
f89e7112a4
...
main
Author | SHA1 | Date | |
---|---|---|---|
d101e186c4
|
|||
2d881a0d34
|
|||
cb735a01a1
|
|||
e3c9924fbc
|
|||
3645f7f30c
|
|||
66ca74441d
|
|||
7bca34cc89
|
|||
e4ab48fb22
|
|||
67c7f48cd7
|
|||
e2d4253050
|
|||
81b07991aa
|
|||
452b0e92e2
|
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
backup
|
29
README.md
29
README.md
@ -1,28 +1,33 @@
|
||||
# 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.
|
||||
Alpm hooks for the pacman package manager.<br>
|
||||
This project consists of two hooks, one for pre-transaction and the other for
|
||||
post-transaction.
|
||||
|
||||
## Installation
|
||||
|
||||
```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.
|
||||
Before installing, please edit the config and configure it to your liking.
|
||||
|
||||
Then install using
|
||||
```bash
|
||||
make
|
||||
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`.
|
||||
|
||||
```
|
||||
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
|
||||
```
|
||||
The function `flatpak-update` contains a variable called `BACKUP_LOCATION`.<br>
|
||||
Please change this path to your desired backup location.
|
||||
|
||||
## Contributing
|
||||
Contributions are always welcome!
|
||||
|
33
backup.go
33
backup.go
@ -5,12 +5,14 @@ import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/otiai10/copy"
|
||||
"github.com/pelletier/go-toml"
|
||||
)
|
||||
|
||||
@ -98,10 +100,12 @@ func zipIt(pathToZip string, pathToFiles string) error {
|
||||
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 {
|
||||
@ -116,13 +120,13 @@ func backup(when string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
err = os.WriteFile(fmt.Sprint(tmpPath, fmt.Sprint(when, "_pacman.txt")), output, 0644)
|
||||
err = os.WriteFile(fmt.Sprint(tmpPath, "/", when, "_pacman.txt"), output, 0666)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if when != "post" {
|
||||
err = CopyDir(pacmanDb, fmt.Sprint(tmpPath, "/", pacmanDb))
|
||||
if strings.Compare(when, "post") != 0 {
|
||||
err = copy.Copy(pacmanDb, fmt.Sprint(tmpPath, "/", pacmanDb))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -130,13 +134,16 @@ func backup(when string) error {
|
||||
|
||||
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 {
|
||||
err = CopyFile(pathToZip, fmt.Sprint(backup_location, "/", when, "_backup_", current_time, ".zip"))
|
||||
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
|
||||
}
|
||||
@ -146,6 +153,23 @@ func backup(when string) error {
|
||||
}
|
||||
|
||||
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")
|
||||
@ -155,6 +179,7 @@ func main() {
|
||||
}
|
||||
|
||||
backup_locations = tree.GetArray("backup.locations").([]string)
|
||||
log.Printf("backup_locations are: %s\n", backup_locations)
|
||||
|
||||
err = backup(os.Args[1])
|
||||
if err != nil {
|
||||
|
134
copy.go
134
copy.go
@ -1,134 +0,0 @@
|
||||
/* MIT License
|
||||
*
|
||||
* Copyright (c) 2017 Roland Singer [roland.singer@desertbit.com]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// CopyFile copies the contents of the file named src to the file named
|
||||
// by dst. The file will be created if it does not already exist. If the
|
||||
// destination file exists, all it's contents will be replaced by the contents
|
||||
// of the source file. The file mode will be copied from the source and
|
||||
// the copied data is synced/flushed to stable storage.
|
||||
func CopyFile(src, dst string) (err error) {
|
||||
in, err := os.Open(src)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer in.Close()
|
||||
|
||||
out, err := os.Create(dst)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
if e := out.Close(); e != nil {
|
||||
err = e
|
||||
}
|
||||
}()
|
||||
|
||||
_, err = io.Copy(out, in)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = out.Sync()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
si, err := os.Stat(src)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = os.Chmod(dst, si.Mode())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// CopyDir recursively copies a directory tree, attempting to preserve permissions.
|
||||
// Source directory must exist, destination directory must *not* exist.
|
||||
// Symlinks are ignored and skipped.
|
||||
func CopyDir(src string, dst string) (err error) {
|
||||
src = filepath.Clean(src)
|
||||
dst = filepath.Clean(dst)
|
||||
|
||||
si, err := os.Stat(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !si.IsDir() {
|
||||
return fmt.Errorf("source is not a directory")
|
||||
}
|
||||
|
||||
_, err = os.Stat(dst)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return
|
||||
}
|
||||
if err == nil {
|
||||
return fmt.Errorf("destination already exists")
|
||||
}
|
||||
|
||||
err = os.MkdirAll(dst, si.Mode())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
entries, err := ioutil.ReadDir(src)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, entry := range entries {
|
||||
srcPath := filepath.Join(src, entry.Name())
|
||||
dstPath := filepath.Join(dst, entry.Name())
|
||||
|
||||
if entry.IsDir() {
|
||||
err = CopyDir(srcPath, dstPath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// Skip symlinks.
|
||||
if entry.Mode()&os.ModeSymlink != 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
err = CopyFile(srcPath, dstPath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
11
go.mod
11
go.mod
@ -2,4 +2,13 @@ module backup
|
||||
|
||||
go 1.23.0
|
||||
|
||||
require github.com/pelletier/go-toml v1.9.5
|
||||
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
|
||||
)
|
||||
|
8
go.sum
8
go.sum
@ -1,2 +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!
|
@ -49,7 +49,7 @@ if [[ ${1:0:2} == \-\- ]]; then
|
||||
fi
|
||||
|
||||
check_for_valid_backup_location() {
|
||||
if [ ! -d $1 ]; then
|
||||
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
|
||||
@ -60,8 +60,20 @@ check_for_valid_backup_location() {
|
||||
fi
|
||||
}
|
||||
|
||||
if [ $BACKUP_LOCATION ]; then
|
||||
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
|
24
makefile
24
makefile
@ -1,28 +1,32 @@
|
||||
IGREEN := \033[0;92m
|
||||
NO_COLOR := \033[0m
|
||||
|
||||
help:
|
||||
@echo "Usage: make [OPTION]"
|
||||
@echo "Available options are:"
|
||||
@echo "help"
|
||||
@echo "install"
|
||||
@echo "compile"
|
||||
@echo "install This will also compile"
|
||||
@echo "uninstall"
|
||||
|
||||
compile:
|
||||
@echo "==> Compiling backup"
|
||||
@echo -e "$(IGREEN)==> Compiling backup$(NO_COLOR)"
|
||||
go build .
|
||||
|
||||
install: compile
|
||||
@echo "==> Copying the hooks into /etc/pacman.d/hooks"
|
||||
@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 "==> Copying backup into /usr/local/bin"
|
||||
@echo -e "$(IGREEN)==> Copying backup into /usr/local/bin$(NO_COLOR)"
|
||||
sudo install -Dm755 backup /usr/local/bin/backup
|
||||
@echo "==> Copying the config into /etc"
|
||||
@echo -e "$(IGREEN)==> Copying the config into /etc$(NO_COLOR)"
|
||||
[[ -f /etc/update.toml ]] || sudo cp update.toml /etc
|
||||
@echo "==> Finished."
|
||||
@echo -e "$(IGREEN)==> Finished.$(NO_COLOR)"
|
||||
|
||||
uninstall:
|
||||
@echo "==> Removing the hooks from /etc/pacman.d/hooks"
|
||||
@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 "==> Removing backup into /usr/local/bin"
|
||||
@echo -e "$(IGREEN)==> Removing backup into /usr/local/bin$(NO_COLOR)"
|
||||
sudo rm /usr/local/bin/backup
|
||||
@echo "==> /etc/update.toml will remain"
|
||||
@echo "==> Finished."
|
||||
@echo -e "$(IGREEN)==> /etc/update.toml will remain$(NO_COLOR)"
|
||||
@echo -e "$(IGREEN)==> Finished.$(NO_COLOR)"
|
||||
|
@ -6,6 +6,6 @@ Type = Package
|
||||
Target = *
|
||||
|
||||
[Action]
|
||||
Description = Backing up the pacman db...
|
||||
Description = Backing up a list of all packages...
|
||||
When = PostTransaction
|
||||
Exec = /usr/local/bin/backup post
|
||||
|
@ -1,4 +1,6 @@
|
||||
# Config for update
|
||||
|
||||
[backup]
|
||||
locations = ['/opt'] # All locations need to be an absolute path
|
||||
# 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