openhab / openhab-docs

This repository contains the documentation for openHAB.
https://www.openhab.org/docs/
Other
272 stars 688 forks source link

Improve Linux config backup script #176

Closed metbril closed 7 years ago

metbril commented 7 years ago

The backup script suggested at the linux installation page suggests copying the entire /var/lib/openhab2 folder. I don't think this is necessary (and taking too much space of and writes to my RPi SD Card). The folders tmp and cache can be excluded. And at restoring them, these folders could first be simply removed.

I'm not a Linux expert, but will suggest an updated script. At first sight the cp command does not have an "exclude" option, so some additional lines might be necessary to implicitly include folders.

The restore script should then include some rm statements to remove what's needed.

metbril commented 7 years ago

Repo file: https://github.com/openhab/openhab-docs/blob/gh-pages/installation/linux.md

metbril commented 7 years ago

Suggestions welcome, @ThomDietrich.

metbril commented 7 years ago

I was thinking of a backup script something like this:

#!/bin/bash

# stop openhab instance (here: systemd service)
echo "Stopping service..."
sudo systemctl stop openhab2.service

# backup current installation with settings
TIMESTAMP="`date +%Y%m%d_%H%M%S`";
bakdir="$HOME/openhab2-backup/$TIMESTAMP";
mkdir -p "${bakdir}"
cp -arv /etc/openhab2 "${bakdir}/conf"
#cp -arv /var/lib/openhab2 "${bakdir}/userdata"
find /var/lib/openhab2 \( -path /var/lib/openhab2/tmp -prune -o -path /var/lib/openhab2/cache -prune \) -o -name '*' -exec cp -arv {} "${bakdir}/userdata"  \;

# restart openhab instance
echo "Starting service..."
sudo systemctl start openhab2.service

This is almost working. Unfortunately, the find command does not produce the expected results. Some of the files are put in the root of the userdata folder instead of the required subdirectory. I need some help with this.

ThomDietrich commented 7 years ago

Hey @rtvb thanks for making us aware of the problem and posting a solution. I've written these instructions a while ago and they were not strictly meant to be a perfectly complete script. Of course I get why this should be the case.

My suggestion would actually be to stay simple and just delete after copying. What do you think?

cp -arv /var/lib/openhab2 "${bakdir}/userdata"
rm -rf "${bakdir}/userdata/tmp"
rm -rf "${bakdir}/userdata/cache"
metbril commented 7 years ago

The point with my issue is that I prefer to limit writes to the sd card. Than a rm before the backup would be better but in that case the start of the service requires downloading any binding etc.

So I still think a restricted copy would be better.

We could prevent tmp and cache being copied or explicitly copy only required folders. But the latter is less future proof.

ThomDietrich commented 7 years ago
$ du -hs /var/lib/openhab2/*
80M     /var/lib/openhab2/cache
19M     /var/lib/openhab2/tmp
...

I'm not sure if this is really an issue... Anyway, an easier solution you could go with is rsync: http://stackoverflow.com/a/14789400 Only downside: rsync is not available on all distros by default.

Btw. If you execute the commands fast enough the files might not even get written from cache to memory. But I'm now stepping into a gray area 🌫

metbril commented 7 years ago

It is not about the size of the file but about my SD card aging more quickly.

http://superuser.com/questions/17350/whats-the-life-expectancy-of-an-sd-card

ThomDietrich commented 7 years ago

I got your point and am aware of the problem.

How about rsync then?

ThomDietrich commented 7 years ago

Writing 100MB once (if they are actually written before deleting from cache) will not wear out your sd card at a considerable speed. 100MB is nothing against 100000 to one million writes plus wear leveling on a e.g. 16GB card. Don't you think?

gahubs commented 7 years ago

Why not use subversion or something? Would be more flexible...

whopperg commented 7 years ago

Hey @rvtb, I did a script whigh updates the oh2 installation with the latest snapshot. I'don't use rsync for backup. See here: https://github.com/openhab/openhab-docs/pull/307#issuecomment-278290828

ThomDietrich commented 7 years ago

@gahubs this ticket is not about version-controlling your configuration. The backup goal is to backup and restore all userdata in case of an upgrade or hardware switch.

@whopperg as said in the other issue, let's moveyour script to a PR, then discuss it there. Please post the PR here, so @rtvb can follow ;)

whopperg commented 7 years ago

Fyi @rtvb @ThomDietrich : https://github.com/openhab/openhab-distro/pull/426