SD card backup: Corrupt /var/lib/dpkg/status (RPi3) 0.40.1 (original is fine)

I occasionally make backups on my laptop of the SD card on which HA runs (with ApplePiBaker). I wanted to try something risky today (move Hassbian to a USB stick and leave just the boot partition on the SD card), and decided I would do it on a backup card rather than my main one. Well, the backup is doing very strange things. This is frustrating me now since I am worried if something were to happen to my install, my backups are actually useless.

I noticed HA had trouble connecting to influx when it started.

I then tried to run apt-get update and got the following error:

E: Encountered a section with no Package: header
E: Problem with MergeList /var/lib/dpkg/status
E: The package lists or status file could not be parsed or opened.

I looked at the contents of this file: /var/lib/dpkg/status. I don’t know that much about linux but this file is part of the package manager and I’m not sure it should have the following contents (which looks like a log file):

[...]
Mar 12 04:27:07 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:07 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" c5293687-06d3-11e7-a7bd-000000000000 92822
Mar 12 04:27:07 hassbian hass[1527]: #033[32m17-03-12 04:27:07 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_relative_humidity_4_5=35.0; friendly_name=multisensor Relative Humidity, node_$
Mar 12 04:27:07 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:07 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" c531e545-06d3-11e7-a7be-000000000000 49948
Mar 12 04:27:07 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:07 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" c539432c-06d3-11e7-a7bf-000000000000 15935
Mar 12 04:27:07 hassbian hass[1527]: #033[32m17-03-12 04:27:07 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_ultraviolet_4_27=0.0; friendly_name=multisensor Ultraviolet, node_id=4 @ 2017-$
Mar 12 04:27:07 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:07 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" c53f5a5f-06d3-11e7-a7c0-000000000000 55525
Mar 12 04:27:07 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:07 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" c53f6b7b-06d3-11e7-a7c1-000000000000 81816
Mar 12 04:27:08 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:07 +0100] "POST /write?db=homeassistant&p=%5BREDACTED%5D&precision=ms&rp=&u=hass HTTP/1.1" 204 0 "-" "-" c58248da-06d3-11e7-a7c2-000000000000 56777
Mar 12 04:27:12 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:12 +0100] "POST /write?db=homeassistant&p=%5BREDACTED%5D&precision=ms&rp=&u=hass HTTP/1.1" 204 0 "-" "-" c878895c-06d3-11e7-a7c3-000000000000 18351
Mar 12 04:27:13 hassbian hass[1527]: #033[32m17-03-12 04:27:13 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_temperature_4_1=20.6; friendly_name=multisensor Temperature, node_id=4, unit_o$
Mar 12 04:27:13 hassbian hass[1527]: #033[32m17-03-12 04:27:13 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_luminance_4_3=0.0; friendly_name=multisensor Luminance, node_id=4, unit_of_mea$
Mar 12 04:27:13 hassbian hass[1527]: #033[32m17-03-12 04:27:13 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_relative_humidity_4_5=35.0; friendly_name=multisensor Relative Humidity, node_$
Mar 12 04:27:13 hassbian hass[1527]: #033[32m17-03-12 04:27:13 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_ultraviolet_4_27=0.0; friendly_name=multisensor Ultraviolet, node_id=4 @ 2017-$
Mar 12 04:27:13 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:13 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" c9095730-06d3-11e7-a7c4-000000000000 39169
Mar 12 04:27:13 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:13 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" c90b842b-06d3-11e7-a7c5-000000000000 39422
Mar 12 04:27:13 hassbian hass[1527]: #033[32m17-03-12 04:27:13 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_temperature_4_1=20.6; friendly_name=multisensor Temperature, node_id=4, unit_o$
Mar 12 04:27:13 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:13 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" c90bcce9-06d3-11e7-a7c6-000000000000 55629
Mar 12 04:27:13 hassbian hass[1527]: #033[32m17-03-12 04:27:13 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_luminance_4_3=0.0; friendly_name=multisensor Luminance, node_id=4, unit_of_mea$
Mar 12 04:27:13 hassbian hass[1527]: #033[32m17-03-12 04:27:13 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_relative_humidity_4_5=35.0; friendly_name=multisensor Relative Humidity, node_$
Mar 12 04:27:13 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:13 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" c90e17ec-06d3-11e7-a7c7-000000000000 55769
Mar 12 04:27:13 hassbian hass[1527]: #033[32m17-03-12 04:27:13 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_ultraviolet_4_27=0.0; friendly_name=multisensor Ultraviolet, node_id=4 @ 2017-$
Mar 12 04:27:14 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:13 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" c918d259-06d3-11e7-a7c8-000000000000 56427
Mar 12 04:27:14 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:13 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" c91a693a-06d3-11e7-a7c9-000000000000 62957
Mar 12 04:27:14 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:13 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" c91ad62a-06d3-11e7-a7ca-000000000000 77864
Mar 12 04:27:14 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:14 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" c91bfac3-06d3-11e7-a7cb-000000000000 81839
Mar 12 04:27:17 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:17 +0100] "POST /write?db=homeassistant&p=%5BREDACTED%5D&precision=ms&rp=&u=hass HTTP/1.1" 204 0 "-" "-" cb7178d5-06d3-11e7-a7cc-000000000000 64355
Mar 12 04:27:18 hassbian hass[1527]: #033[32m17-03-12 04:27:18 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.date__time=2017-03-12, 04:26; friendly_name=Date & Time, icon=mdi:calendar-clock @ 2017-03$
Mar 12 04:27:18 hassbian hass[1527]: #033[32m17-03-12 04:27:18 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.time__date=04:26, 2017-03-12; friendly_name=Time & Date, icon=mdi:calendar-clock @ 2017-03$
Mar 12 04:27:18 hassbian hass[1527]: #033[32m17-03-12 04:27:18 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.time=04:26; friendly_name=Time, icon=mdi:clock @ 2017-03-12T04:26:16.570459+01:00>, new_st$
Mar 12 04:27:18 hassbian hass[1527]: #033[32m17-03-12 04:27:18 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.time_utc=03:26; friendly_name=Time (UTC), icon=mdi:clock @ 2017-03-12T04:26:16.578782+01:0$
Mar 12 04:27:18 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:18 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" cbd8e112-06d3-11e7-a7cd-000000000000 17517
Mar 12 04:27:18 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:18 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" cbd8fd58-06d3-11e7-a7ce-000000000000 52924
Mar 12 04:27:18 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:18 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" cbdfcb18-06d3-11e7-a7cf-000000000000 20454
Mar 12 04:27:18 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:18 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" cbe06f7b-06d3-11e7-a7d0-000000000000 32406
Mar 12 04:27:20 hassbian hass[1527]: #033[32m17-03-12 04:27:20 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_temperature_4_1=20.6; friendly_name=multisensor Temperature, node_id=4, unit_o$
Mar 12 04:27:20 hassbian hass[1527]: #033[32m17-03-12 04:27:20 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_luminance_4_3=0.0; friendly_name=multisensor Luminance, node_id=4, unit_of_mea$
Mar 12 04:27:20 hassbian hass[1527]: #033[32m17-03-12 04:27:20 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_relative_humidity_4_5=35.0; friendly_name=multisensor Relative Humidity, node_$
Mar 12 04:27:20 hassbian hass[1527]: #033[32m17-03-12 04:27:20 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_ultraviolet_4_27=0.0; friendly_name=multisensor Ultraviolet, node_id=4 @ 2017-$
Mar 12 04:27:20 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:20 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" ccebdb8c-06d3-11e7-a7d1-000000000000 17160
Mar 12 04:27:20 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:20 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" ccf299e5-06d3-11e7-a7d2-000000000000 16850
Mar 12 04:27:20 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:20 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" ccf3ce96-06d3-11e7-a7d3-000000000000 29674
Mar 12 04:27:20 hassbian hass[1527]: #033[32m17-03-12 04:27:20 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_temperature_4_1=20.6; friendly_name=multisensor Temperature, node_id=4, unit_o$
Mar 12 04:27:20 hassbian hass[1527]: #033[32m17-03-12 04:27:20 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_luminance_4_3=0.0; friendly_name=multisensor Luminance, node_id=4, unit_of_mea$
Mar 12 04:27:20 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:20 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" ccf4539b-06d3-11e7-a7d4-000000000000 76127
Mar 12 04:27:20 hassbian hass[1527]: #033[32m17-03-12 04:27:20 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_relative_humidity_4_5=35.0; friendly_name=multisensor Relative Humidity, node_$
Mar 12 04:27:20 hassbian hass[1527]: #033[32m17-03-12 04:27:20 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_ultraviolet_4_27=0.0; friendly_name=multisensor Ultraviolet, node_id=4 @ 2017-$
Mar 12 04:27:20 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:20 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" cd0385f7-06d3-11e7-a7d5-000000000000 20512
Mar 12 04:27:20 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:20 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" cd050a3c-06d3-11e7-a7d6-000000000000 28968
Mar 12 04:27:20 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:20 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" cd0bc81c-06d3-11e7-a7d7-000000000000 22781
Mar 12 04:27:20 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:20 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" cd0bf142-06d3-11e7-a7d8-000000000000 82507
Mar 12 04:27:22 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:22 +0100] "POST /write?db=homeassistant&p=%5BREDACTED%5D&precision=ms&rp=&u=hass HTTP/1.1" 204 0 "-" "-" ce6ac4e7-06d3-11e7-a7d9-000000000000 21296
Mar 12 04:27:26 hassbian hass[1527]: #033[32m17-03-12 04:27:26 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_temperature_4_1=20.6; friendly_name=multisensor Temperature, node_id=4, unit_o$
Mar 12 04:27:26 hassbian hass[1527]: #033[32m17-03-12 04:27:26 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_luminance_4_3=0.0; friendly_name=multisensor Luminance, node_id=4, unit_of_mea$
Mar 12 04:27:26 hassbian hass[1527]: #033[32m17-03-12 04:27:26 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_relative_humidity_4_5=35.0; friendly_name=multisensor Relative Humidity, node_$
Mar 12 04:27:26 hassbian hass[1527]: #033[32m17-03-12 04:27:26 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_ultraviolet_4_27=0.0; friendly_name=multisensor Ultraviolet, node_id=4 @ 2017-$
Mar 12 04:27:26 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:26 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" d0d48cfc-06d3-11e7-a7da-000000000000 19711
Mar 12 04:27:26 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:26 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" d0d4cb45-06d3-11e7-a7db-000000000000 30354
Mar 12 04:27:27 hassbian hass[1527]: #033[32m17-03-12 04:27:27 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_temperature_4_1=20.6; friendly_name=multisensor Temperature, node_id=4, unit_o$
Mar 12 04:27:27 hassbian hass[1527]: #033[32m17-03-12 04:27:27 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state sensor.multisensor_luminance_4_3=0.0; friendly_name=multisensor Luminance, node_id=4, unit_of_mea$
Mar 12 04:27:27 hassbian influxd[788]: [httpd] 127.0.0.1 - hass [12/Mar/2017:04:27:26 +0100] "POST /write?db=homeassistant HTTP/1.1" 204 0 "-" "python-requests/2.13.0" d0d6dac3-06d3-11e7-a7dc-000000000000 72430
[...]

At the end the file appeared to be corrupted, it had a bunch of junk characters.

I’m not sure how my restore of the image could have put a log into this file. There was a backup of this file in the same folder and it had similar corruption.

I found a backup of the file in /var/backups as well, and it is supposed to look like this (and when I rebooted with my original SD card this is what I found in /var/lib/dpkg/status as well):

Package: python-apt-common
Status: install ok installed
Priority: optional
Section: python
Installed-Size: 478
Maintainer: APT Development Team <[email protected]>
Architecture: all
Source: python-apt
Version: 0.9.3.12
Replaces: python-apt (<< 0.7.98+nmu1)
Depends: python | python3
Breaks: python-apt (<< 0.7.98+nmu1)
Enhances: python-apt, python3-apt
Description: Python interface to libapt-pkg (locales)
 The apt_pkg Python interface will provide full access to the internal
 libapt-pkg structures allowing Python programs to easily perform a
 variety of functions.
 .
 This package contains locales.

Package: python-dnspython
Status: install ok installed
Priority: optional
Section: python
Installed-Size: 554
Maintainer: Debian Python Modules Team <[email protected]>
Architecture: all
Source: dnspython
Version: 1.12.0-1
Depends: python:any (<< 2.8), python:any (>= 2.7.5-5~)
Description: DNS toolkit for Python
 dnspython is a DNS toolkit for Python. It supports almost all record types. It
 can be used for queries, zone transfers, and dynamic updates. It supports TSIG
 authenticated messages and EDNS0.
 .
 dnspython provides both high and low level access to DNS. The high level
 classes perform queries for data of a given name, type, and class, and return
 an answer set. The low level classes allow direct manipulation of DNS zones,
 messages, names, and records.
Homepage: http://www.dnspython.org

After restoring this file on the backup SD card I was able to run apt-get update and upgrade successfully again.

But I still couldn’t get influxdb and homeassistant to communicate to each other (grafana and influx also had difficulty, I could only see very limited datasets from specific time periods). I suppose if one file got corrupt it’s possible that other files did also, and perhaps this happened to some of the data in influx?

Has anyone successfully restored HA from an SD card backup? Is there something I need to be conscious of?