Files
borg-backup/backup-Wordpress.sh
2019-10-05 21:01:44 +05:30

100 lines
3.6 KiB
Bash
Executable File

#!/bin/sh
backup_name=wp-baseline
backup_dir=/home/pratik/backup/borg/
log_dir=/home/pratik/logs/borg/
unique_identifier=pratik
#########################################################################
readonly backup_name
readonly backup_dir
readonly log_dir
readonly unique_identifier
# Setting this, so you won't be asked for your repository passphrase:
export BORG_PASSPHRASE='d00x^UXryuEYTwPuZwQh'
# Setting this, so the repo does not need to be given on the commandline:
export BORG_REPO=$backup_dir$backup_name
# some helpers and error handling:
log_file=$log_dir$backup_name-$(date +%d%m%Y-%H%M%S)
readonly log_file
# if borg is currently running - stop sync
if [ $(pidof borg | wc -w) -eq 1 ]
then
echo "Another Backup is currently running. Exiting." >> $log_file 2>&1
exit 2
fi
info() { printf "\n%s %s\n\n" "$( date )" "$*" >> $log_file 2>&1; }
trap 'echo $( date ) Backup interrupted >> $log_file 2>&1; exit 2' INT TERM
info "Starting backup" >> $log_file 2>&1
# Backup the most important directories into an archive named after
# the machine this script is currently running on:
borg create \
--verbose \
--filter AME \
--list \
--stats \
--show-rc \
--compression zstd,3 \
--exclude-caches \
--exclude '/home/*/.cache/*' \
--exclude '/home/*/.local/share/Trash/*' \
--exclude '/var/cache/*' \
--exclude '/var/tmp/*' \
--exclude "$backup_dir" \
--exclude "$log_file" \
::"$unique_identifier-$backup_name-{now}" \
/etc \
/root \
/var \
/srv \
/opt \
/usr/local \
/usr/share/nginx \
/home \
>> $log_file 2>&1 \
backup_exit=$?
info "Pruning repository" >> $log_file 2>&1
# 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 \
--stats \
--prefix "$unique_identifier-$backup_name" \
--show-rc \
--keep-within 2d \
--keep-last 5 \
--keep-daily 7 \
>> $log_file 2>&1 \
prune_exit=$?
# use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
if [ ${global_exit} -eq 1 ];
then
info "Backup and/or Prune finished with a warning" >> $log_file 2>&1
fi
if [ ${global_exit} -gt 1 ];
then
info "Backup and/or Prune finished with an error" >> $log_file 2>&1
fi
echo Status Code - $global_exit >> $log_file 2>&1
exit ${global_exit}