From 79bfb93acd9338567d05b6cd28900dd6b95a64bb Mon Sep 17 00:00:00 2001 From: Dan Date: Sun, 23 Jan 2022 16:30:57 +1300 Subject: [PATCH] added files --- 1.cleanup_cache.hook | 11 +++++++ 2.archive_packages-client.hook | 11 +++++++ 2.archive_packages-server.hook | 11 +++++++ archive_packages.sh | 10 ++++++ client.sh | 24 ++++++++++++++ delete.sh | 35 +++++++++++++++++++++ repo-add.sh | 3 ++ server.sh | 57 ++++++++++++++++++++++++++++++++++ 8 files changed, 162 insertions(+) create mode 100644 1.cleanup_cache.hook create mode 100644 2.archive_packages-client.hook create mode 100644 2.archive_packages-server.hook create mode 100644 archive_packages.sh create mode 100644 client.sh create mode 100644 delete.sh create mode 100644 repo-add.sh create mode 100644 server.sh diff --git a/1.cleanup_cache.hook b/1.cleanup_cache.hook new file mode 100644 index 0000000..b751d64 --- /dev/null +++ b/1.cleanup_cache.hook @@ -0,0 +1,11 @@ +[Trigger] +Type = Package +Operation = Remove +Operation = Install +Operation = Upgrade +Target = * + +[Action] +Description = Removing obsolete cached package files (keeping the latest two)... +When = PostTransaction +Exec = /usr/bin/paccache -rvk2 \ No newline at end of file diff --git a/2.archive_packages-client.hook b/2.archive_packages-client.hook new file mode 100644 index 0000000..9cb13f1 --- /dev/null +++ b/2.archive_packages-client.hook @@ -0,0 +1,11 @@ +[Trigger] +Type = Package +Operation = Remove +Operation = Install +Operation = Upgrade +Target = * + +[Action] +Description = Archive packages to your archive server. +When = PostTransaction +Exec = /root/archive_packages.sh \ No newline at end of file diff --git a/2.archive_packages-server.hook b/2.archive_packages-server.hook new file mode 100644 index 0000000..79c1c59 --- /dev/null +++ b/2.archive_packages-server.hook @@ -0,0 +1,11 @@ +[Trigger] +Type = Package +Operation = Remove +Operation = Install +Operation = Upgrade +Target = * + +[Action] +Description = Archive packages to your archive server. +When = PostTransaction +Exec = /root/repo-add.sh \ No newline at end of file diff --git a/archive_packages.sh b/archive_packages.sh new file mode 100644 index 0000000..f098ca7 --- /dev/null +++ b/archive_packages.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# Variables +arch=$(uname -m) +## Add your archive server ip and user (rsync auth user) +server="$(cat /etc/rsyncd.server)" +user="$(cat /etc/rsyncd.user)" + +# Rsync daemon +rsync -chavzP --password-file=/etc/rsyncd.password --ignore-existing /var/cache/pacman/pkg/*pkg* rsync://$user@$server/archiverepo/archlinux/$arch diff --git a/client.sh b/client.sh new file mode 100644 index 0000000..513d55a --- /dev/null +++ b/client.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# Variables +read -p "Enter your archive server ip address: " server +read -p "Enter your user for rsync daemon credentials: " user +read -p "Enter your password: " -s password + +# Hooks +mkdir /etc/pacman.d/hooks/ +cp 1.cleanup_cache.hook /etc/pacman.d/hooks/ +cp 2.archive_packages-client.hook /etc/pacman.d/hooks/2.archive_packages.hook + +# Script/s +cp archive_packages.sh /root/ +chmod +x /root/archive_packages.sh + +# Rsync credentials +echo "$user" > /etc/rsyncd.user +echo "$password" > /etc/rsyncd.password +echo "$server" > /etc/rsyncd.server +chmod 400 /etc/rsyncd.user /etc/rsyncd.password /etc/rsyncd.server + +# Add repository to pacman.conf +echo "[homerepo] +Server = http://$server:8080/archlinux/\$arch" >> /etc/pacman.conf \ No newline at end of file diff --git a/delete.sh b/delete.sh new file mode 100644 index 0000000..0ee6b33 --- /dev/null +++ b/delete.sh @@ -0,0 +1,35 @@ +#!/bin/bash +# Delete everything +read -r -p "You are going to delete everything. Are you sure? [YES/no] " prompt + +if [[ $prompt == "YES" ]] +then + +arch=$(uname -m) + +# Hooks +rm /etc/pacman.d/hooks/1.cleanup_cache.hook +rm /etc/pacman.d/hooks/2.archive_packages.hook + +# scripts +rm /root/repo-add.sh +rm /root/archive_packages.sh + +# Repo +rm -r /srv/http/repo/ + +# Pacman config +sed -i "s|CacheDir = /srv/http/repo/archlinux/$arch/|#CacheDir = /var/cache/pacman/pkg/|g" /etc/pacman.conf +head -n -2 /etc/pacman.conf > tmp.txt && mv tmp.txt /etc/pacman.conf + +# Rsync +rm /etc/rsyncd.secrets +rm /etc/rsyncd.user +rm /etc/rsyncd.password +rm /etc/rsyncd.server + +else + +echo "Nothing has been deleted." + +fi \ No newline at end of file diff --git a/repo-add.sh b/repo-add.sh new file mode 100644 index 0000000..543b908 --- /dev/null +++ b/repo-add.sh @@ -0,0 +1,3 @@ +#!/bin/bash +# Used for `2.archive_packages-SERVER.hook` and rsync daemon `post-xfer exec = /root/repo-add.sh` +repo-add /srv/http/repo/archlinux/*/homerepo.db.tar.zst /srv/http/repo/archlinux/*/{*.zst,*.gz,*.xz} \ No newline at end of file diff --git a/server.sh b/server.sh new file mode 100644 index 0000000..fb10481 --- /dev/null +++ b/server.sh @@ -0,0 +1,57 @@ +#!/bin/bash +# Variables +arch=$(uname -m) +server="$(ip addr show | grep "inet " | grep -v 127.0.0. | head -1 | cut -d" " -f6 | cut -d/ -f1)" +## Rsync credentials +read -p "Enter your user for rsync daemon credentials: " user +read -p "Enter your password: " -s password + +# Hooks +mkdir /etc/pacman.d/hooks/ +cp 1.cleanup_cache.hook /etc/pacman.d/hooks/ +cp 2.archive_packages-server.hook /etc/pacman.d/hooks/2.archive_packages.hook + +# Script/s +cp repo-add.sh /root/ +chmod +x /root/repo-add.sh + +# Database +mkdir -p /srv/http/repo/archlinux/{x86_64,aarch64}/AUR +repo-add /srv/http/repo/archlinux/x86_64/homerepo.db.tar.zst +repo-add /srv/http/repo/archlinux/x86_64/AUR/homerepo.db.tar.zst +repo-add /srv/http/repo/archlinux/aarch64/homerepo.db.tar.zst +repo-add /srv/http/repo/archlinux/aarch64/AUR/homerepo.db.tar.zst + +# Move existing packages +mv /var/cache/pacman/pkg/*pkg* /srv/http/repo/archlinux/$arch/ +repo-add /srv/http/repo/archlinux/$arch/homerepo.db.tar.zst /srv/http/repo/archlinux/$arch/{*.zst,*.gz,*.xz} +chown -R http: /srv/http/repo/ + +# Change cache path in pacman.conf +sed -i "s|#CacheDir = /var/cache/pacman/pkg/|CacheDir = /srv/http/repo/archlinux/$arch/|g" /etc/pacman.conf + +# Rsync daemon +## Credentials +echo "$user:$password" > /etc/rsyncd.secrets +chmod 400 /etc/rsyncd.secrets + +## Config +echo "[archiverepo] +path = /srv/http/repo/ +comment = Arch Archive Repository +timeout = 300 +read only = false +uid = 33 +gid = 33 +post-xfer exec = /root/repo-add.sh +# Security +auth users = $user +secrets file = /etc/rsyncd.secrets +# Optional +#hosts allow = 192.168.1.0/255.255.255.0 +" >> /etc/rsyncd.conf +systemctl enable --now rsyncd.service + +# Add repository to pacman.conf +echo "[homerepo] +Server = http://$server:8080/archlinux/\$arch" >> /etc/pacman.conf \ No newline at end of file