Archived
Compare commits
2
Commits
master
..
f93d26b9c1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f93d26b9c1
|
||
|
|
8828e1235c
|
+1
-7
@@ -4,11 +4,8 @@
|
|||||||
# File with hosts and their backup source paths
|
# File with hosts and their backup source paths
|
||||||
HOSTS="$CURDIR"/hosts.csv
|
HOSTS="$CURDIR"/hosts.csv
|
||||||
|
|
||||||
# Temporary download destination for backups
|
|
||||||
TEMPDIR=/tmp/uberspace-backup
|
|
||||||
|
|
||||||
# root dir where backups shall be saved to
|
# root dir where backups shall be saved to
|
||||||
BACKUPDIR=/mnt/remotesrv/uberspace
|
BACKUPDIR=/var/backups/uberspace
|
||||||
|
|
||||||
# GPG fingerprint of key used for encryption
|
# GPG fingerprint of key used for encryption
|
||||||
GPG=6775E8DDD8CEABCC83E38CEHE6334BCA29DF8192
|
GPG=6775E8DDD8CEABCC83E38CEHE6334BCA29DF8192
|
||||||
@@ -18,6 +15,3 @@ MAXBAK=3
|
|||||||
|
|
||||||
# SSH key
|
# SSH key
|
||||||
#SSH_KEY="~/.ssh/mykey_rsa"
|
#SSH_KEY="~/.ssh/mykey_rsa"
|
||||||
|
|
||||||
# Logfile. Default: $CURDIR/backup.log
|
|
||||||
# LOG_FILE=/var/log/uberspace-backup.log
|
|
||||||
|
|||||||
@@ -68,4 +68,6 @@ while read -r line; do
|
|||||||
echo "[SUCCESS] SSH login possible for ${RHOST}."
|
echo "[SUCCESS] SSH login possible for ${RHOST}."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
echo
|
||||||
|
|
||||||
done < "$HOSTS"
|
done < "$HOSTS"
|
||||||
|
|||||||
+18
-29
@@ -9,15 +9,9 @@
|
|||||||
#
|
#
|
||||||
########################################################################
|
########################################################################
|
||||||
|
|
||||||
# Fail fast on errors
|
|
||||||
set -Eeuo pipefail
|
|
||||||
|
|
||||||
# Set correct UTF-8 encoding (for FreeBSD jail)
|
# Set correct UTF-8 encoding (for FreeBSD jail)
|
||||||
export LC_ALL=en_US.UTF-8
|
export LC_ALL=en_US.UTF-8
|
||||||
|
|
||||||
# Initialise variables
|
|
||||||
LOG_FILE=
|
|
||||||
|
|
||||||
CURDIR=$(dirname "$(readlink -f "$0")")
|
CURDIR=$(dirname "$(readlink -f "$0")")
|
||||||
if [ ! -e "$CURDIR"/config.cfg ]; then echo "Missing config.cfg file. Edit and rename config.cfg.sample"; exit 1; fi
|
if [ ! -e "$CURDIR"/config.cfg ]; then echo "Missing config.cfg file. Edit and rename config.cfg.sample"; exit 1; fi
|
||||||
source "$CURDIR"/config.cfg
|
source "$CURDIR"/config.cfg
|
||||||
@@ -32,13 +26,11 @@ else
|
|||||||
SSH_KEY=~/.ssh/id_rsa
|
SSH_KEY=~/.ssh/id_rsa
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -z "${LOG_FILE}" ]; then
|
ARG1="$1"
|
||||||
# defaults
|
|
||||||
LOG_FILE="$CURDIR"/backup.log
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Get current date
|
# Get current date
|
||||||
DATE=$(date +"%Y-%m-%d_%H-%M")
|
DATE=$(date +"%Y-%m-%d_%H-%M")
|
||||||
|
LOG="$CURDIR"/backup.log
|
||||||
|
|
||||||
function trim {
|
function trim {
|
||||||
sed -r -e 's/^[[:space:]]*//g' -e 's/[[:space:]]*$//g'
|
sed -r -e 's/^[[:space:]]*//g' -e 's/[[:space:]]*$//g'
|
||||||
@@ -50,18 +42,17 @@ function pdate {
|
|||||||
function logecho {
|
function logecho {
|
||||||
# Echo string and copy it to log while attaching the current date
|
# Echo string and copy it to log while attaching the current date
|
||||||
echo "$(pdate) $*"
|
echo "$(pdate) $*"
|
||||||
echo "$(pdate) $*" >> "$LOG_FILE"
|
echo "$(pdate) $*" >> "$LOG"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Loop over all hosts
|
|
||||||
while read -r line; do
|
while read -r line; do
|
||||||
# if line is a comment or blank, go to next line
|
# if line is a comment or blank, go to next line
|
||||||
if echo "$line" | grep -qE "^\s*(#|$)"; then continue; fi
|
if echo "$line" | grep -qE "^\s*(#|$)"; then continue; fi
|
||||||
|
|
||||||
RHOST=$(echo "$line" | cut -d";" -f1 | trim)
|
RHOST=$(echo "$line" | cut -d";" -f1 | trim)
|
||||||
|
|
||||||
# Jump to next line if this line's host does not match host of first argument (if given)
|
# Jump to next line if this line's host does not match host of ARG1 (if given)
|
||||||
if [[ "${1-}" != "" ]] && [[ "${1-}" != "${RHOST}" ]]; then
|
if [[ "${ARG1}" != "" ]] && [[ "${ARG1}" != "${RHOST}" ]]; then
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
# Task ssh-checker.sh to check this host
|
# Task ssh-checker.sh to check this host
|
||||||
@@ -88,10 +79,7 @@ while read -r line; do
|
|||||||
|
|
||||||
logecho "${RHOST}: Starting backups"
|
logecho "${RHOST}: Starting backups"
|
||||||
|
|
||||||
logecho "${RHOST}: Deleting host's temporary directories in ${TEMPDIR}"
|
NORDIR=$(echo "$ALLRDIR" | grep -o "|" | wc -l)
|
||||||
rm -rf "${TEMPDIR:?}/${RHOST:?}/"*
|
|
||||||
|
|
||||||
NORDIR=$(echo "$ALLRDIR" | grep -o "|" | wc -l || true)
|
|
||||||
NORDIR=$(($NORDIR + 1))
|
NORDIR=$(($NORDIR + 1))
|
||||||
|
|
||||||
# Loop through all backup sources
|
# Loop through all backup sources
|
||||||
@@ -119,9 +107,12 @@ while read -r line; do
|
|||||||
# Example:
|
# Example:
|
||||||
# DEST=/tmp/uberspace-backup/user@example.com/2019-01-01/virtual
|
# DEST=/tmp/uberspace-backup/user@example.com/2019-01-01/virtual
|
||||||
# DEST_FINAL=/media/Uberspace/user@example.com/2019-01-01/
|
# DEST_FINAL=/media/Uberspace/user@example.com/2019-01-01/
|
||||||
DEST="${TEMPDIR}/${DEST_REL}"
|
DEST="${TEMPDIR}/${DEST}"
|
||||||
DEST_FINAL="$(dirname "${BACKUPDIR}/${DEST_REL}")"
|
DEST_FINAL="$(dirname "${BACKUPDIR}/${DEST_REL}")"
|
||||||
|
|
||||||
|
logecho "DEBUG: $DEST"
|
||||||
|
logecho "DEBUG: $DEST_FINAL"
|
||||||
|
|
||||||
# Set Source directory, and make exception for %mysql
|
# Set Source directory, and make exception for %mysql
|
||||||
SOURCE="${RDIR}"
|
SOURCE="${RDIR}"
|
||||||
if [ "${RDIR}" == "mysql" ]; then
|
if [ "${RDIR}" == "mysql" ]; then
|
||||||
@@ -152,17 +143,15 @@ while read -r line; do
|
|||||||
|
|
||||||
# Push encrypted backup to final backup destination
|
# Push encrypted backup to final backup destination
|
||||||
logecho "${RHOST}: Moving $(basename "${DEST}") to ${DEST_FINAL}"
|
logecho "${RHOST}: Moving $(basename "${DEST}") to ${DEST_FINAL}"
|
||||||
cp "${DEST}".tar.gpg "${DEST_FINAL}/"
|
mv "${DEST}".tar.gpg "${DEST_FINAL}/"
|
||||||
rm "${DEST}".tar.gpg
|
|
||||||
|
|
||||||
done # End of loop through all backup sources
|
# Delete all old directories except the $MAXBAK most recent
|
||||||
|
if [ $(ls -tp "${BACKUPDIR}"/"${RHOST}"/ | grep '/$' | wc -l | tr -d ' ') -gt $MAXBAK ]; then
|
||||||
# Delete all old directories except the $MAXBAK most recent
|
oldbackups=$(ls -tp "${BACKUPDIR}"/"${RHOST}"/ | grep '/$' | tail -n +$(($MAXBAK + 1)))
|
||||||
if [ $(ls -tp "${BACKUPDIR}"/"${RHOST}"/ | grep '/$' | wc -l | tr -d ' ') -gt $MAXBAK ]; then
|
logecho "${RHOST}: Removing older backups of $(basename "${DEST}"): ${oldbackups}"
|
||||||
oldbackups=$(ls -tp "${BACKUPDIR}"/"${RHOST}"/ | grep '/$' | tail -n +$(($MAXBAK + 1)))
|
ls -tpd "${BACKUPDIR}"/"${RHOST}"/* | grep '/$' | tail -n +$(($MAXBAK + 1)) | xargs -0 | xargs rm -r --
|
||||||
logecho "${RHOST}: Removing older backup directories: ${oldbackups}"
|
fi
|
||||||
ls -tpd "${BACKUPDIR}"/"${RHOST}"/* | grep '/$' | tail -n +$(($MAXBAK + 1)) | xargs -0 | xargs rm -r --
|
done
|
||||||
fi
|
|
||||||
|
|
||||||
done < "$HOSTS"
|
done < "$HOSTS"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user