diff --git a/b2backup.sh b/b2backup.sh new file mode 100644 index 0000000..b59a6fb --- /dev/null +++ b/b2backup.sh @@ -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 "
" >> $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
\ No newline at end of file
diff --git a/b2mount.sh b/b2mount.sh
new file mode 100644
index 0000000..277ba1a
--- /dev/null
+++ b/b2mount.sh
@@ -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
\ No newline at end of file
diff --git a/config-example.txt b/config-example.txt
new file mode 100644
index 0000000..6301c15
--- /dev/null
+++ b/config-example.txt
@@ -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"
\ No newline at end of file
diff --git a/mailfooter.txt b/mailfooter.txt
new file mode 100644
index 0000000..dc684c5
--- /dev/null
+++ b/mailfooter.txt
@@ -0,0 +1,3 @@
+
+ + diff --git a/mailheader.txt b/mailheader.txt new file mode 100644 index 0000000..e48f9ee --- /dev/null +++ b/mailheader.txt @@ -0,0 +1,5 @@ +MIME-Version: 1.0 +Content-Type: text/html +Content-Disposition: inline + + diff --git a/vmbackup.sh b/vmbackup.sh new file mode 100644 index 0000000..e5abed6 --- /dev/null +++ b/vmbackup.sh @@ -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 "
" >> $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
\ No newline at end of file