Import of backup script from various sources

This commit is contained in:
Geoffrey Frogeye 2019-08-01 21:29:20 +02:00
parent 077f53bbf1
commit ad22ef40cf
2 changed files with 170 additions and 0 deletions

105
backup.sh Executable file
View file

@ -0,0 +1,105 @@
#!/usr/bin/env bash
# Backup script v3
# Uses restic
cd `dirname ${BASH_SOURCE-$0}`
RUNFILE=/var/run/bkp
if [ -e $RUNFILE ] && ps -p $(cat $RUNFILE) > /dev/null
then
>&2 echo "Another backup instance is running. Skipping"
exit 1
fi
echo $$ > $RUNFILE
echo → Backup started
source config.sh
if [ ! -z ${ETCKEEPER_COMMIT_MESSAGE+x} ]
then
etckeeper commit "$ETCKEEPER_COMMIT_MESSAGE" &> /dev/null &
fi
# Dumping acl
ACLFILE=~/bkp/acl.gz
function bkpACL() {
touch "$ACLFILE"
chmod 700 "$ACLFILE"
sudo getfacl -R "$SRC" 2> /dev/null | gzip > "$ACLFILE"
}
# Dumping database
DBFILE=/var/bkp/psql.sql.gz
function bkpPgsql() {
touch "$DBFILE"
chmod 700 "$DBFILE"
pg_dumpall | gzip > "$DBFILE"
}
# Dumping database
DBFILE=./mysql.sql.gz
function bkpMysql() {
touch "$DBFILE"
chmod 700 "$DBFILE"
mysqldump --defaults-extra-file=mysql.cnf --all-databases --flush-privileges | gzip > "$DBFILE"
}
# Dumping package list
PACFILE=/var/bkp/pacman.list.gz
function bkpPacman() {
touch "$PACFILE"
chmod 700 "$PACFILE"
comm -23 <(pacman -Qeq | sort) <(pacman -Qgq base base-devel | sort) | gzip > "$PACFILE"
}
# Dumping package list
APTFILE=./apt.list.gz
function bkpApt() {
touch "$APTFILE"
chmod 700 "$APTFILE"
apt list --installed 2> /dev/null | gzip > "$APTFILE"
}
echo → Creating snapshots of databases
bkpPgsql &
bkpPacman &
wait
# echo "→ Restic: Transfering files"
#
# $RESTIC_CMD $RESTIC_OPTIONS backup $BACKUP_OPTIONS --cleanup-cache
#
# if [ ! -z ${FORGET_OPTIONS+x} ]
# then
#
# echo "→ Restic: Cleaning up"
#
# $RESTIC_CMD $RESTIC_OPTIONS forget $FORGET_OPTIONS
#
# fi
echo "→ Borg: Transfering files"
TAG="$(date -Isec)"
$BORG_CMD $BORG_OPTIONS create --stats $CREATE_OPTIONS "${BORG_REPO}::${TAG}" $FILES 2>&1
if [ ! -z ${PRUNE_OPTIONS+x} ]
then
echo "→ Borg: Cleaning up"
$BORG_CMD $BORG_OPTIONS prune --stats $PRUNE_OPTIONS $BORG_REPO 2>&1
fi
echo → Remove temporary files
rm "$PACFILE" "$DBFILE"
echo → Backup ended
rm $RUNFILE

65
config.sample.sh Executable file
View file

@ -0,0 +1,65 @@
#!/usr/bin/env bash
# Configuration options for the backup.sh script v4
# TODO Do the v4
cd `dirname ${BASH_SOURCE-$0}`
# Connection options
# Type of connection to the backup server
TYPE="sftp"
# Hostname/IP of the backup server
HOST="bkpuser@backup.example.com"
# Location of the backup directory on the backup server
DEST="/srv/bkp/host"
BDEST="/srv/bkp/host"
# You will need to create a `resticpass` file in this directory
# Exported variable for extra restic manipulation if needed
export RESTIC_REPOSITORY="$TYPE:$HOST:$DEST"
export BORG_REPO="ssh://$HOST$BDEST"
export RESTIC_PASSWORD_FILE="$PWD/resticpass"
export BORG_PASSPHRASE="$(cat $PWD/borgpass)"
# Export options
# Where the various exports will be saved
# The file will be deleted afterwards, so it is only to locate it in the backup
WORKING_DIR="/var/bkp"
# Create a etckeeper commit (make sure /etc is in $FILES for it to be saved) (unset if uneeded)
ETCKEEPER_COMMIT_MESSAGE="Sauvegarde automatique du $(date)"
# Backup options
# Directories to backup
FILES="/"
# Pattern to exclude
EXCLUDE=$(echo --exclude={"/var/cache","**.lock","**/.cache/"})
# Tags to add to the backups
TAGS=$(echo --tag={"auto","v3"})
# Options for the backup command
export BACKUP_OPTIONS="--one-file-system --cleanup-cache $FILES $EXCLUDE $TAGS"
export CREATE_OPTIONS="--one-file-system --compression auto,zstd $EXCLUDE"
# Forget options
# Which snapshots to keep (inclusive)
KEEP="--keep-hourly 24 --keep-daily 7 --keep-weekly 4 --keep-monthly 12 --keep-yearly 10"
# Options for the restic forget command (unset if uneeded)
export FORGET_OPTIONS="$KEEP --prune"
export PRUNE_OPTIONS="$KEEP"
# Executable options
# Location of the restic command (you might want to add sudo in there)
# RESTIC_CMD="$(which restic)"
RESTIC_OPTIONS="--cache-dir /var/cache/restic"
BORG_CMD="$(which borg)"
BORG_OPTIONS=""