Initial Commit

This commit is contained in:
Russ Long 2020-04-08 15:09:44 -04:00
parent 74ea0e085f
commit f7ce0b65f5
6 changed files with 142 additions and 0 deletions

44
b2backup.sh Normal file
View File

@ -0,0 +1,44 @@
#!/bin/bash
MAILFILE="/tmp/mailtxt"
SERVER_NAME=$HOSTNAME
SENDER=$(whoami)
USER="noreply"
MAILHEADER="/opt/b2backup/mailheader.txt"
MAILFOOTER="/opt/b2backup/mailfooter.txt"
. ${PWD}/config.txt
export B2_ACCOUNT_ID="$B2_ACCOUNT_ID"
export B2_ACCOUNT_KEY="$B2_ACCOUNT_KEY"
export RESTIC_REPOSITORY="$RESTIC_REPOSITORY"
export RESTIC_PASSWORD_FILE="$RESTIC_PASSWORD_FILE"
#dump dbs:
#Uncomment double-commented lines to enable dumping of all MySQL databases, MySQL auth is up to you to configure.
#remove old dumps
##find /backup/sqldumps -type f -mtime +7 | xargs rm -f
#backup dbs to one massive file
##/usr/bin/mysqldump -u root --single-transaction --quick --lock-tables=false --all-databases > /backup/sqldumps/full-backup-$(date +"%Y-%m-%d_%H-%M-%S").sql
#backup dbs to separate files
##for DB in $(mysql -e 'show databases' -s --skip-column-names); do
## mysqldump $DB > "/backup/sqldumps/$DB-$(date +"%Y-%m-%d_%H-%M-%S").sql";
##done
/usr/bin/restic backup $BACKUPDATA
/usr/bin/restic forget --keep-last $KEEPLAST --keep-daily $KEEPDAILY --keep-weekly $KEEPWEEKLY --keep-monthly $KEEPMONTHLY --keep-yearly $KEEPYEARLY
echo "Backup Complete. Currently available snapshots:" > $MAILFILE
echo "<pre style='font:monospace'>" >> $MAILFILE
echo "$(/usr/bin/restic snapshots)" >> $MAILFILE
(
echo "Subject: $EMAILSUB"
echo "From: $SENDER"
echo "To: $RECIPIENT"
cat $MAILHEADER $MAILFILE $MAILFOOTER
) | sendmail -t
rm $MAILFILE
for var in $(awk -F"=" '{print $1}' ${PWD}/config.txt); do
unset $var
done

19
b2mount.sh Normal file
View File

@ -0,0 +1,19 @@
#!/bin/bash
. /opt/b2backup/b2config.txt
export B2_ACCOUNT_ID="$B2_ACCOUNT_ID"
export B2_ACCOUNT_KEY="$B2_ACCOUNT_KEY"
export RESTIC_REPOSITORY="$RESTIC_REPOSITORY"
export RESTIC_PASSWORD_FILE="$RESTIC_PASSWORD_FILE"
RESTOREDIR="$RESTORE_DIR"
mkdir -p $RESTOREDIR
/usr/bin/restic mount $RESTOREDIR &
echo "Backups mounted to $RESTOREDIR"
echo ""
echo "Run umount $RESTOREDIR when finished"
echo ""
for var in $(awk -F"=" '{print $1}' ${PWD}/config.txt); do
unset $var
done

17
config-example.txt Normal file
View File

@ -0,0 +1,17 @@
EMAILSUB="SERVERANAME Backup Complete"
RECIPIENT="email@addre.ss"
BACKUPDATA="/path/to/dir1 /path/to/dir2 --exclude /path/to/exclude1 --exclude /path/to/exclude2"
KEEPLAST="7"
KEEPDAILY="0"
KEEPWEEKLY="4"
KEEPMONTHLY="2"
KEEPYEARLY="2"
B2_ACCOUNT_ID=""
B2_ACCOUNT_KEY=""
RESTIC_REPOSITORY=""
RESTIC_PASSWORD_FILE="/path/to/restic/password/file"
RESTORE_DIR="/path/to/mount/snapshot/repo"
VM_EMAILSUB="VM Backups Complete on SERVERNAME"
VM_XML_PATH="/path/to/backup/vm/xml"
VM_SNAP_PATH="/path/to/backup/vm/snapshots
VM_KEEP_BACKUP_DAYS="7"

3
mailfooter.txt Normal file
View File

@ -0,0 +1,3 @@
</pre>
</body>
</html>

5
mailheader.txt Normal file
View File

@ -0,0 +1,5 @@
MIME-Version: 1.0
Content-Type: text/html
Content-Disposition: inline
<html>
<body>

54
vmbackup.sh Normal file
View File

@ -0,0 +1,54 @@
#!/bin/bash
MAILFILE="/tmp/vmmailtxt"
SERVER_NAME=$HOSTNAME
SENDER=$(whoami)
USER="noreply"
MAILHEADER="/opt/b2backup/mailheader.txt"
MAILFOOTER="/opt/b2backup/mailfooter.txt"
. ${PWD}/config.txt
export XMLPATH="$VM_XML_PATH"
export SNAP_PATH="$VM_SNAP_PATH"
export KEEP_DAYS="$VM_KEEP_BACKUP_DAYS"
VMS=$(virsh list --all --name)
for vm in $(virsh list --all --name); do
DATE=$(date +"%Y-%m-%d_%H-%M-%S")
DEVPATH=$(virsh domblklist $vm | grep -i vda | awk '{print $2}')
virsh dumpxml --security-info $vm > $XMLPATH/${vm}_${DATE}.xml
virsh snapshot-create-as --domain $vm ${vm}_temp_$DATE --diskspec vda,file=$SNAP_PATH/${vm}_temp_$DATE.qcow2 --disk-only --quiesce --atomic --no-metadata
tar czvf $SNAP_PATH/${vm}_$DATE.qcow2.tar.gz $DEVPATH
virsh blockcommit $vm vda --active --verbose --pivot
rm -f $SNAP_PATH/${vm}_temp_$DATE.qcow2
done
for xmldel in $(find $XMLPATH -type f -mtime +$KEEP_DAYS); do
rm -f $xmldel
done
for snapdel in $(find $SNAP_PATH -type f -mtime +$KEEP_DAYS); do
rm -f $snapdel
done
echo "VM Backup Complete. Currently available snapshots:" > $MAILFILE
echo "<pre style='font:monospace'>" >> $MAILFILE
echo "XML:" >> $MAILFILE
echo "$(ls -lh $XMLPATH)" >> $MAILFILE
echo "DISKS:" >> $MAILFILE
echo "$(ls -lh $SNAP_PATH)" >> $MAILFILE
(
echo "Subject: $VM_EMAILSUB"
echo "From: $SENDER"
echo "To: $RECIPIENT"
cat $MAILHEADER $MAILFILE $MAILFOOTER
) | sendmail -t
rm $MAILFILE
for var in $(awk -F"=" '{print $1}' ${PWD}/config.txt); do
unset $var
done