1
0

」 feat: finished borg backup

This commit is contained in:
2025-03-03 21:16:45 +01:00
parent 25ec0ebb31
commit 57387462cf
6 changed files with 49 additions and 31 deletions

View File

@ -1,16 +1,16 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"log" "log"
"os" "os"
"os/exec" "os/exec"
"bufio" "slices"
"git.keyzox.me/42_adjoly/inception/internal/cmd" "git.keyzox.me/42_adjoly/inception/internal/cmd"
"git.keyzox.me/42_adjoly/inception/internal/env" "git.keyzox.me/42_adjoly/inception/internal/env"
_log "git.keyzox.me/42_adjoly/inception/internal/log" _log "git.keyzox.me/42_adjoly/inception/internal/log"
"git.keyzox.me/42_adjoly/inception/internal/pass"
) )
func overrideCronFile(filePath string, jobs []string) error { func overrideCronFile(filePath string, jobs []string) error {
@ -70,13 +70,13 @@ func main() {
_log.Log("error", "No passphrase specified, exiting...") _log.Log("error", "No passphrase specified, exiting...")
} }
err = cmd.ExecCmd([]string{"borg", "init", "--encryption=" + passphrase, repo}) err = cmd.ExecCmd([]string{"borg", "init", "--encryption=repokey-blake2", repo}, slices.Insert(os.Environ(), len(os.Environ()), "BORG_PASSPHRASE="+passphrase))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
interval := env.EnvCheck("CRON_INTERVAL", "0 0 * * *") interval := env.EnvCheck("CRON_INTERVAL", "0 2 * * *")
cronFilePath := "/etc/crontabs/root" cronFilePath := "/etc/crontabs/root"
newJobs := []string{ newJobs := []string{
"# Borg Backup Cron Job", "# Borg Backup Cron Job",

View File

@ -0,0 +1,16 @@
package main
import (
"fmt"
"git.keyzox.me/42_adjoly/inception/internal/env"
_log "git.keyzox.me/42_adjoly/inception/internal/log"
)
func main(){
pass := env.FileEnv("BORG_PASSPHRASE", "")
if pass == "" {
_log.Log("error", "Could not found passphrase")
}
fmt.Print(pass)
}

View File

@ -4,6 +4,7 @@ volumes:
wp-db: wp-db:
wp-site: wp-site:
nginx-certs: nginx-certs:
backup:
networks: networks:
inception: inception:
@ -103,15 +104,12 @@ services:
- inception - inception
environment: environment:
- TZ=Europe/Paris # handled by tzdata - TZ=Europe/Paris # handled by tzdata
- CRON_INTERVAL=0 0 * * * # handled by entrypoint - CRON_INTERVAL=0 2 * * * # handled by entrypoint
- BORG_PASSPHRASE=Hanky-Kangaroo-Thinning5-Statute-Mascot-Islamist - BORG_PASSPHRASE=Hanky-Kangaroo-Thinning5-Statute-Mascot-Islamist
- BORG_REPO=/backup - BORG_COMPRESS=
- BORG_SRC=/source
- BORG_COMPRESS=zstd
- BORG_PRUNE_KEEP_DAILY=3 - BORG_PRUNE_KEEP_DAILY=3
- BORG_PRUNE_KEEP_WEEKLY=2 - BORG_PRUNE_KEEP_WEEKLY=2
- BORG_PRUNE_KEEP_MONTHLY=1 - BORG_PRUNE_KEEP_MONTHLY=1
#- BORG_EXCLUDE_PATTERNS=/var/www/cache # just an exemple to remove after
- BORG_LOG_LEVEL=info - BORG_LOG_LEVEL=info
- BORG_CHECK_LAST=3 - BORG_CHECK_LAST=3
- BORG_CHECK_DATA=1 - BORG_CHECK_DATA=1
@ -121,4 +119,5 @@ services:
volumes: volumes:
- wp-db:/source/db - wp-db:/source/db
- wp-site:/source/wordpress - wp-site:/source/wordpress
- backup:/backup
restart: unless-stopped restart: unless-stopped

View File

@ -1,5 +1,7 @@
FROM scratch AS builder FROM scratch AS builder
ADD alpine-minirootfs-3.21.2-x86_64.tar.gz / ADD docker/alpine/alpine-minirootfs-3.21.2-x86_64.tar.gz /
RUN apk add go
WORKDIR /build WORKDIR /build
@ -9,10 +11,11 @@ COPY cmd /build/cmd
COPY internal /build/internal COPY internal /build/internal
RUN cd /build \ RUN cd /build \
&& go build git.keyzox.me/42_adjoly/inception/cmd/borg-backup/entrypoint && go build git.keyzox.me/42_adjoly/inception/cmd/borg-backup/entrypoint \
&& go build git.keyzox.me/42_adjoly/inception/cmd/borg-backup/getpassphrase
FROM scratch FROM scratch
ADD alpine-minirootfs-3.21.2-x86_64.tar.gz / ADD docker/alpine/alpine-minirootfs-3.21.2-x86_64.tar.gz /
RUN mkdir -p /backup \ RUN mkdir -p /backup \
&& mkdir -p /source \ && mkdir -p /source \
@ -23,11 +26,12 @@ RUN apk add --no-cache borgbackup tzdata \
&& rm -rf /var/cache/apk/* && rm -rf /var/cache/apk/*
COPY --from=builder /build/entrypoint /docker-entrypoint COPY --from=builder /build/entrypoint /docker-entrypoint
COPY docker/bonus/borg-backup/default-bak.sh /docker-backup.d COPY --from=builder /build/getpassphrase /bin/getpassphrase
COPY docker/bonus/borg-backup/default-bak.sh /docker-backup.d/default-bak.sh
ENTRYPOINT [ "/docker-entrypoint" ] ENTRYPOINT [ "/docker-entrypoint" ]
WORKDIR / WORKDIR /
STOPSIGNAL SIGQUIT STOPSIGNAL SIGQUIT
CMD [ "crond", "-l", "${CRON_LOGLEVEL:-8}", "-f" ] CMD [ "crond", "-l", "8", "-f" ]

View File

@ -3,9 +3,9 @@
set -e set -e
# Define variables from environment # Define variables from environment
REPO=${BORG_REPO} REPO=${BORG_REPO:-/backup}
PASSPHRASE=${BORG_PASSPHRASE} BORG_PASSPHRASE=$(getpassphrase)
SOURCE=${BORG_SOURCE} SOURCE=${BORG_SOURCE:-/source}
COMPRESSION=${BORG_COMPRESS:-zstd} COMPRESSION=${BORG_COMPRESS:-zstd}
PRUNE_KEEP_DAILY=${BORG_PRUNE_KEEP_DAILY:-7} PRUNE_KEEP_DAILY=${BORG_PRUNE_KEEP_DAILY:-7}
PRUNE_KEEP_WEEKLY=${BORG_PRUNE_KEEP_WEEKLY:-4} PRUNE_KEEP_WEEKLY=${BORG_PRUNE_KEEP_WEEKLY:-4}
@ -13,19 +13,20 @@ PRUNE_KEEP_MONTHLY=${BORG_PRUNE_KEEP_MONTHLY:-6}
EXCLUDE_PATTERNS=${BORG_EXCLUDE_PATTERNS:-} EXCLUDE_PATTERNS=${BORG_EXCLUDE_PATTERNS:-}
CHECK_LAST=${BORG_CHECK_LAST} CHECK_LAST=${BORG_CHECK_LAST}
BAK_ARGS=--compression $COMPRESSION BAK_ARGS="--compression $COMPRESSION"
if [[ -z "$PASSPHRASE" ]]; then if [ -z "$BORG_PASSPHRASE" ]; then
echo "Could not found passphrase"
exit 1 exit 1
fi fi
if [[ -n "$EXCLUDE_PATTERNS" ]]; then if [ -n "$EXCLUDE_PATTERNS" ]; then
BAK_ARGS+=--exclude $EXCLUDE_PATTERNS BAK_ARGS="$BAK_ARGS --exclude $EXCLUDE_PATTERNS"
fi fi
# Borg backup command # Borg backup command
borg create --stats $BAK_ARGS \ borg create --stats $BAK_ARGS \
$REPO::$(hostname)-$(date +%Y-%m-%d) $SOURCE $REPO::$(date +%Y-%m-%d) $SOURCE
# Borg prune command # Borg prune command
@ -35,11 +36,11 @@ borg prune --list $REPO --keep-daily=$PRUNE_KEEP_DAILY --keep-weekly=$PRUNE_KEEP
# Borg check command # Borg check command
CHECK_ARGS="" CHECK_ARGS=""
if [[ -n "$CHECK_LAST" ]]; then if [ -n "$CHECK_LAST" ]; then
CHECK_ARGS+=--last $CHECK_LAST CHECK_ARGS="$CHECK_ARGS --last $CHECK_LAST"
fi fi
if [[ -n "$CHECK_DATA" ]]; then if [ -n "$CHECK_DATA" ]; then
CHECK_ARGS+=--verify-data CHECK_ARGS="$CHECK_ARGS --verify-data"
fi fi
borg check $CHECK_ARGS $REPO borg check $CHECK_ARGS $REPO

View File

@ -3,13 +3,11 @@ package cmd
import ( import (
"os" "os"
"os/exec" "os/exec"
_log "git.keyzox.me/42_adjoly/inception/internal/log"
) )
func ExecCmd(cmdStr []string) error { func ExecCmd(cmdStr, env []string) error {
cmd := exec.Command(cmdStr[0], cmdStr...) cmd := exec.Command(cmdStr[0], cmdStr[1:]...)
cmd.Env = os.Environ() cmd.Env = env
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin cmd.Stdin = os.Stdin