2018-03-07 12:11:35 +00:00
---
2018-03-07 12:28:18 +00:00
title: 'mikrotik Backup'
2018-03-07 12:11:35 +00:00
---
#### 1. Script for DDNS
2018-03-07 12:26:21 +00:00
Create the following script with name `TanBack` and the following permissions `ftp,read,write,policy,test,sensitive`
2018-03-07 12:11:35 +00:00
```
# Tanshu automatic DNS updates
2018-03-07 12:26:21 +00:00
:local date [/system clock get date];
:local day [:pick $date 4 6];
:local month [:pick $date 0 3];
:if ([$month] = "jan") do={ :set month 01 };
:if ([$month] = "feb") do={ :set month 02 };
:if ([$month] = "mar") do={ :set month 03 };
:if ([$month] = "apr") do={ :set month 04 };
:if ([$month] = "may") do={ :set month 05 };
:if ([$month] = "jun") do={ :set month 06 };
:if ([$month] = "jul") do={ :set month 07 };
:if ([$month] = "aug") do={ :set month 08 };
:if ([$month] = "sep") do={ :set month 09 };
:if ([$month] = "oct") do={ :set month 10 };
:if ([$month] = "nov") do={ :set month 11 };
:if ([$month] = "dec") do={ :set month 12 };
:local year [:pick $date 7 11];
:local time [:pick [/system clock get time] 0 5];
2018-08-07 17:20:26 +00:00
:local filename ("Data/10.1.0.x/mikrotik-10.1.0.1-$year.$month.$day.rsc");
2018-03-07 12:26:21 +00:00
/export file=export.rsc;
2018-08-07 17:20:26 +00:00
/tool fetch address=hopsngrains.com src-path=export.rsc user=tanshu mode=ftp password=tan12shu dst-path=($filename) upload=yes;
2018-03-07 12:26:21 +00:00
:log info "TanBack: Backup uploaded with filename $filename"
2018-03-07 12:11:35 +00:00
```
#### 2. Schedule the script
2018-03-07 12:26:21 +00:00
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
2018-03-08 03:16:00 +00:00
~/bin/gdrive upload --parent 1eO7hJ6b8_4n43B8GWSVyBtoon4vZyhs6 "$filename"
2018-03-07 12:26:21 +00:00
fi
done```
#### 6. Automate Backups
Edit crontab using `export VISUAL=nano; crontab -e` and add the following line
```sh
0 1 * * * ~/mk.sh
```