Automating FRR backups with Unimus — a how-to guide

STEP 1 — preparing Unimus and setting you up with an API token

STEP 2 — getting familiar with Unimus API

curl -H "Accept: application/json" -H "Content-type: application/json" -H "Authorization: Bearer <token>" \ 
-d '{"backup":"<backup>","type":"<TEXT>"}' "http://example.unimus/api/v2/devices/<deviceId>/backups"
<token> - this is our API token we generated in step 1 
<backup> - this will be our encoded backup we will prepare in step 3
<TEXT> - this will be a type of backup we will choose (BINARY/TEXT) also in step 3
<example.unimus> - this is your Unimus server address
<deviceId> - this will be an ID of our device we created in step 1
curl -H "Accept: application/json" -H "Authorization: Bearer <token>" \ 
"http://<example.unimus>/api/v2/devices/findByAddress/<address>?attr=s,c"
<token> - this is our API token we generated in step 1 
<example.unimus> - this is Unimus' server address
<address> - this is IP/hostname of our device
curl -H "Accept: application/json" -H "Authorization: Bearer \ eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.ACruAhyEiipDrX7-QRsPfAJpsTooibm5RznqSHSMtuM" \ "http://10.10.10.10:8085/api/v2/devices/findByAddress/10.20.30.40?attr=s,c"
"data":[{"id":234,"createTime":1629477261,"address":"10.20.30.40","description":"FRRouter@Deb9@123", "schedule":null,"vendor":null,"type":null,"model":null,"lastJobStatus":"FAILED","connections":[]}], "paginator":{"totalCount":1,"totalPages":1,"page":0,"size":20}}
curl -H "Accept: application/json" -H "Authorization: Bearer \ eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.ACruAhyEiipDrX7-QRsPfAJpsTooibm5RznqSHSMtuM" \ "http://10.10.10.10:8085/api/v2/devices/findByDescription/FRRouter@Deb9@123attr=s,c"
"data":[{"id":234,"createTime":1629477261,"address":"10.20.30.40","description":"FRRouter@Deb9@123", "schedule":null,"vendor":null,"type":null,"model":null,"lastJobStatus":"FAILED","connections":[]}], "paginator":{"totalCount":1,"totalPages":1,"page":0,"size":20}}

STEP 3 — preparing a backup and uploading it into Unimus

/etc/frr/daemons 
/etc/frr/frr.conf

Method 1 — TEXT backup

#!/bin/bash cd /tmp #BACKUP PREP 
echo -e "#BEGIN /etc/frr/daemons" > frrbackup.txt
cat /etc/frr/daemons >> frrbackup.txt
echo -e "#END /etc/frr/daemons\n\n\n" >> frrbackup.txt
echo -e "#BEGIN /etc/frr/frr.conf" >> frrbackup.txt
cat /etc/frr/frr.conf >> frrbackup.txt
echo -e "#END /etc/frr/frr.conf" >> frrbackup.txt
#BASE64 ENCODING encodedbackup=$(base64 -w 0 frrbackup.txt) #BACKUP PUSH INTO UNIMUS
curl -H "Accept: application/json" -H "Content-type: application/json" -H "Authorization: Bearer \ eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.ACruAhyEiipDrX7-QRsPfAJpsTooibm5RznqSHSMtuM" \ -d '{"backup":"'"$encodedbackup"'","type":"TEXT"}' "http://10.10.10.10:8085/api/v2/devices/234/backups"
#CLEANUP
rm frrbackup.txt
  • First it prepares a backup file with some additional formatting so that it is easier to distinguish beginning/end of each in the final text file.
  • Then a content of the prepared backup text file is passed to BASE64 encoder and loaded into a variable — this encoding is very important as it encodes the contents into a single streamlined string of characters allowing us to move it efficiently. Note, BASE64 encoding doesn’t encrypt the content of your files, anyone could decode it with any BASE64 decoder.
  • Then using curl call from step 2 we fill in all parameters required with actual data, and changed backup type to TEXT — note the use of extra single/double quotes to insert the variable containing our encoded backup, This format is important so that the variable is processed correctly.
  • Lastly we clean up.
{"data":{"success":"true"}}

Method 2 — BINARY backup

#!/bin/bash cd /tmp #BACKUP PREP 
tar -czvf frrbackup.tar.gz -C /etc/frr/ daemons frr.conf
#BASE64 ENCODING
encodedbackup=$(base64 -w 0 frrbackup.tar.gz)
#BACKUP PUSH INTO UNIMUS curl -H "Accept: application/json" -H "Content-type: application/json" -H "Authorization: Bearer \ eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.ACruAhyEiipDrX7-QRsPfAJpsTooibm5RznqSHSMtuM" \ -d '{"backup":"'"$encodedbackup"'","type":"BINARY"}' "http://10.10.10.10:8085/api/v2/devices/234/backups" #CLEANUP
rm frrbackup.tar.gz
  • First it prepares and packs our target files into a single .tar.gz archive, but you can use other formats if you prefer different archiver/compressor.
  • Then it is passed to a BASE64 encoder and loaded into a variable — this encoding is very important as it allows the binary file to be transferred as a single streamlined string of characters. Note, BASE64 encoding doesn’t encrypt the content of your files, anyone could decode it with any BASE64 decoder.
  • Then using the curl call from step 2 we fill in all parameters required with actual data, and change backup type to BINARY — note the use of extra single/double quotes to insert the variable containing our encoded backup, This format is important so that the variable is processed correctly.
  • Lastly we clean up.
{"data":{"success":"true"}}

STEP 4 — job scheduling

0 3 * * * root /root/frrbackup.sh

Final words

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store