diff --git a/.gitignore b/.gitignore index 218cbb3..cf58469 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,10 @@ !.gitignore !Dockerfile !docker-compose.yml -!entrypoint.sh +!backup.sh !README.md !.drone.yml -!init.sh \ No newline at end of file +!init.sh +!setup.sh +!interactive.sh +!mysql.sh \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 5a2c1fc..4c32adb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,16 @@ -FROM alpine:latest +FROM restic/restic -RUN apk add --no-cache mysql-client restic rclone +RUN apk add --no-cache mysql-client rclone bash fuse RUN mkdir /app && mkdir /config -COPY entrypoint.sh /app/entrypoint.sh -COPY init.sh /app/init.sh +COPY setup.sh /app/setup.sh +COPY backup.sh /app/backup.sh +COPY interactive.sh /app/interactive.sh +COPY mysql.sh /app/mysql.sh WORKDIR /config VOLUME /config -ENTRYPOINT /app/entrypoint.sh +ENTRYPOINT /app/backup.sh diff --git a/backup.sh b/backup.sh new file mode 100755 index 0000000..a791634 --- /dev/null +++ b/backup.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env sh + +set -e + +. /app/setup.sh + +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.sql" +done + +restic --verbose backup "/backup" diff --git a/docker-compose.yml b/docker-compose.yml index f9721ad..1591261 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,15 +3,4 @@ version: "3" services: backup: build: . - # entrypoint: /app/init.sh - environment: - - RCLONE_REMOTE=gdrive:/backupTest - - RCLONE_CONFIG=/config/rclone.conf - - ENC_PASSWORD=repopw - - MYSQL_USERNAME=dbuser - - MYSQL_PASSWORD=dbPassword - - MYSQL_DUMP_DB=dbName1,dbName2 - - MYSQL_HOST=mariadb - volumes: - - ./mounts:/backup - - ./config:/config \ No newline at end of file + image: djeeberjr/backup \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh deleted file mode 100755 index 4d21945..0000000 --- a/entrypoint.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env sh - -set -e - -[ -z "${RCLONE_REMOTE}" ] && echo "\$RCLONE_REMOTE is not set" && exit 1 -[ -z "${RCLONE_CONFIG}" ] && echo "\$RCLONE_CONFIG is not set" && exit 1 - -if [ -n "${ENC_PASSWORD}" ]; then - RESTIC_PASSWORD="${ENC_PASSWORD}" -elif [ -n "${ENC_PASSWORD_FILE}" ]; then - RESTIC_PASSWORD=$(cat "$ENC_PASSWORD_FILE") -else - echo "\$ENC_PASSWORD or \$ENC_PASSWORD_FILE is not set" && exit 1 -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 - -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.sql" -done - -export RESTIC_PASSWORD - -restic -r "rclone:$RCLONE_REMOTE" --verbose backup "/backup" diff --git a/init.sh b/init.sh deleted file mode 100755 index c1abc4f..0000000 --- a/init.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env sh - -set -e - -[ -z "${RCLONE_REMOTE}" ] && echo "\$RCLONE_REMOTE is not set" && exit 1 -[ -z "${RCLONE_CONFIG}" ] && echo "\$RCLONE_CONFIG is not set" && exit 1 - -if [ -n "${ENC_PASSWORD}" ]; then - RESTIC_PASSWORD="${ENC_PASSWORD}" -elif [ -n "${ENC_PASSWORD_FILE}" ]; then - RESTIC_PASSWORD=$(cat "$ENC_PASSWORD_FILE") -else - echo "\$ENC_PASSWORD or \$ENC_PASSWORD_FILE is not set" && exit 1 -fi - -echo "Init new repo" - -export RESTIC_PASSWORD - -restic -r "rclone:$RCLONE_REMOTE" init - -echo "Created new repo at $RCLONE_REMOTE" diff --git a/interactive.sh b/interactive.sh new file mode 100755 index 0000000..2464679 --- /dev/null +++ b/interactive.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +. /app/setup.sh + +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 diff --git a/setup.sh b/setup.sh new file mode 100755 index 0000000..3e9ea80 --- /dev/null +++ b/setup.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env sh + +set -e + +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 + +export RCLONE_REMOTE +export RESTIC_PASSWORD +export MYSQL_PASSWORD_ACTUAL +export RESTIC_REPOSITORY