106 lines
2 KiB
Bash
Executable file
106 lines
2 KiB
Bash
Executable file
#!/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
|
|
|