big refactor
All checks were successful
continuous-integration/drone/push Build is passing

too lazy to list things
This commit is contained in:
2022-03-27 19:28:00 +02:00
parent 5f8248d236
commit 818e58f179
11 changed files with 74 additions and 32 deletions

21
src/backup.sh Executable file
View File

@@ -0,0 +1,21 @@
#!/usr/bin/env sh
set -e
mkdir -p /backup/sqlDump
for db in $(echo "$MYSQL_DUMP_DB" | sed 's/,/\n/g')
do
echo "Dumping db: $db"
mysqldump --compact -P 3306 --host "$MYSQL_HOST" -u "$MYSQL_USERNAME" "-p$MYSQL_PASSWORD_ACTUAL" "$db" > "/backup/sqlDump/$db-mysql.sql"
done
for db in $(echo "$POSTGRES_DUMP_DB" | sed 's/,/\n/g')
do
echo "Dumping db: $db"
pg_dump -U "$POSTGRES_USERNAME" --host "$POSTGRES_HOST" "$db" > "/backup/sqlDump/$db-postgres.sql"
done
restic --verbose backup "/backup"
restic --verbose prune --keep-last 3

47
src/entrypoint.sh Executable file
View File

@@ -0,0 +1,47 @@
#!/usr/bin/env sh
set -e
COMMAND="$1"
# print usage if no command is given
if [ -z "$COMMAND" ]; then
echo "Usage: $0 <command>"
echo "Available commands:"
echo " backup"
echo " restore"
echo " init"
echo " interactive"
exit 1
fi
# if command is init
if [ "$COMMAND" = "init" ]; then
/app/init.sh
exit 0
fi
. /app/setup.sh
case "$COMMAND" in
backup)
# backup
/app/backup.sh
;;
restore)
# restore
/app/restore.sh
;;
interactive)
# interactive
/app/interactive.sh
;;
list)
# list
/app/list.sh
;;
*)
echo "Unknown command: $COMMAND"
exit 1
;;
esac

43
src/init.sh Executable file
View File

@@ -0,0 +1,43 @@
#!/usr/bin/env sh
set -e
# if RCLONE_CONFIG_CONTENT is empty and no config file was found
if [ -z "$RCLONE_CONFIG_CONTENT" ] && [ ! -e "${RCLONE_CONFIG:=/config/rclone.conf}" ]; then
echo "No rclone config found"
rclone --config "${RCLONE_CONFIG:=/config/rclone.conf}" config
fi
RCLONE_REMOTE="$(rclone listremotes | head -1)"
echo "Using remote: $RCLONE_REMOTE"
RESTIC_REPOSITORY="rclone:$RCLONE_REMOTE"
echo "Using repository: $RESTIC_REPOSITORY"
# if RESTIC_PASSWORD is empty
if [ -z "$RESTIC_PASSWORD" ] && [ -z "$RESTIC_PASSWORD_FILE" ]; then
echo "Set \$RESTIC_PASSWORD or \$RESTIC_PASSWORD_FILE"
exit 1
fi
export RCLONE_REMOTE
export RESTIC_REPOSITORY
restic init
echo "Display rclone config [y/N]?"
read -r answer
# check if answer is yes
if [ "$answer" = "y" ] || [ "$answer" = "Y" ]; then
rclone --config "${RCLONE_CONFIG:=/config/rclone.conf}" config show
echo ""
fi
echo "Environemnt variables:"
echo "RCLONE_REMOTE=$RCLONE_REMOTE"
echo "RESTIC_PASSWORD=*********"
echo "RESTIC_REPOSITORY=$RESTIC_REPOSITORY"
echo ""

20
src/interactive.sh Executable file
View File

@@ -0,0 +1,20 @@
#!/usr/bin/env bash
mkdir -p /restore
echo "### Interactive mode ###"
echo "Remote: $RESTIC_REPOSITORY"
echo "Volumes mounted at /backup"
echo "########################"
echo "rclone config # edit/add remote"
echo "restic init # create new repo"
echo "restic snapshots # list backups"
echo "restic restore latest --target /restore # restore latest backup to /restore"
echo "restic dump latest /backup/sqlDump/dbname.sql # Dump sql dump to console"
echo "/app/backup.sh # Create backup"
echo "/app/mysql.sh # Open mysql client"
echo "########################"
export PS1="[${RCLONE_REMOTE} \w]\$ "
bash -i

9
src/list.sh Executable file
View File

@@ -0,0 +1,9 @@
#!/usr/bin/env sh
set -e
echo "Volumes:"
restic ls latest "/backup"
echo "Snapshots:"
restic snapshots latest

11
src/restore.sh Executable file
View File

@@ -0,0 +1,11 @@
#!/usr/bin/env bash
VOLUMES=$(find /backup -maxdepth 1 -type d -printf '%f\n' | tail -n +2)
#loop volumes
for VOLUME in $VOLUMES; do
echo "Restoring $VOLUME"
restic --verbose restore latest --include "/backup/$VOLUME"
done

44
src/setup.sh Executable file
View File

@@ -0,0 +1,44 @@
#!/usr/bin/env sh
set -e
if [ -n "$RCLONE_CONFIG_CONTENT" ]; then
echo "$RCLONE_CONFIG_CONTENT" > "${RCLONE_CONFIG:=/config/rclone.conf}"
fi
if [ -z "${RCLONE_CONFIG}" ]; then
RCLONE_CONFIG="/config/rclone.conf"
[ -e "${RCLONE_CONFIG}" ] || echo "No config file found"
echo "\$RCLONE_CONFIG not set. Defaulting to \"/config/rclone.conf\""
fi
export RCLONE_CONFIG
if [ -z "$RCLONE_REMOTE" ]; then
RCLONE_REMOTE="$(rclone listremotes | head -1)/backup"
echo "\$RCLONE_REMOTE not set. Defaulting to \"$RCLONE_REMOTE\""
fi
RESTIC_REPOSITORY="rclone:$RCLONE_REMOTE"
if [ -n "${RESTIC_PASSWORD}" ] && [ -n "$RESTIC_PASSWORD_FILE" ]; then
echo "\$RESTIC_PASSWORD or \$RESTIC_PASSWORD_FILE is not set"
fi
if [ -n "${MYSQL_PASSWORD}" ]; then
MYSQL_PASSWORD_ACTUAL="${MYSQL_PASSWORD}"
elif [ -n "${MYSQL_PASSWORD_FILE}" ]; then
MYSQL_PASSWORD_ACTUAL=$(cat "$MYSQL_PASSWORD_FILE")
fi
if [ -n "$POSTGRES_PASSWORD" ];then
PGPASSWORD="$POSTGRES_PASSWORD"
elif [ -n "$POSTGRES_PASSWORD_FILE" ]; then
PGPASSWORD=$(cat "$POSTGRES_PASSWORD_FILE")
fi
export RCLONE_REMOTE
export RESTIC_PASSWORD
export MYSQL_PASSWORD_ACTUAL
export RESTIC_REPOSITORY
export PGPASSWORD