97 lines
3.4 KiB
Bash
Executable File
97 lines
3.4 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
backup_name=systemonly
|
|
backup_dir=/backups/borg/$backup_name/
|
|
log_dir=/backups/log/
|
|
unique_identifier=Manjaro18
|
|
|
|
#########################################################################
|
|
|
|
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
|
|
|
|
# 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,1 \
|
|
--exclude-caches \
|
|
--exclude '/home/*/.cache/*' \
|
|
--exclude '/home/*/.local/share/Trash/*' \
|
|
--exclude '/home/pratik/Downloads/Videos/*' \
|
|
--exclude '/home/pratik/Downloads/Study/Video/*' \
|
|
--exclude '/var/cache/*' \
|
|
--exclude '/var/tmp/*' \
|
|
--exclude "$backup_dir" \
|
|
--exclude "$log_file" \
|
|
\
|
|
::"$unique_identifier-$backup_name-{now}" \
|
|
/etc \
|
|
/root \
|
|
/var \
|
|
>> $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
|
|
|
|
exit ${global_exit} |