{"id":4289,"date":"2021-12-03T13:48:36","date_gmt":"2021-12-03T19:48:36","guid":{"rendered":"https:\/\/www.incredigeek.com\/home\/?p=4289"},"modified":"2021-12-03T13:59:10","modified_gmt":"2021-12-03T19:59:10","slug":"extract-unifi-unf-backup-file","status":"publish","type":"post","link":"https:\/\/www.incredigeek.com\/home\/extract-unifi-unf-backup-file\/","title":{"rendered":"Extract UniFi .unf backup file"},"content":{"rendered":"\n<p>In this post we are going to extract the contents of a UniFi .unf backup.<\/p>\n\n\n\n<p>This is helpful if we need to do any sort of recovery, or need to look through the database to find system information.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Acquire backup<\/li><li>Decrypt and extract backup<\/li><li>Dump database to JSON file<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Acquire Backup<\/h2>\n\n\n\n<p>This is easy to do.  Log into the web interface go to Settings -> System -> Maintenance -> Backup and Restore<\/p>\n\n\n\n<p>Scroll down to Available Backups and download.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2021\/12\/image-3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"684\" height=\"145\" src=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2021\/12\/image-3.png\" alt=\"\" class=\"wp-image-4291\" srcset=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2021\/12\/image-3.png 684w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2021\/12\/image-3-300x64.png 300w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2021\/12\/image-3-500x106.png 500w\" sizes=\"auto, (max-width: 684px) 100vw, 684px\" \/><\/a><figcaption>Download Backup in UniFi Controller<\/figcaption><\/figure>\n\n\n\n<p>You can also get the file via scp or sftp.  Manual backups are located in<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/usr\/lib\/unifi\/data\/backup<\/pre>\n\n\n\n<p>and auto backups are in <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/usr\/lib\/unifi\/data\/backup\/autobackup<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Decrypt and Extract Backup<\/h2>\n\n\n\n<p>We&#8217;ll be getting the following decrypt script from here.  <a href=\"https:\/\/github.com\/zhangyoufu\/unifi-backup-decrypt\">https:\/\/github.com\/zhangyoufu\/unifi-backup-decrypt<\/a>  More notes on it below.<\/p>\n\n\n\n<p>We&#8217;ll need to make sure that openssl and zip are installed<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt install openssl zip<\/pre>\n\n\n\n<p>Download the script with wget<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">wget https:\/\/raw.githubusercontent.com\/zhangyoufu\/unifi-backup-decrypt\/master\/decrypt.sh<\/pre>\n\n\n\n<p>Make it executable <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo chmod u+x decrypt.sh<\/pre>\n\n\n\n<p>And now we can convert the UniFi .unf backup file to a .zip<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo .\/decrypt.sh autobackup_6.2.33.unf autobackup_6.2.33.zip<\/pre>\n\n\n\n<p>Now we can extract the zip archive.  You can do this on Windows, macOS, or Linux through the GUI or you can extract with <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo unzip autobackup_6.2.33.zip -d unifi<\/pre>\n\n\n\n<p>This will extract all the files and folders to a directory named unifi.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cd unifi<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Dump database to JSON<\/h2>\n\n\n\n<p>You should now see the db.gz file. This is a compressed archive of the database in BSON (Binary JSON) format.  We can use the mongo-tools to convert this to a more human readable JSON format.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt install mongo-tools<\/pre>\n\n\n\n<p>Now we can extract the archive and pipe it through bsondump.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">gunzip -c db.gz | bsondump<\/pre>\n\n\n\n<p>You can run it through grep to filter out what you need.<\/p>\n\n\n\n<p>You can also dump the db to a json file with<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">bsondump --bsonFile=db --outFile=db.json<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>More notes on the decrypt script.<\/p>\n\n\n\n<p>The decrypt script is really simple.  It looks like it uses a key to decrypt the UniFi backup and then puts all the contents into a zip file.  There is also an encryption script.  Theoretically you can decrypt, make changes to the config and then reencrypt and restore to a server.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/bin\/sh\n\n# Authors:\n# 2017-2019 Youfu Zhang\n# 2019 Balint Reczey &lt;balint.reczey@canonical.com>\n\nset -e\n\nusage() {\n    echo \"Usage: $0 &lt;input .unf file> &lt;output .zip file>\"\n}\n\nif [ -z \"$2\" -o ! -f \"$1\" ]; then\n    usage\n    exit 1\nfi\n\nINPUT_UNF=$1\nOUTPUT_ZIP=$2\n\nTMP_FILE=$(mktemp)\ntrap \"rm -f ${TMP_FILE}\" EXIT\n\nopenssl enc -d -in \"${INPUT_UNF}\" -out \"${TMP_FILE}\" -aes-128-cbc -K 626379616e676b6d6c756f686d617273 -iv 75626e74656e74657270726973656170 -nopad\nyes | zip -FF \"${TMP_FILE}\" --out \"${OUTPUT_ZIP}\" > \/dev\/null 2>&amp;1<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this post we are going to extract the contents of a UniFi .unf backup. This is helpful if we need to do any sort of recovery, or need to look through the database to find system information. Acquire backup &hellip; <a href=\"https:\/\/www.incredigeek.com\/home\/extract-unifi-unf-backup-file\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[129,979],"tags":[1268,75,1269,795,131,974,1267,188],"class_list":["post-4289","post","type-post","status-publish","format-standard","hentry","category-ubiquiti","category-unifi","tag-autobackup","tag-backup","tag-bson","tag-decrypt","tag-ubiquiti-2","tag-ui","tag-unf","tag-unifi"],"_links":{"self":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/4289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/comments?post=4289"}],"version-history":[{"count":4,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/4289\/revisions"}],"predecessor-version":[{"id":4296,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/4289\/revisions\/4296"}],"wp:attachment":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/media?parent=4289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/categories?post=4289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/tags?post=4289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}