summaryrefslogtreecommitdiff
path: root/mikrotik-backup
blob: cf4059d0524695dd4e68c53bce9430ebbedd84c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash

# Copyright 2017 Peter Palfrader
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


# /system script add name="backups-create" source={/ export compact file=config.export;
# system backup save name=config.backup dont-encrypt=yes; }
# 
# /system scheduler add name="backup" on-event="/system script run backups-create" start-date=jan/01/1970 start-time=00:00:00 interval=10h
# /system script run backups-create
# 
# /user group add name="backupgroup" policy="ssh,read,ftp,sensitive"
# /user add name=backup group=backupgroup address=XXX.YYY/ZZZ password=XXXXXXXX
# 
# /user ssh-keys import user=backup public-key-file=id_rsa.pub

set -e
set -u
set -x

umask 077

user=backup
hosts="router.noreply.org router2.debian.org"
ts=$(date +"%Y%m%dT%H%M%S")

rc=0
for h in $hosts; do
  r=0
  dir="$h/$ts"
  mkdir -p "$dir"
  scp -B -p "${user}@${h}:config.backup"     "$dir/" || r=1
  scp -B -p "${user}@${h}:config.export.rsc" "$dir/" || r=1
  if [ "$r" -gt 0 ]; then
    rmdir "$h/$ts" || true
    rc="$r"
  else
    ln -sf "$ts"/config.backup     "$h/config.backup"
    ln -sf "$ts"/config.export.rsc "$h/config.export.rsc"
  fi
done

exit "$r"