(Grav GitSync) Automatic Commit from tanshu

This commit is contained in:
tanshu 2018-03-07 12:26:21 +00:00 committed by GitSync
parent b0f48b0e7e
commit 52fa515139

View File

@ -1,54 +1,75 @@
--- ---
title: 'mikrotik DDNS 2' title: 'mikrotik Configuration Autobackup'
--- ---
#### 1. Script for DDNS #### 1. Script for DDNS
Create the following script with name `TanDNS` and the following permissions `write, test, read, policy` Create the following script with name `TanBack` and the following permissions `ftp,read,write,policy,test,sensitive`
``` ```
# Tanshu automatic DNS updates # Tanshu automatic DNS updates
:local date [/system clock get date];
# User account info :local day [:pick $date 4 6];
:local user "user" :local month [:pick $date 0 3];
:local pass "123456" :if ([$month] = "jan") do={ :set month 01 };
:if ([$month] = "feb") do={ :set month 02 };
# Set the hostname or label of network to be updated. :if ([$month] = "mar") do={ :set month 03 };
:local host "home.tanshu.com" :if ([$month] = "apr") do={ :set month 04 };
:if ([$month] = "may") do={ :set month 05 };
# Change to the name of interface that gets the changing IP address :if ([$month] = "jun") do={ :set month 06 };
:local inetinterface "pppoe-out1" :if ([$month] = "jul") do={ :set month 07 };
:if ([$month] = "aug") do={ :set month 08 };
#- No more changes need ------------------------------------------------------------- :if ([$month] = "sep") do={ :set month 09 };
:global previousIP; :if ([$month] = "oct") do={ :set month 10 };
:if ([$month] = "nov") do={ :set month 11 };
:if ([/interface get $inetinterface value-name=running]) do={ :if ([$month] = "dec") do={ :set month 12 };
# Get the current IP on the interface :local year [:pick $date 7 11];
:local currentIP [/ip address get [find interface="$inetinterface" disabled=no] address]; :local time [:pick [/system clock get time] 0 5];
:local filename ("Data/mikrotik-Home/mikrotik_$year-$month-$day-$time.rsc");
# Strip the net mask off the IP address /export file=export.rsc;
:for i from=( [:len $currentIP] - 1) to=0 do={ /tool fetch address=hopsngrains.com src-path=export.rsc user=tanshu mode=ftp password=tan12shu dst-path=($filen>
:if ( [:pick $currentIP $i] = "/") do={ :log info "TanBack: Backup uploaded with filename $filename"
:set currentIP [:pick $currentIP 0 $i]
}
}
:if ($currentIP != $previousIP) do={
:log info "TanDNS: Update needed"
:set previousIP $currentIP
# The update URL. Note the "\3F" is hex for question mark (?). Required since ? is a special character in commands.
:local url "http://bifrost.tanshu.com/update\3Fdomain=$host"
:log info "TanDNS: Sending update for $host"
/tool fetch url=$url user=$user password=$pass mode=http keep-result=no
:log info "TanDNS: Host $host updated on TanDNS with IP $currentIP"
} else={
:log info "TanDNS: Previous IP $previousIP and current IP equal, no update need"
}
} else={
:log info "TanDNS: $inetinterface is not currently running, so therefore will not update."
}
``` ```
#### 2. Schedule the script #### 2. Schedule the script
Create the following schedule with name `TanDNS-Update` and the following permissions `write, test, read, policy`, `5 minute` interval and `TanDNS` On Event. Create the following schedule with name `TanBack-Daily` and the following permissions `ftp,read,write,policy,test,sensitive`, `1 day` interval and `TanBack` On Event.
#### 3. Server preperation
```sh
mkdir -p ~/Data/mikrotik-Home
```
#### 4. Install gdrive
Install gdrive to backup the database to google account in case of data loss at webfaction
Go to `https://github.com/prasmussen/gdrive` and download `gdrive-linux-x64` in `~/bin/`
```sh
wget -o ~/bin/gdrive $URL_FOR_GDRIVE_LINUX_X64_BINARY
```
Make it executable `chmod +x ~/bin/gdrive`
#### 5. Configure Google Drive
Link your gdrive to the account by running `gdrive list` and following the instructions.
Create a folder called `mikrotik-Home` and get its id by running `gdrive list`.
Create upload script named `mk.sh` in the home directory and make it executable `chmod +x ~/mk.sh`
```sh
#!/usr/bin/env bash
for filename in ~/Data/mikrotik-Home/*.rsc; do
hash=`sha512sum ${filename} | awk '{ print $1 }'`
if grep -q "$hash" ~/Data/mikrotik-Home/mikrotik.sha512sums; then
echo "File $(basename $filename) is uploaded"
else
echo "Need to upload $filename"
sha512sum "$filename" >> ~/Data/mikrotik-Home/mikrotik.sha512sums
~/bin/gdrive upload --delete --parent 1eO7hJ6b8_4n43B8GWSVyBtoon4vZyhs6 "$filename"
fi
done```
#### 6. Automate Backups
Edit crontab using `export VISUAL=nano; crontab -e` and add the following line
```sh
0 1 * * * ~/mk.sh
```