From 8507731caf0ab480a33b7bc788845bd3b116ac9a Mon Sep 17 00:00:00 2001 From: Matthew McKinnon Date: Tue, 6 Feb 2018 21:36:08 +1000 Subject: [PATCH 1/7] Added godaddy-ddns.sh script for Dynamic A Recrod Update with godaddy.com Updated .gitignore to exclude godaddy_keys containing API KEY and SECRET --- .gitignore | 1 + godaddy-ddns.sh | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100755 godaddy-ddns.sh diff --git a/.gitignore b/.gitignore index 473656d..30ef9c9 100755 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ office365.conf +godaddy_keys diff --git a/godaddy-ddns.sh b/godaddy-ddns.sh new file mode 100755 index 0000000..62bc50e --- /dev/null +++ b/godaddy-ddns.sh @@ -0,0 +1,72 @@ +#!/bin/bash + +# This script is used to check and update your GoDaddy DNS server to the IP address of your current internet connection. +# Special thanks to mfox for his ps script +# https://github.com/markafox/GoDaddy_Powershell_DDNS +# +# First go to GoDaddy developer site to create a developer account and get your key and secret +# +# https://developer.godaddy.com/getstarted +# Be aware that there are 2 types of key and secret - one for the test server and one for the production server +# Get a key and secret for the production server +# +# +#Create a godaddy_keys file with the lines +# +# KEY +# SECRET +# +# +#Update the first 4 variables with your information + + +MAILTO="support@comprofix.com" +MAILFROM="support@comprofix.com" +SMTP="mail.comprofix.com" + +domain="comprofix.com" # your domain +name="home" # name of A record to update +key=$(cat godaddy_keys | grep KEY | awk '{ print $2 }') # key for godaddy developer API +secret=$(cat godaddy_keys | grep SECRET | awk '{ print $2 }') # secret for godaddy developer API + +headers="Authorization: sso-key $key:$secret" + +# echo $headers + +result=$(curl -s -X GET -H "$headers" "https://api.godaddy.com/v1/domains/$domain/records/A/$name") + +# echo $result + +dnsIp=$(echo $result | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b") + +# DEBUG: Remove hash from below line +# echo "dnsIp:" $dnsIp + +# Get public ip address there are several websites that can do this. +ret=$(curl -s GET "http://ipinfo.io/json") +currentIp=$(echo $ret | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b") + +# DEBUG: Remove hash from below line +# echo "currentIp:" $currentIp + + if [ $dnsIp != $currentIp ]; + then + # echo "Ips are not equal" + request='{"data":"'$currentIp'","ttl":600}' + # echo $request + nresult=$(curl -i -s -X PUT \ + -H "$headers" \ + -H "Content-Type: application/json" \ + -d $request "https://api.godaddy.com/v1/domains/$domain/records/A/$name") + # echo $nresult + + sendemail -o tls=no -s $SMTP -t $MAILTO -f "$name.$domain <$MAILFROM>" -u "$name.$domain IP has been updated" -m " + + $name.$domain IP has been updated + + $name.$domain IP is now: $currentIp + + + " -q + +fi From cd551d04d843af2218ec66d58f8a1e4aac8c4d63 Mon Sep 17 00:00:00 2001 From: Matthew McKinnon Date: Fri, 4 May 2018 15:40:28 +1000 Subject: [PATCH 2/7] Updated godaddy.sh for keyfile --- godaddy-ddns.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/godaddy-ddns.sh b/godaddy-ddns.sh index 62bc50e..3dc9e4e 100755 --- a/godaddy-ddns.sh +++ b/godaddy-ddns.sh @@ -26,8 +26,8 @@ SMTP="mail.comprofix.com" domain="comprofix.com" # your domain name="home" # name of A record to update -key=$(cat godaddy_keys | grep KEY | awk '{ print $2 }') # key for godaddy developer API -secret=$(cat godaddy_keys | grep SECRET | awk '{ print $2 }') # secret for godaddy developer API +key=$(cat /opt/scripts/godaddy_keys | grep KEY | awk '{ print $2 }') # key for godaddy developer API +secret=$(cat /opt/scripts//godaddy_keys | grep SECRET | awk '{ print $2 }') # secret for godaddy developer API headers="Authorization: sso-key $key:$secret" From a82c5cd068dbd6c12ea19a8bc3f2283cae8aa693 Mon Sep 17 00:00:00 2001 From: Matthew McKinnon Date: Fri, 4 May 2018 22:07:48 +1000 Subject: [PATCH 3/7] Added plesk-backup.sh script --- dbbackup.sh | 6 ++-- plesk-backup.sh | 87 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 3 deletions(-) create mode 100755 plesk-backup.sh diff --git a/dbbackup.sh b/dbbackup.sh index 90f609e..07eb1ca 100755 --- a/dbbackup.sh +++ b/dbbackup.sh @@ -24,7 +24,7 @@ SMTP="mail.comprofix.com" SUBJECT="$(hostname -f) Database Backup Completed $BAKDATE" BAKDATE=$(date +%Y%m%d) DBUSER='dbbackup' -DBPASS='EWFfP3GZsqr427Yj' +DBPASS='MdCg8uTSEWhmv7+D' BACKUPDIR='/BACKUP/db/' rotate_backups() { @@ -44,8 +44,8 @@ for db in $databases; do done -sendemail -o tls=no -s $SMTP -t $MAILTO -f "$THISSERVER <$MAILFROM>" -u "$SUBJECT" -m "$(cat /tmp/dbbackup.msg)" -q +#sendemail -o tls=no -s $SMTP -t $MAILTO -f "$THISSERVER <$MAILFROM>" -u "$SUBJECT" -m "$(cat /tmp/dbbackup.msg)" -q #Use Below to use systems postfix or local MTA -#cat /tmp/dbbackup.msg | mail -s "$SUBJECT" "$MAIL" +cat /tmp/dbbackup.msg | mail -s "$SUBJECT" "$MAIL" rm -fr /tmp/dbbackup.msg diff --git a/plesk-backup.sh b/plesk-backup.sh new file mode 100755 index 0000000..37de4ee --- /dev/null +++ b/plesk-backup.sh @@ -0,0 +1,87 @@ +#!/bin/bash +# Script Name:plesk-backup +# Author: Matt McKinnon +# Date: 04 May 2018 +# Description: +# This script will backup your plesk hosting files. +# Send an email report of plesk hosting files that have been backed up. +# Rotate backups for 7 days +# + +MAIL="support@comprofix.com" +MAILTO="support@comprofix.com" +MAILFROM="support@comprofix.com" +THISSERVER=$(hostname -f) +SMTP="mail.comprofix.com" +SUBJECT="$(hostname -f) Hosting Files Backup Completed $BAKDATE" +BAKDATE=$(date +%Y%m%d) +BACKUPDIR='/BACKUP' +VHOSTS='/var/www/vhosts/' +LOGFOLDER=/var/log/ +LOGFILE=$LOGFOLDER/backuplog-`date +%d-%m-%Y.log` + + +rotate_backups() { + find $BACKUPDIR -type f -mtime +7 -exec rm -frv {} \; + +} + +startlogging() { + echo $DASHES2 >> $LOGFILE + echo "$0 started running at $(date)" >> $LOGFILE + echo $DASHES >> $LOGFILE +} + +stoplogging() { + echo $DASHES >> $LOGFILE + echo "$0 finished running at $(date)" >> $LOGFILE >> $LOGFILE + echo $DASHES2 >> $LOGFILE +} + +DASHES="---------------------------------------------------------------------------------" +DASHES2="=================================================================================" + +startlogging +rotate_backups + + +#Backup www_root website files + +WWW_ROOT=$(MYSQL_PWD=$(cat /etc/psa/.psa.shadow) mysql -sN -uadmin -e 'select www_root from psa.hosting; ') + +for domain in $WWW_ROOT; do + ARCHIVE=$(echo $domain | awk -F/ '{print $6}';) + echo "$(date) [MESSAGE] Creating archive of $domain" >> $LOGFILE + zip -rq $BACKUPDIR/$ARCHIVE.$BAKDATE.zip $domain +done + +#Backup databases + +databases=$(MYSQL_PWD=$(cat /etc/psa/.psa.shadow) mysql -sN -uadmin -e "SHOW DATABASES;" | tr -d "| " | grep -v Database) + +#echo $databases; + + +for db in $databases; do + if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] && [[ "$db" != "apsc" ]] && [[ "$db" != "horde" ]] && [[ "$db" != phpmyadmin_* ]] && [[ "$db" != "psa" ]] && [[ "$db" != "roundcubemail" ]] ; then + echo "$(date) [MESSAGE] Dumping $db to sql file" >> $LOGFILE + mysqldump --force --opt --user=$DBUSER --password=$DBPASS --databases $db > $BACKUPDIR/$db.$BAKDATE.sql + fi +done + +#Backup files to offsite location + +echo "$(date) [MESSAGE] Copying backup files to offsite location" >> $LOGFILE +scp -rq -P 2222 $BACKUPDIR/* moe@home.comprofix.com:/data/backup/website + +echo "$(date) [MESSAGE] Sending email of backup report" >> $LOGFILE + +stoplogging + +#sendemail -o tls=no -s $SMTP -t $MAILTO -f "$THISSERVER <$MAILFROM>" -u "$SUBJECT" -m "$(cat /tmp/dbbackup.msg)" -q + +#Use below if using POSTFIX +cat $LOGFILE | mail -s "$SUBJECT" "$MAIL" + + + From 5a2d4265163bcd16c1dc60f2e2ad4525efad89ed Mon Sep 17 00:00:00 2001 From: Matthew McKinnon Date: Sat, 5 May 2018 00:06:41 +1000 Subject: [PATCH 4/7] Updated gitlabbackup.sh to include logging and offsite backup. --- gitlabbackup.sh | 70 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/gitlabbackup.sh b/gitlabbackup.sh index 1d69506..c314e21 100755 --- a/gitlabbackup.sh +++ b/gitlabbackup.sh @@ -1,8 +1,76 @@ #!/bin/bash +# Script Name: gitlabbackup +# Author: Matt McKinnon +# Date: 04 May 2018 +# Description: +# This script will backup your gitlab configuration files. +# Send an email report. +# Rotate backups for 7 days +# # Add the following lines to /etc/gitlab/gitlab.rb once added # run gitlab-ctl reconfigure for changes to take effect # # gitlab_rails['backup_path'] = '' # gitlab_rails['backup_keep_time'] = 604800 #7 days of backups to keep -gitlab-rake gitlab:backup:create +MAIL="support@comprofix.com" +MAILTO="support@comprofix.com" +MAILFROM="support@comprofix.com" +THISSERVER=$(hostname -f) +SMTP="mail.comprofix.com" +SUBJECT="$(hostname -f) Gitlab Backup Completed $BAKDATE" +BAKDATE=$(date +%Y%m%d) +BACKUPDIR='/BACKUP' +VHOSTS='/var/www/vhosts/' +LOGFOLDER=/var/log/ +LOGFILE=$LOGFOLDER/backuplog-`date +%d-%m-%Y.log` + + +rotate_backups() { + find $BACKUPDIR -type f -mtime +7 -exec rm -frv {} \; >> $LOGFILE + +} + +startlogging() { + echo $DASHES2 >> $LOGFILE + echo "$0 started running at $(date)" >> $LOGFILE + echo $DASHES >> $LOGFILE +} + +stoplogging() { + echo $DASHES >> $LOGFILE + echo "$0 finished running at $(date)" >> $LOGFILE + echo $DASHES2 >> $LOGFILE +} + +DASHES="---------------------------------------------------------------------------------" +DASHES2="=================================================================================" + +if [ ! -d "$BACKUPDIR" ]; then + # Control will enter here if $DIRECTORY doesn't exist. + mkdir $BACKUPDIR +fi + +startlogging + +# Rotate backup files +echo "$(date) [MESSAGE] Removing old backups" >> $LOGFILE +rotate_backups + +echo "$(date) [MESSAGE] Backing up gitlab for $(hostname -f)" >> $LOGFILE +gitlab-rake gitlab:backup:create >> $LOGFILE + +#Backup files to offsite location + +echo "$(date) [MESSAGE] Copying backup files to offsite location" >> $LOGFILE +scp -rq -P 2222 $BACKUPDIR/* moe@home.comprofix.com:/data/backup/website + +echo "$(date) [MESSAGE] Sending email of backup report" >> $LOGFILE + +stoplogging + +#sendemail -o tls=no -s $SMTP -t $MAILTO -f "$THISSERVER <$MAILFROM>" -u "$SUBJECT" -m "$(cat /tmp/dbbackup.msg)" -q + +#Use below if using POSTFIX +cat $LOGFILE | mail -s "$SUBJECT" "$MAIL" + From dd946b33c4e4cf6cefeb8d42ac31ab714c4f876f Mon Sep 17 00:00:00 2001 From: Matthew McKinnon Date: Sat, 5 May 2018 12:29:08 +1000 Subject: [PATCH 5/7] Updated plesk-backup.sh * Added mySQL query to get domain name * Added mySQL query to get www_root * Named Archive using domain name --- plesk-backup.sh | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/plesk-backup.sh b/plesk-backup.sh index 37de4ee..ac707eb 100755 --- a/plesk-backup.sh +++ b/plesk-backup.sh @@ -45,23 +45,24 @@ startlogging rotate_backups -#Backup www_root website files +#Backup website files -WWW_ROOT=$(MYSQL_PWD=$(cat /etc/psa/.psa.shadow) mysql -sN -uadmin -e 'select www_root from psa.hosting; ') +# Get domain ID +IDS=$(MYSQL_PWD=$(cat /etc/psa/.psa.shadow) mysql -sN -uadmin -e 'select id from psa.domains, psa.hosting where id = dom_id order by id;') -for domain in $WWW_ROOT; do - ARCHIVE=$(echo $domain | awk -F/ '{print $6}';) - echo "$(date) [MESSAGE] Creating archive of $domain" >> $LOGFILE - zip -rq $BACKUPDIR/$ARCHIVE.$BAKDATE.zip $domain +#Use ID to get domain name and www_root folders and create archive using domain name. +for ID in $IDS; do + DOMAIN_NAME=$(MYSQL_PWD=$(cat /etc/psa/.psa.shadow) mysql -sN -uadmin -e 'select name from psa.domains,psa.hosting where id = '$ID' AND dom_id ='$ID' order by id;') + WWW_ROOT=$(MYSQL_PWD=$(cat /etc/psa/.psa.shadow) mysql -sN -uadmin -e 'select www_root from psa.domains,psa.hosting where id = '$ID' AND dom_id ='$ID' order by id;') + + echo "$(date) [MESSAGE] Creating archive of $DOMAIN_NAME" >> $LOGFILE + zip -rq $BACKUPDIR/$DOMAIN_NAME.$BAKDATE.zip $WWW_ROOT done #Backup databases databases=$(MYSQL_PWD=$(cat /etc/psa/.psa.shadow) mysql -sN -uadmin -e "SHOW DATABASES;" | tr -d "| " | grep -v Database) -#echo $databases; - - for db in $databases; do if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] && [[ "$db" != "apsc" ]] && [[ "$db" != "horde" ]] && [[ "$db" != phpmyadmin_* ]] && [[ "$db" != "psa" ]] && [[ "$db" != "roundcubemail" ]] ; then echo "$(date) [MESSAGE] Dumping $db to sql file" >> $LOGFILE From 55a67177e862492307a1f8f3960a616299c785c8 Mon Sep 17 00:00:00 2001 From: Matthew McKinnon Date: Wed, 16 May 2018 11:39:27 +1000 Subject: [PATCH 6/7] Changed from using scp to rsync Updated zip command to exclude data folder when archiving cloud.comprofix.com --- plesk-backup.sh | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/plesk-backup.sh b/plesk-backup.sh index ac707eb..a42a269 100755 --- a/plesk-backup.sh +++ b/plesk-backup.sh @@ -22,7 +22,7 @@ LOGFILE=$LOGFOLDER/backuplog-`date +%d-%m-%Y.log` rotate_backups() { - find $BACKUPDIR -type f -mtime +7 -exec rm -frv {} \; + find $BACKUPDIR -type f -mtime +1 -exec rm -fr {} \; } @@ -56,7 +56,12 @@ for ID in $IDS; do WWW_ROOT=$(MYSQL_PWD=$(cat /etc/psa/.psa.shadow) mysql -sN -uadmin -e 'select www_root from psa.domains,psa.hosting where id = '$ID' AND dom_id ='$ID' order by id;') echo "$(date) [MESSAGE] Creating archive of $DOMAIN_NAME" >> $LOGFILE - zip -rq $BACKUPDIR/$DOMAIN_NAME.$BAKDATE.zip $WWW_ROOT + if [ $DOMAIN_NAME = 'cloud.comprofix.com' ]; then + zip -rq $BACKUPDIR/$DOMAIN_NAME.$BAKDATE.zip $WWW_ROOT -x '*data*' + else + zip -rq $BACKUPDIR/$DOMAIN_NAME.$BAKDATE.zip $WWW_ROOT + fi + done #Backup databases @@ -73,8 +78,8 @@ done #Backup files to offsite location echo "$(date) [MESSAGE] Copying backup files to offsite location" >> $LOGFILE -scp -rq -P 2222 $BACKUPDIR/* moe@home.comprofix.com:/data/backup/website - +#scp -rq -P 2222 $BACKUPDIR/* moe@home.comprofix.com:/data/backup/website +rsync -avz -e "ssh -p 2222 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" /BACKUP/ moe@home.comprofix.com:/data/backup/websites/ >> $LOGFILE echo "$(date) [MESSAGE] Sending email of backup report" >> $LOGFILE stoplogging From 73fe0156a006aa7dbf1e708595a80f2f6e5f6f7b Mon Sep 17 00:00:00 2001 From: Matthew McKinnon Date: Mon, 28 May 2018 12:51:36 +1000 Subject: [PATCH 7/7] Added ssl-install.sh script for copy Certificates to ESXi Server --- ssl-install.sh | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100755 ssl-install.sh diff --git a/ssl-install.sh b/ssl-install.sh new file mode 100755 index 0000000..5bd814f --- /dev/null +++ b/ssl-install.sh @@ -0,0 +1,34 @@ +#!/bin/bash +# Script Name: ssl-install +# Author: Matt McKinnon +# Date: 28th May 2018 +# Description: +# +# Script used to copy Let's Encrypt Generated Certificates from generating server to ESXi VPS Server. +# This script requires acme.sh be used to setup your Let's Encrypt Certificates. +# - https://github.com/Neilpang/acme.sh +# +# SSH Key Login also needs to be enabled on ESXi +# +# +# +# + +DOM_NAME="home.comprofix.com" +ESXI_SERVER="esxi.home.comprofix.com" + + +if [ ! -d ~/.acme.sh ]; then + echo "Folder does not exist" + exit 0 +else + # echo "You are using acme.sh. Well done" +fi + + +scp -q ~/.acme.sh/$DOM_NAME/$DOM_NAME.cer root@$ESXI_SERVER:/etc/vmware/ssl/rui.crt +scp -q ~/.acme.sh/$DOM_NAME/$DOM_NAME.key root@$ESXI_SERVER:/etc/vmware/ssl/rui.key +ssh -qt root@$ESXI_SERVER "/sbin/services.sh restart" + + +