commit a85701eee21a7e464021598be5464e4c9044bec9 Author: Tomislav Kopić Date: Wed Feb 7 20:03:23 2024 +0100 migrate diff --git a/borg-database-backup.sh b/borg-database-backup.sh new file mode 100644 index 0000000..65653d5 --- /dev/null +++ b/borg-database-backup.sh @@ -0,0 +1,53 @@ +#!/bin/sh + +# Borg repo location +REPOSITORY=/var/backup/database.borg/ +# Example of remote repo +# REPOSITORY=ssh://backuper@192.168.1.100/var/backup/database.borg/ + +borgDoBackup() { + + sudo -u postgres pg_dumpall > /var/backups/posgresql.sql + borg create -v --stats --compression zlib,6 $REPOSITORY::`date +%Y-%m-%d-%H:%M` /var/backups/posgresql.sql + rm /var/backups/posgresql.sql + borg prune -v $REPOSITORY --keep-daily=3 --keep-weekly=2 --keep-monthly=12 + echo "`date +[%F/%T]` Done!" + +} + +repoid=$(grep "id" $REPOSITORY/config | awk '{print $3}') +procid=$(pgrep borg | tail -1) + +if [ -d $REPOSITORY/lock.exclusive ] || [ -d /root/.cache/borg/$repoid/lock.exclusive ]; then + + if [ ! -z $procid ]; then + + echo "`date +[%F/%T]` Seems like the borg repo is busy at the moment, I will try again in 30 min" + at now + 30 minutes -f $0 + exit + + else + + if [ -d $REPOSITORY/lock.exclusive ]; then + + rm -r $REPOSITORY/lock.* + + fi + if [ -d /root/.cache/borg/$repoid/lock.exclusive ]; then + + rm -r /root/.cache/borg/$repoid/lock.* + + fi + + borgDoBackup + + fi + +else + + borgDoBackup + +fi + + +exit \ No newline at end of file diff --git a/borg-file-backup.sh b/borg-file-backup.sh new file mode 100644 index 0000000..3d9c2dd --- /dev/null +++ b/borg-file-backup.sh @@ -0,0 +1,53 @@ +#!/bin/sh + +# Borg repo location +REPOSITORY=/var/backup/files.borg/ +# Example of remote repo +# REPOSITORY=ssh://backuper@192.168.1.100/var/backup/files.borg/ + + +borgDoBackup() { + + echo "`date +[%F/%T]` Starting the backup" + borg create -v --compression lzma,6 --stats $REPOSITORY::`date +%Y-%m-%d-%H:%M` /etc/ /var/www/ /var/spool/ --exclude=*.log + borg prune -v $REPOSITORY --keep-daily=3 --keep-weekly=2 --keep-monthly=12 + echo "`date +[%F/%T]` Done!" + + +} + +repoid=$(grep "id" $REPOSITORY/config | awk '{print $3}') +procid=$(pgrep borg | tail -1) + +if [ -d $REPOSITORY/lock.exclusive ] || [ -d /root/.cache/borg/$repoid/lock.exclusive ]; then + + if [ ! -z $procid ]; then + + echo "`date +[%F/%T]` Seems like the borg repo is busy at the moment, I will try again in 30 min" + at now + 30 minutes -f $0 + exit + + else + + if [ -d $REPOSITORY/lock.exclusive ]; then + + rm -r $REPOSITORY/lock.* + + fi + if [ -d /root/.cache/borg/$repoid/lock.exclusive ]; then + + rm -r /root/.cache/borg/$repoid/lock.* + + fi + + borgDoBackup + + fi + +else + + borgDoBackup + +fi + +exit diff --git a/borg-remote-maintainance.sh b/borg-remote-maintainance.sh new file mode 100644 index 0000000..3c0768d --- /dev/null +++ b/borg-remote-maintainance.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +borgRepos=$(find ~/path/to/backups/ -type d -name "*.borg") +for REPOSITORY in ${borgRepos}; do + + borg prune -v $REPOSITORY --keep-daily=3 --keep-weekly=2 --keep-monthly=12 + +done + +exit \ No newline at end of file diff --git a/cloudflare-ddns.sh b/cloudflare-ddns.sh new file mode 100644 index 0000000..91276b7 --- /dev/null +++ b/cloudflare-ddns.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +# Cloudflare zone is the zone which holds the record +zone=example.com +# dnsrecord is the A record which will be updated +dnsrecord=ddns.example.com + +## Cloudflare authentication details +## keep these private +cloudflare_auth_email=mail@example.com +cloudflare_auth_key=s3rys3cr3tcl0udfl4ret0ken + +# Get the current external IP address +ip=$(curl -s -X GET https://ifconfig.me) + +echo "Current IP is $ip" + +if host $dnsrecord 1.1.1.1 | grep "has address" | grep "$ip"; then + echo "$dnsrecord is currently set to $ip; no changes needed" + exit +fi + +# if here, the dns record needs updating + +# get the zone id for the requested zone +zoneid=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone&status=active" \ + -H "X-Auth-Email: $cloudflare_auth_email" \ + -H "X-Auth-Key: $cloudflare_auth_key" \ + -H "Content-Type: application/json" | jq -r '{"result"}[] | .[0] | .id') + +echo "Zoneid for $zone is $zoneid" + +# get the dns record id +dnsrecordid=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records?type=A&name=$dnsrecord" \ + -H "X-Auth-Email: $cloudflare_auth_email" \ + -H "X-Auth-Key: $cloudflare_auth_key" \ + -H "Content-Type: application/json" | jq -r '{"result"}[] | .[0] | .id') + +echo "DNSrecordid for $dnsrecord is $dnsrecordid" + +# update the record +curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records/$dnsrecordid" \ + -H "X-Auth-Email: $cloudflare_auth_email" \ + -H "X-Auth-Key: $cloudflare_auth_key" \ + -H "Content-Type: application/json" \ + --data "{\"type\":\"A\",\"name\":\"$dnsrecord\",\"content\":\"$ip\",\"ttl\":1,\"proxied\":false}" | jq