grav/pages/08.mikrotik-backup/docs.md

2.6 KiB

title
mikrotik Backup

1. Script for DDNS

Create the following script with name TanBack and the following permissions ftp,read,write,policy,test,sensitive

# Tanshu automatic DNS updates
: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];
:local filename ("Data/mikrotik-Home/mikrotik_$year-$month-$day-$time.rsc");
/export file=export.rsc;
/tool fetch address=hopsngrains.com src-path=export.rsc user=tanshu mode=ftp password=tan12shu dst-path=($filen>
:log info "TanBack: Backup uploaded with filename $filename"

2. Schedule the script

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

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/

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

#!/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 --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