Jeżeli padła Ci baza danych w HA to możesz skorzystać z tego rozwiązania. Jest to moje tłumaczenie posta który znajdziesz na forum home-assistant.io
1. Zatrzymaj HA poprzez polecenie ssh:
ha core stop
2.Pobierz pliki bazy danych: aktualny oraz ten uszkodzony:
home-assistant_v2.db
home-assistant_v2.db.corrupt.*
Drugiego pliku nie będziesz mógł skopiować poprzez Windowsa ponieważ zawiera : oraz + w nazwie pliku. Musisz wcześniej zmienić poprzez ssh jego nazwę i dopiero wtedy pobrać do na dysk
3. Zainstaluj program SQLite Browser, przy jego pomocy otwórz powyższe pliki. Otwórz pierwszy plik poprzez „Otwórz bazę danych” a drugi poprzez „Dołącz bazę danych”
Drugi plik podczas dołączania nazwij corrupt
bo bez tego nie zadziałają poniższe skrypty
4. As some statistics_meta record could be missing, get those from corrupt db:
-- insert possibly missing statistics_meta records insert into statistics_meta select row_number() over () + (select max(id) from statistics_meta) as id, c.statistic_id, c.source, c.unit_of_measurement, c.has_mean, c.has_sum, c.name from corrupt.statistics_meta c left outer join statistics_meta m on m.statistic_id = c.statistic_id where m.id is null;
5. Before inserting the records, update the id column of the existing data:
-- move existing ids, as all corrupt records are older update statistics set id = id + (select max(id) from corrupt.statistics);
6. Now, the statistics table is missing all those records from the corrupt database, so we import those with this sql:
-- import statistics from corrupt db insert into statistics select c.id, c.created, c.created_ts, m.new_id as metadata_id, c.start, c.start_ts, c.mean, c.min, c.max, c.last_reset, c.last_reset_ts, c.state, c.sum from corrupt.statistics c inner join ( select o.id as old_id , n.id as new_id , case when row_number() over (partition by o.id) > 1 or row_number() over (partition by n.id) > 1 then 0 else 1 end as test from corrupt.statistics_meta o inner join statistics_meta n on n.statistic_id = o.statistic_id ) m on m.old_id = c.metadata_id and m.test = 1
7. As the sum is incorrect for new values in current db, we update those as well:
-- update sum values causing mega negative values for day of corruption in Energy Dashboard update statistics set sum = sum + a.add_sum from ( select s.id, a.statistic_id, sum(a.lag_sum) as add_sum from statistics s inner join ( select * from ( select m.statistic_id, s.*, lag(s.sum) over (partition by s.metadata_id order by s.start_ts) as lag_sum from statistics s inner join statistics_meta m on m.id = s.metadata_id where s.sum is not null ) t where t.sum < t.lag_sum ) a on a.metadata_id = s.metadata_id and s.start_ts >= a.start_ts group by s.id, a.statistic_id ) a where a.id = statistics.id
8. Rebuild statistics_short_term from today’s statistics – it is needed so new statistic records have correct sum value
-- rebuild statistics_short_term delete from statistics_short_term; insert into statistics_short_term select row_number() over () as id, c.created, c.created_ts, c.metadata_id, c.start, c.start_ts, c.mean, c.min, c.max, c.last_reset, c.last_reset_ts, c.state, c.sum from statistics c where date(c.start_ts, 'unixepoch') = DATE('now') order by c.start_ts, c.id;
9. Skopiuj plik bazy danych na HA i odpal ha core start
Póki co dla mnie to działa 😉
A i jeszcze:
10. Zmień bazę na MariaDB! 😀
Źródło:
https://community.home-assistant.io/t/home-assistant-v2-db-corrupt-how-to-recover-long-term-statistics/346519/5