#!/bin/bash _err=0 which 7z >/dev/null || let _err="_err+1" which s-nail >/dev/null || let _err="_err+2" if [[ $_err -ge 2 ]]; then echo "Please install s-nail.";fi if [[ $_err -eq 1 ]] || [[ $_err -eq 3 ]]; then echo "Please install p7zip-full.";fi [ $_err -ge 1 ] && exit; source /path/to/your/smtp-creds.sh if [ $? -ne 0 ]; then echo "Please fill in the path to your smtp-creds.sh file.";exit;fi if [ $smtp_defaults -eq 1 ]; then echo "Please edit your smtp-creds file.";exit;fi backupdate=`date +%Y-%m-%d_%H-%M-%S%z` logfile="/mnt/b/Backups/ProgramFiles/ProgramFiles-$backupdate.log" backupdir="/mnt/b/Backups/ProgramFiles" isodate=`date +%Y-%m-%d_%H-%M-%S%z` echo -e "[$isodate] Backup script starting" >> $logfile isodate=`date +%Y-%m-%d_%H-%M-%S%z` echo -e "[$isodate] Checking for backups older than 7 days..." >> $logfile oldbackups=`find $backupdir -type f -mtime +7 -exec ls -lh {} \;` isodate=`date +%Y-%m-%d_%H-%M-%S%z` echo -e "[$isodate] Found the following backups to be deleted:\n" >> $logfile echo -e $oldbackups | sed 's/\.7z\ /\.7z\n/g' >> $logfile isodate=`date +%Y-%m-%d_%H-%M-%S%z` echo -e "\n[$isodate] Deleting the above backups..." >> $logfile echo -e $oldbackups | sed 's/\.7z\ /\.7z\n/g' | rev | cut -d' ' -f1 | rev | grep 1 | xargs rm -f isodate=`date +%Y-%m-%d_%H-%M-%S%z` echo -e "[$isodate] Old backups deleted." >> $logfile echo -e "[$isodate] Starting the backup...\n\n" >> $logfile # Run the backup 7z a $backupdir/ProgramFiles-$backupdate.7z -x\!/mnt/c/Program\ Files/Common\ Files/* /mnt/c/Program\ Files /mnt/c/Program\ Files\ \(x86\) >> $logfile isodate=`date +%Y-%m-%d_%H-%M-%S%z` echo -e "[$isodate] The backup has completed." >> $logfile backupsize=`ls -lh $backupdir/ProgramFiles-$backupdate.7z | cut -d' ' -f5` echo -e "[$isodate] The backup file's size is $backupsize." >> $logfile isodate=`date +%Y-%m-%d_%H-%M-%S%z` echo -e "\n\n\n[$isodate] Backup script completed, sending email." >> $logfile cat $logfile | s-nail -s "Weekly Program Files Backup Results" -S smtp-use-starttls -S ssl-verify=ignore -S smtp-auth=login -S smtp=smtp://$smtp_relay:587 -S from="$smtp_from" -S smtp-auth-user=$smtp_user -S smtp-auth-password=$smtp_pass -S ssl-verify=ignore $smtp_to >> $logfile