#!/bin/bash # Cronjob needs to be on one line, spaced out for example/ in-case running cronjob from a script that just runs the below # e.g. 0 0 * * * /home/nathan/backupScripts/docker_backup.sh \ # -c "duckdns homer" \ # -d /home/samba/share/Docker_prod \ # -b /home/nathan/testBack \ # -r pi2 \ # -R ~/backups/pi1/docker \ # -N 0 # Flags while getopts c:d:b:r:R: flag do case "${flag}" in c) CONTAINER_DIRS=${OPTARG};; # Will accept an 'array', just individual names spaced within quotes. i.e. "homer npm vaultwarden" d) DIR=${OPTARG};; # /home/nathan/docker typically b) BACKUP_DIR=${OPTARG};; # Where the backup is on local host r) REMOTE=${OPTARG};; # user@remote or alias (from SSH config), prefer to use alias, as it can deal with port differences R) REMOTE_DIR=${OPTARG};; # Location of remote backup i.e. /backup/borg/HOSTNAME/docker (then /npm, /vaultwarden, etc.) # N) NOW=${OPTARG};; # 1/0 for yes/no do offsite backup now esac done # Borg assistance: https://borgbackup.readthedocs.io/en/stable/quickstart.html # export BORG_PASSPHRASE='' # If using encryption in borg, ignoring for now, to just have it work # Script DIRS=($CONTAINER_DIRS) # Put the CONTAINER_DIRS passed into an array that can be looped # DIRS=(homer npm) to hardcode #DIRS=( "$DOCKER"/*/ ) for i in "${DIRS[@]}" do # Stop docker containers before backup incase any moving parts echo $i # If local directory doesn't exist for backup if [ ! -d "$BACKUP_DIR/$i" ]; then # Create new repo # --encryption=none # --encryption=repokey for encryption with key on server/in repo borg init --encryption=none $BACKUP_DIR/$i # Will create repo if it doesn't exist 'A repo already exist...' 'error' otherwise # borg init $REMOTE:$REMOTE_DIR/$i # Will create repo if it doesn't exist EXAMPLE for future, will need to do a different check too ig # --encryption=repokey after init if you want encryption # TODO: If using encryption, backup the fi docker compose stop # LOCAL export BORG_REPO=$BACKUP_DIR/$i borg create ::{hostname}-{now} $DIR/$i #rdiff-backup $DIR/$i $BACKUP_DIR/$i # If a directory doesn't exist, it get created too # Keep last 24 hours of backups, 7 daily backups (one a day/week), 4 weekly (one a week for a month), and 6 monthly, and 1 a year # Not 100% on this, but will keep this for now borg prune \ --glob-archives '{hostname}-*' \ --keep-hourly 24 \ --keep-daily 7 \ --keep-weekly 4 \ --keep-monthly 6 \ --keep-yearly 1 #rdiff-backup --force --remove-older-than 1M $BACKUP_DIR/$i # Keep 1 month worth of backups # OFFSITE # TODO: This will be better as an actual unique off-site borg backup docker compose start done # Nightly backup offsite (TEMP FOR NOW!!) # If time is 00:00/midnight, rsync the entire directory of borg backups # Inefficient for borg, but for now it'll work, will need a seperate borg on remote in future # So in future, will just run a borg update straight to the server if [ "$TIME" = 0000 ] || [ "$NOW" = 1 ] then # Create the remote directory for backup if it doesn't exist ssh $REMOTE mkdir -p $REMOTE_DIR # Copy the entire backup directory accross # -e ssh makes it secure rsync -azh -e ssh \ --delete \ $BACKUP_DIR/ \ $REMOTE:$REMOTE_DIR # End-slash on backupDir here, as want to just backup the contents of the directory, not the directory itself fi