LIQUIDATION SALE! FREE Shipping on orders over $49 USA $99 INTERNATIONAL USE CODE: "LIQUIDATION" for 50% OF

OpenHab 2: OpenHab Backup Using Mac

OpenHab Backup

Using Mac:

 

Hey guys Matt here from MKSmartHouse.com and in this video I am going to show you the complete way to backup your openhab server using a mac.
Imagine that one day you open up your openHAB app on your phone and there is nothing there, you then quickly go to your computer and try to ssh into the server but it just keeps saying “Connection refused”. At this point all is lost, all your hard work, all the time you put in, simply gone. But, lucky for you that was just an imagination and it can all be simply avoided by following this guide.
In this complete guide I will cover exactly how to backup your openhab server. The way this backup will work is there will be an external flash drive plugged into the raspberry pi and every day the raspberry pi will backup the openhab configuration and user data files to the flash drive. Now, this does solve a majority of the backup problem but what about the rest of the pi or other non openhab related items. Well, since we don't change much non openhab items on the raspberry pi after it is initially set up, we will do a manually backup of the SD card onto the flash drive through the computer. More on that later, let's just get started!
The items needed to complete this guide are the following: Number one, is a fully setup any model of raspberry pi running openhab, I am using a raspberry pi 3 in a clear enclosure that has a fan which generates a lot of air flow to the raspberry pi, I also installed heatsinks on the pi for even more cooling. I did this much cooling because I never want to run the risk of the pi overheating and failing. If you want a kit to protect your pi you can find it at mksmarthouse.com/shop. If your pi is not setup check out my home automation server setup guide. Number two is a computer running MAC OS or OS X, if you have a windows computer then check out my guide where I do this same guide except using a windows computer. Number three, is an SD card reader of some kind that can read the type of SD card you have in your pi, I only need a micro SD card reader but this one reads both sizes of sd cards. Lastly, number four you need a flash drive that is the same size as the sd card in your pi my sd card is 32gb so I bought a 32gb flash drive. The links to all the parts and guides mentioned are in the video description. 
Speaking of links in the description, follow @mksmarthouse on twitter, instagram and snapchat because those are the place where I give sneak peaks on when videos are releasing and also where I ask for your input on topics and future videos.
1. The first thing we have to do is plug the flash drive into the computer. 
2. Next go to spotlight, type in disk utility and press enter. 
3. In the left column click on your flash drive. The next part we are about to do will erase everything on the flash drive so make sure you do not have anything important on it. 
4. Then press the erase button. In the name box type in OHBACKUP and in the format select ExFat. Click erase.
5. Next we need to head over to the written version of this guide on my website, link is in the description. 
6. Here we need to download the OHBackup_script. This is the script that we setup to automatically backup openhab every day at 12am and it will also delete backups that over 30 days old in order to not fill up the flash drive completely. 7. So go over to your downloads folder, double click on the zip file and then drag the script to the flash drive we will use for backing up. Once that is copied over eject the flash drive from your computer.
8. Grab the flash drive from your computer and plug it into the raspberry pi.
9. Before we get any further I just wanted to show you the basic ui of this server to demonstrate that this pi is fully configured and has a sitemap with items in it in other words it has a data on it. I have already done this process to my main openhab server which is why I will be using this pi.
10. Now open up terminal on your mac and ssh into the pi. I recommend having the written version of this guide open so that way you can just copy and paste the commands I am about to show you. 
11. The first thing we have to do is install support for exfat so type in sudo apt-get install exfat-fuse and press enter. You may need to type in your password and press enter. 
11.1.  It will then ask if you want to confirm type y and press enter.
11.2. Next enter super user so type in sudo su and press enter. 
12. Next we have to make a directory to mount the flash drive to, so type in mkdir /mnt/OHBackupDrive and press enter. 
13. Now we have to find out what the UUID or id number of our flash drive is in order to mount it to the directory so type in lsblk -o UUID,NAME,FSTYPE,SIZE,MOUNTPOINT,LABEL,MODEL and press enter. 
14. As you can see this is the 32gb flash drive I inserted into the pi and here is its UUID, this is important so write it down somewhere. 
15. In order to mount the flash drive to the directory we need to modify the fstab so type in nano /etc/fstab and press enter. 
16. Now at the bottom of the file type in 
UUID=C73C-F2E6 /mnt/OHBackupDrive auto defaults,auto,umask=000,users,rw 0 0 and press enter. 
17. Replace the UUID with the UUID of your flash drive. 
18. Once you changed the UUID press control x then y and enter to save. 
19. Now to execute the mounting procedure instead of rebooting type in mount -a and press enter. 
20. To confirm that it mounted to the directory type in df -k /mnt/OHBackupDrive and press enter, you should see how much of the folder is used and some other information. 
21. Ok, now we are done with super user so type in exit and press enter. 
22. Next up is to copy the script from the flash drive to the bin folder do this by typing in sudo cp /mnt/OHBackupDrive/OHBackup_script /bin and press enter. 
23. Next we need to give the script permission to execute, so type in sudo chmod u+x /bin/OHBackup_script and press enter. 
24. To execute the script type in sudo /bin/OHBackup_script and press enter. 
25. You should see it say Openhab backup script and Success! Backup made. 
26. Now, let's confirm that it did save so type in cd /mnt/OHBackupDrive and press enter then type in ls and press enter. You should see a zip file. Great! OpenHAB was successfully backed up. 
27. Now let’s make this backup process happen automatically everyday at 12 am. 
28. Type in sudo crontab -e and press enter. It might ask you which editor to use, type in 2 to use nano and press enter. 
29. In the bottom of the file type in 0 0 * * * /bin/OHBackup_script what this does is execute the backup script everyday at 12 am. 
30. Now press control x then y and enter to save. 
31. Like I mentioned in the beginning the script only backs up openhab stuff like your items, things, sitemaps, configuration files etc, so stuff like the the mqtt server settings will not be backed up, those things we need to backup manually. I recommend backing up manually before/after every time you make a change to the raspberry pi that is not directly integrated with openhab or when you make an openhab software update. 
32. In order to backup manually we need to first shut down the pi so type in sudo halt and press enter. 
33. Once the pi is fully shut down and the lights are not blinking, unplug the power cable, sd card and flash drive. 
34. Then plug the sd card and flash drive into the computer. 
35. Next open up applepibaker, if you dont know what apple pi baker is then go check out my automation server setup guide mac version. 
36. Anyway type in your admin password for your computer and press ok. 
37. In the pi crust section select your sd card and then click create backup. 
38. It will then ask what to name it also where to save, name it what you want and choose your desktop as the location and click ok. 
39. Once it finished open up finder and select your flash drive. Right click, create a new folder, and name it SDCardBackups. 
40. Now, right click on the img file and click compress. 
41. Once it finished compressing drag the zip file from your desktop to the SDCardBackups folder. 
42. Once it copies over you are completely done backing up your automation server. 
43.You can now eject the flash drive as well as the sd card and plug them back into the pi. 
44. Once they are plugged in you can connect power again. 
45. Congratulations, you are now safe from potential failures and part of the small percentage of people that actually backup their OpenHAB server. Help spread awareness of backing up openhab by sharing this video everywhere you possibly can and save people from the headache of a failed server with no backup. To demonstrate a change made after a manual backup I am going to make a modification to the sitemap file. As you can see the sitemap is changed. Now for demonstration purposes I am going to issue a OpenHAB Runtime backup with the command sudo /bin/OHBackup_script normally your backup would occur automatically at 12am but for the purposes of this video I am going to back it up now. You can also use this command if you are about to do any modifications to the configuration files and want something to fall back on just in case you mess up.
For demonstration purposes I am going to simulate a raspberry pi failure that can happen to anyone and results in a complete data loss. 
[Hits raspberry pi with the hammer]
Now lets go over how to restore your openhab server if it did go through a failure. 
1. You would first plug both a new sd card either the same size or bigger than the original and the OHBackup flash drive into the computer. 
2. Then open finder to your SDCardBackups folder and drag the backup you want to use to your desktop. 
3. Once it finishes copying double click on the zip file to expand it into a proper img file. 
4. Then open up apple pi baker, type in your admin password and press ok. 
5. Now click on your sd card in the Pi-Crust Section. In the Pi-Ingredients section click on the 3 dots. It will bring up a mini finder, navigate to your desktop and click on the img file we previously expanded from the zip file. 
6. Then click restore backup. Once it is formatted eject the flash drive and unplug both the sd card and flash drive. Plug both items items into the pi and plug in the power cable. 
7. Once the pi boots up ssh into it through terminal. 
8. The first thing we have to do is get into the backup drive and see which zip file we want to restore to so type in cd /mnt/OHBackupDrive and press enter. 
9. To see what's inside type in ls and enter. I am going to backup to the latest zip file which is this one. 
10. Next we need to stop openhab so type in sudo systemctl stop openhab2.service and press enter. 
11. Now we are going to restore openhab so type in sudo $OPENHAB_RUNTIME/bin/restore /mnt/OHBackupDrive/INSERTBACKUPNAME.zip before you hit enter replace INSERTBACKUPNAME with the name of your backup and press enter. 
12. It will ask you to confirm, press y and enter. 
13. Finally, start up openhab again by typing in sudo systemctl start openhab2.service and press enter. 
14. Your openhab server should be fully restored. If I open up the basic UI you can see all the data is still there including the recently made modification to the sitemap. 
Finally, that concludes this complete guide to backing up and restoring your openhab server. In the description you will find links to all the parts and devices used in the video as well as a link to mksmarthouse.com/shop where I have a wide range of smart home kits that you can put together such as blinds, led strips, sprinkler system and door sensors. Alright thank you for watching and If you have any questions leave them in the comments section below or head over to mksmarthouse.com/forum. Good Bye!