Dear Community,
I have had to restore my home-assistant from VM based to now a HA Supervised. This all worked really easy and well. I was impressed.
BUT after first start it showed the long term statistics and after a few minutes, I got an error and a new DB was created and the old one was called corrupted in file name.
I tried to do all recovery methods on this forum: DB image malformed. How to fix it? - #5 by eddriesen or Fix corrupted / malformed SQLite database (home-assistant_v2.db). But without any luck.
I then moved to mariadb to avoid this, the next time (hopefully) as I’m just running the ha addon., maybe need to consider using my NAS.
Anyway now my ask, how can I recover only my longterm statistics from db file in sqlite3 format and import into mariadb? This is what I tried and could even import some data, but it is not shown in HA:
sqlite3 home-assistant_v2.db .dump \
| sed -re 's/^PRAGMA .+OFF/SET FOREIGN_KEY_CHECKS=0;SET UNIQUE_CHECKS=0/' \
-e 's/^CREATE INDEX .+//' \
-e 's/^BEGIN TRANSACTION;$/SET autocommit=0;BEGIN;/' \
-e '/^CREATE TABLE .+ \($/,/^\);/ d' \
-e 's/^INSERT INTO "([^"]+)"/INSERT INTO \1/' \
-e 's/\\n/\n/g' \
| perl -pe 'use utf8; use open qw(:std :utf8); binmode STDOUT, ":utf8mb4";s/\\u([0-9A-Fa-f]{4})/pack"U*",hex($1)/ge' > output.sql
grep -v "INSERT INTO events" output.sql > import.sql
cat import.sql | mysql -h 127.0.0.1 homeassistant --default-character-set=utf8mb4 -u homeassistant -p
I also had problems importing it with datetime and references, so used this:
set sql_mode='';
set FOREIGN_KEY_CHECKS=0;
I have Influxdb, but was really enjoying the new energy dashboard and now all that data for 2 month is gone.
Any hint, idea is appreciated.
Thank you very much