commit 33a79b7b31a488542dba054ebce503f69e32cefc Author: root Date: Wed Sep 19 08:32:47 2018 +0000 INIT diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..13692b2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +config +software.list +backup.log +id_backup +data/ diff --git a/after.sh b/after.sh new file mode 100755 index 0000000..f68d00a --- /dev/null +++ b/after.sh @@ -0,0 +1,14 @@ +#! /bin/bash + +## +## Send mail to admin +## + +#mail -a "From: "$HOST" Backup <"$HOST"@server.de>" -s "Backup | "$HOST $MAIL < $LOG + +if [ ${global_exit} -ne 0 ]; +then + echo ${global_exit} +# curl -X POST 'https://api.telegram.org/bot$TELEGRAM_API_TOKEN/sendMessage?chat_id=$TELEGRAM_CHAT_ID&text="$(cat $LOG)"' + wget "https://api.telegram.org/bot$TELEGRAM_API_TOKEN/sendMessage" --post-data="chat_id=$TELEGRAM_CHAT_ID&text='$(cat $LOG)'" +fi diff --git a/before.sh b/before.sh new file mode 100755 index 0000000..9939542 --- /dev/null +++ b/before.sh @@ -0,0 +1,15 @@ +#! /bin/bash + +## +## Create list of installed software +## + +dpkg --get-selections > $DIR_DATA/software.list + +## +## Create database dumps +## + +#echo "Creating database dumps ..." +#/bin/bash /root/backup/dbdump.sh + diff --git a/config.exp b/config.exp new file mode 100644 index 0000000..a4bc1b9 --- /dev/null +++ b/config.exp @@ -0,0 +1,5 @@ +REPOSITORY="ssh://borg@backupserver:22/media/hdd/backup" +PASSPHRASE="" +TELEGRAM_API_TOKEN="" +TELEGRAM_CHAT_ID="@..." + diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..bb295c9 --- /dev/null +++ b/run.sh @@ -0,0 +1,97 @@ +#!/bin/bash + +## +## Save backup to directory named after hostname +## + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" +DIR_DATA=$DIR"/data" + +# load config +. $DIR/config + +LOG=$DIR"/backup.log" +export BORG_REPO=$REPOSITORY +export BORG_PASSPHRASE=$PASSPHRASE +export BORG_RSH="ssh -i $DIR/id_backup" + +## +## Write output to logfile +## + +exec > >(tee -i ${LOG}) +exec 2>&1 + +# some helpers and error handling: +info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; } +trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM + +echo "###### Starting backup on $(date) ######" + +. $DIR/before.sh + +## +## Sync backup data +## + +echo "Syncing backup files ..." +borg create \ + --verbose \ + --filter AME \ + --list \ + --stats \ + --show-rc \ + --compression lz4 \ + --exclude-caches \ + --exclude '/home/*/.cache/*' \ + --exclude '/var/cache/*' \ + --exclude '/var/tmp/*' \ + \ + ::'{hostname}-{now}' \ + /etc \ + /home \ + /root \ + /var \ + $DIR_DATA \ + /media/data/live + +backup_exit=$? + +echo "###### Finished backup on $(date) ######" + +echo "###### Pruning repository on $(date) ######" + +# Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 monthly +# archives of THIS machine. The '{hostname}-' prefix is very important to +# limit prune's operation to this machine's archives and not apply to +# other machines' archives also: + +borg prune \ + --list \ + --prefix '{hostname}-' \ + --show-rc \ + --keep-daily 7 \ + --keep-weekly 4 \ + --keep-monthly 6 \ + +prune_exit=$? + +echo "###### Finished pruning on $(date) ######" + +# use highest exit code as global exit code +global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit )) + +if [ ${global_exit} -eq 1 ]; +then + echo "Backup and/or Prune finished with a warning" +fi + +if [ ${global_exit} -gt 1 ]; +then + echo "Backup and/or Prune finished with an error" +fi + +. $DIR/after.sh + +exit ${global_exit} +