summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-08-16 12:43:16 -0300
committerEuAndreh <eu@euandre.org>2024-08-16 12:43:16 -0300
commit7121c7a8a14825fe3334c447161afe6727db3a7a (patch)
treee5694e53a0effeb54b2d6b958d1c5e97ba0a8e11
parentInitial empty commit (diff)
downloadsyskeep-7121c7a8a14825fe3334c447161afe6727db3a7a.tar.gz
syskeep-7121c7a8a14825fe3334c447161afe6727db3a7a.tar.xz
Copy some files over
-rw-r--r--Makefile88
-rw-r--r--deps.mk10
-rwxr-xr-xmkdeps.sh13
-rwxr-xr-xsrc/backupit.txt7
-rwxr-xr-xsrc/gc146
5 files changed, 264 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..ebbb2af
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,88 @@
+.POSIX:
+DATE = 1970-01-01
+VERSION = 0.1.0
+NAME = syskeep
+NAME_UC = $(NAME)
+LANGUAGES = en
+## Installation prefix. Defaults to "/usr".
+PREFIX = /usr
+BINDIR = $(PREFIX)/bin
+LIBDIR = $(PREFIX)/lib
+INCLUDEDIR = $(PREFIX)/include
+SRCDIR = $(PREFIX)/src/$(NAME)
+SHAREDIR = $(PREFIX)/share
+LOCALEDIR = $(SHAREDIR)/locale
+MANDIR = $(SHAREDIR)/man
+EXEC = ./
+## Where to store the installation. Empty by default.
+DESTDIR =
+LDLIBS =
+
+
+
+.SUFFIXES:
+
+
+
+all:
+include deps.mk
+
+sources = \
+ $(sources.sh) \
+
+
+derived-assets = \
+
+side-assets = \
+
+
+
+## Default target. Builds all artifacts required for testing
+## and installation.
+all: $(derived-assets)
+
+
+
+check-unit:
+
+
+check-integration:
+
+
+## Run all tests. Each test suite is isolated, so that a parallel
+## build can run tests at the same time. The required artifacts
+## are created if missing.
+check: check-unit check-integration
+
+
+
+## Remove *all* derived artifacts produced during the build.
+## A dedicated test asserts that this is always true.
+clean:
+ rm -rf $(derived-assets) $(side-assets)
+
+
+## Installs into $(DESTDIR)$(PREFIX). Its dependency target
+## ensures that all installable artifacts are crafted beforehand.
+install: all
+ mkdir -p \
+ '$(DESTDIR)$(BINDIR)' \
+ '$(DESTDIR)$(SRCDIR)' \
+
+ cp $(sources.sh) '$(DESTDIR)$(BINDIR)'
+ cp $(sources) '$(DESTDIR)$(SRCDIR)'
+
+
+## Uninstalls from $(DESTDIR)$(PREFIX). This is a perfect mirror
+## of the "install" target, and removes *all* that was installed.
+## A dedicated test asserts that this is always true.
+uninstall:
+ rm -rf \
+ '$(DESTDIR)$(SRCDIR)' \
+
+ for f in $(sources.sh); do \
+ rm -f '$(DESTDIR)$(BINDIR)'/"$${f#src/}"; \
+ done
+
+
+ALWAYS:
diff --git a/deps.mk b/deps.mk
new file mode 100644
index 0000000..f2f8207
--- /dev/null
+++ b/deps.mk
@@ -0,0 +1,10 @@
+sources.sh = \
+ src/backup \
+ src/check \
+ src/cicd \
+ src/cronjob \
+ src/deploy \
+ src/gc \
+ src/reconfigure \
+ src/report \
+
diff --git a/mkdeps.sh b/mkdeps.sh
new file mode 100755
index 0000000..8204798
--- /dev/null
+++ b/mkdeps.sh
@@ -0,0 +1,13 @@
+#!/bin/sh
+set -eu
+
+export LANG=POSIX.UTF-8
+
+varlist() {
+ printf '%s = \\\n' "$1"
+ sort | sed 's|^\(.*\)$|\t\1 \\|'
+ printf '\n'
+}
+
+
+find src/* -type f -perm -111 | varlist 'sources.sh'
diff --git a/src/backupit.txt b/src/backupit.txt
new file mode 100755
index 0000000..e419623
--- /dev/null
+++ b/src/backupit.txt
@@ -0,0 +1,7 @@
+/mnt/production/
+/root/
+/home/
+/etc/
+/var/
+/opt/
+/srv/
diff --git a/src/gc b/src/gc
new file mode 100755
index 0000000..e037f3c
--- /dev/null
+++ b/src/gc
@@ -0,0 +1,146 @@
+#!/bin/sh
+set -eu
+
+usage() {
+ cat <<-'EOF'
+ Usage:
+ gc [TYPE]
+ gc -h
+ EOF
+}
+
+help() {
+ cat <<-'EOF'
+
+
+ Options:
+ -h, --help show this message
+
+ TYPE what to do GC on (default: all):
+ - guix
+ - deploy
+ - trash
+ - tmpdir
+ - logs
+
+
+ GC the server, deleting old, unusable data, in order to free
+ disk space system-wide.
+
+
+ Examples:
+
+ Just run it, for all:
+
+ $ gc
+
+
+ Cleanup tmpdir:
+
+ $ gc tmpdir
+ EOF
+}
+
+
+for flag in "$@"; do
+ case "$flag" in
+ --)
+ break
+ ;;
+ --help)
+ usage
+ help
+ exit
+ ;;
+ *)
+ ;;
+ esac
+done
+
+while getopts 'h' flag; do
+ case "$flag" in
+ h)
+ usage
+ help
+ exit
+ ;;
+ *)
+ usage >&2
+ exit 2
+ ;;
+ esac
+done
+shift $((OPTIND - 1))
+
+
+if [ "$(id -un)" != 'root' ]; then
+ printf 'This script must be run as root.\n\n' >&2
+ usage >&2
+ exit 2
+fi
+
+
+disk() {
+ df -h / /mnt/backup/ |
+ tail -n +2 |
+ awk '{ printf "%s\t%s/%s\t%s\n", $4, $3, $2, $6 }'
+}
+
+today() {
+ date '+%Y-%m-%d'
+}
+
+gc_guix() {
+ sudo -i guix system delete-generations 1m
+ sudo -i guix gc -d 1m
+}
+
+gc_deploy() {
+ find /opt/deploy \
+ ! -path /opt/deploy -prune \
+ -type d \
+ -not -name "$(today)*" \
+ -exec rm -rvf "{}" ';'
+}
+
+gc_trash() {
+ yes | sudo -i trash-empty
+}
+
+gc_tmpdir() {
+ find "${TMPDIR:-/tmp}" -atime +10 -exec rm -vf "{}" ';'
+}
+
+gc_logs() {
+ find /var/log/ci/ -atime +10 -exec rm -vf "{}" ';'
+}
+
+
+gc_all() {
+ gc_guix
+ gc_deploy
+ gc_trash
+ gc_tmpdir
+ gc_logs
+}
+
+
+TYPE="${1:-all}"
+CMD=gc_"$TYPE"
+if ! command -v "$CMD" >/dev/null; then
+ printf 'Invalid TYPE: "%s".\n\n' "$TYPE" >&2
+ usage >&2
+ exit 2
+fi
+
+BEFORE="$(disk)"
+set -x
+"$CMD"
+set +x
+AFTER="$(disk)"
+
+cat <<-EOF
+ Disk space:
+ before: $BEFORE
+ after: $AFTER
+EOF