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
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"bufio"
"slices"
"git.keyzox.me/42_adjoly/inception/internal/cmd"
"git.keyzox.me/42_adjoly/inception/internal/env"
_log "git.keyzox.me/42_adjoly/inception/internal/log"
"git.keyzox.me/42_adjoly/inception/internal/pass"
)
func overrideCronFile(filePath string, jobs []string) error {
@ -70,13 +70,13 @@ func main() {
_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 {
log.Fatal(err)
}
}
interval := env.EnvCheck("CRON_INTERVAL", "0 0 * * *")
interval := env.EnvCheck("CRON_INTERVAL", "0 2 * * *")
cronFilePath := "/etc/crontabs/root"
newJobs := []string{
"# 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-site:
nginx-certs:
backup:
networks:
inception:
@ -103,15 +104,12 @@ services:
- inception
environment:
- 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_REPO=/backup
- BORG_SRC=/source
- BORG_COMPRESS=zstd
- BORG_COMPRESS=
- BORG_PRUNE_KEEP_DAILY=3
- BORG_PRUNE_KEEP_WEEKLY=2
- BORG_PRUNE_KEEP_MONTHLY=1
#- BORG_EXCLUDE_PATTERNS=/var/www/cache # just an exemple to remove after
- BORG_LOG_LEVEL=info
- BORG_CHECK_LAST=3
- BORG_CHECK_DATA=1
@ -121,4 +119,5 @@ services:
volumes:
- wp-db:/source/db
- wp-site:/source/wordpress
- backup:/backup
restart: unless-stopped

View File

@ -1,5 +1,7 @@
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
@ -9,10 +11,11 @@ COPY cmd /build/cmd
COPY internal /build/internal
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
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 \
&& mkdir -p /source \
@ -23,11 +26,12 @@ RUN apk add --no-cache borgbackup tzdata \
&& rm -rf /var/cache/apk/*
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" ]
WORKDIR /
STOPSIGNAL SIGQUIT
CMD [ "crond", "-l", "${CRON_LOGLEVEL:-8}", "-f" ]
CMD [ "crond", "-l", "8", "-f" ]

View File

@ -3,9 +3,9 @@
set -e
# Define variables from environment
REPO=${BORG_REPO}
PASSPHRASE=${BORG_PASSPHRASE}
SOURCE=${BORG_SOURCE}
REPO=${BORG_REPO:-/backup}
BORG_PASSPHRASE=$(getpassphrase)
SOURCE=${BORG_SOURCE:-/source}
COMPRESSION=${BORG_COMPRESS:-zstd}
PRUNE_KEEP_DAILY=${BORG_PRUNE_KEEP_DAILY:-7}
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:-}
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
fi
if [[ -n "$EXCLUDE_PATTERNS" ]]; then
BAK_ARGS+=--exclude $EXCLUDE_PATTERNS
if [ -n "$EXCLUDE_PATTERNS" ]; then
BAK_ARGS="$BAK_ARGS --exclude $EXCLUDE_PATTERNS"
fi
# Borg backup command
borg create --stats $BAK_ARGS \
$REPO::$(hostname)-$(date +%Y-%m-%d) $SOURCE
$REPO::$(date +%Y-%m-%d) $SOURCE
# Borg prune command
@ -35,11 +36,11 @@ borg prune --list $REPO --keep-daily=$PRUNE_KEEP_DAILY --keep-weekly=$PRUNE_KEEP
# Borg check command
CHECK_ARGS=""
if [[ -n "$CHECK_LAST" ]]; then
CHECK_ARGS+=--last $CHECK_LAST
if [ -n "$CHECK_LAST" ]; then
CHECK_ARGS="$CHECK_ARGS --last $CHECK_LAST"
fi
if [[ -n "$CHECK_DATA" ]]; then
CHECK_ARGS+=--verify-data
if [ -n "$CHECK_DATA" ]; then
CHECK_ARGS="$CHECK_ARGS --verify-data"
fi
borg check $CHECK_ARGS $REPO

View File

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