Skip to content

Instantly share code, notes, and snippets.

@oxagast
Last active October 21, 2022 14:22
Show Gist options
  • Save oxagast/181c4834d3d02e1c36b720b2e526006b to your computer and use it in GitHub Desktop.
Save oxagast/181c4834d3d02e1c36b720b2e526006b to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Backup to nas and copy to other partition
#
# Marshall Whittaker / oxagast
#
logd=$(date "+%Y%m%d%H%M%S")
echo "$logd Starting backup:" | tee -a $log
# What to backup.
backup_files="/home/marshall"
# log file
log="/var/log/backup.log"
# Where to backup to.
desta="/var/storage/Backups/"
destb="/var/backups/USB/"
destc="/home/marshall/GDrive/Backup/"
# Create archive filename.
#day=$(date +%A)
day=$(date +%d-%m-%y)
hostname=$(hostname -s)
archive_file="backup-$hostname-$day.tar.gz.aes"
# Print start status message.
echo "$logd Backing up $backup_files to $desta/$archive_file ..." | tee -a $log
# Backup the files using tar.
size=$(du -bs /home/marshall | cut -d '/' -f 1)
newsz=$(echo "$size * 0.85" | bc)
szint=$(printf "%.0f" $newsz)
tar --exclude='/home/marshall/.bin/' --exclude='/home/marshall/Work/customers' --exclude='/home/marshall/GDrive' \
--exclude='/home/marshall/.local' --exclude='/home/marshall/.cargo' --exclude='/home/marshall/.cpan' \
--exclude='/home/marshall/snap' --exclude='/home/marshall/.cache' --exclude='/home/marshall/Downloads' \
--exclude='/home/marshall/ISOs' --exclude='/home/marshall/Photos' --exclude='/home/marshall/Videos' \
--exclude='/home/marshall/Pictures' --exclude='/home/marshall/VMs' --exclude='/home/marshall/Work/customers/rahaf' \
-cvf - $backup_files 2>/tmp/file.tar.backup.lst | pv -ptrabI -N "Generating backup file" -s $szint | gzip | \
openssl enc -aes-256-cbc -e -out "$desta/$archive_file" -pbkdf2 -pass pass:"nicetryskid"
echo -n "$logd Files backed up: "
cat /tmp/file.tar.backup.lst | wc -l
echo "$logd Moving offsite..." | tee -a $log
rclone mount GDrive: /home/marshall/GDrive &
rsync -Px "$desta/$archive_file" "$destb/$archive_file"
rsync -Px "$desta/$archive_file" "$destc/$archive_file"
echo "$logd Removing out of date backups..." | tee -a $log
number_current=$(find /home/marshall/GDrive/Backup/ -name "backup-$hostname-*.tar.gz*" -type f -mtime -16 | wc -l)
if [[ $number_current -ge 1 ]]; then
find /home/marshall/GDrive/Backup/ -name "backup-$hostname-*.tar.gz*" -type f -mtime +16 -delete
echo "$logd Removed stale backup..." | tee -a $log
fi
echo "$logd Checking sizes..." | tee -a $log
du -b "$desta/$archive_file" "$destb/$archive_file" "$destc/$archive_file" | tee -a $log
echo "$logd Dismounting offsite backup server"| tee -a $log
umount /home/marshall/GDrive
# sending notification to desktop
kdialog --passivepopup "Backup Complete at $desta/$archive_file" --icon drive-multidisk --title "Backup"
echo "$logd Backup complete!" | tee -a $log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment