{"id":323,"date":"2017-10-23T19:28:58","date_gmt":"2017-10-23T19:28:58","guid":{"rendered":"http:\/\/www.cribbstechnologies.com\/?page_id=323"},"modified":"2017-10-30T20:15:43","modified_gmt":"2017-10-30T20:15:43","slug":"moving-home-assistant-logs","status":"publish","type":"page","link":"https:\/\/www.cribbstechnologies.com\/index.php\/home-assistant\/moving-home-assistant-logs\/","title":{"rendered":"Moving Home Assistant Logs"},"content":{"rendered":"<p>[et_pb_section bb_built=&#8221;1&#8243; admin_label=&#8221;Section&#8221; fullwidth=&#8221;on&#8221; specialty=&#8221;off&#8221;][et_pb_fullwidth_header admin_label=&#8221;Fullwidth Header&#8221; title=&#8221;Moving Home Assistant Logs&#8221; subhead=&#8221;Stop eating my SD card&#8221; background_layout=&#8221;light&#8221; text_orientation=&#8221;left&#8221; header_fullscreen=&#8221;off&#8221; header_scroll_down=&#8221;off&#8221; parallax=&#8221;off&#8221; parallax_method=&#8221;off&#8221; content_orientation=&#8221;center&#8221; image_orientation=&#8221;center&#8221; custom_button_one=&#8221;off&#8221; button_one_letter_spacing=&#8221;0&#8243; button_one_use_icon=&#8221;default&#8221; button_one_icon_placement=&#8221;right&#8221; button_one_on_hover=&#8221;on&#8221; button_one_letter_spacing_hover=&#8221;0&#8243; custom_button_two=&#8221;off&#8221; button_two_letter_spacing=&#8221;0&#8243; button_two_use_icon=&#8221;default&#8221; button_two_icon_placement=&#8221;right&#8221; button_two_on_hover=&#8221;on&#8221; button_two_letter_spacing_hover=&#8221;0&#8243; \/][\/et_pb_section][et_pb_section bb_built=&#8221;1&#8243; admin_label=&#8221;section&#8221;][et_pb_row admin_label=&#8221;row&#8221;][et_pb_column type=&#8221;4_4&#8243;][et_pb_text admin_label=&#8221;Text&#8221; background_layout=&#8221;light&#8221; text_orientation=&#8221;left&#8221; use_border_color=&#8221;off&#8221; border_color=&#8221;#ffffff&#8221; border_style=&#8221;solid&#8221;]<\/p>\n<p>&#8220;Read only file system&#8221; &#8220;No disk available&#8221; Most of us have been there at some time or another. Impatience or ignorance of knowing not shutting down a Raspberry Pi properly can chew up a SD card. Another lesser known issue is that SD cards have a limited lifespan; they only get a certain number of write cycles before they stop working. It&#8217;s like that Eddie Murphy movie where he&#8217;s only got a limited number of words he can say and they&#8217;re counted by leaves on a tree.<img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-324 aligncenter\" src=\"http:\/\/www.cribbstechnologies.com\/wp-content\/uploads\/2017\/10\/homepage_EB20120307REVIEWS120309980AR-300x169.jpg\" alt=\"\" width=\"300\" height=\"169\" srcset=\"https:\/\/www.cribbstechnologies.com\/wp-content\/uploads\/2017\/10\/homepage_EB20120307REVIEWS120309980AR-300x169.jpg 300w, https:\/\/www.cribbstechnologies.com\/wp-content\/uploads\/2017\/10\/homepage_EB20120307REVIEWS120309980AR.jpg 400w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Because of this write-intensive stuff like logs and databases a card can get worn out\u00a0<em>far<\/em> before it should due to excessive writes. There&#8217;s hope! You can use a USB drive and redirect all of this write-heavy activity there and your SD card will be safe.<\/p>\n<p>[\/et_pb_text][et_pb_text admin_label=&#8221;Instructions&#8221; background_layout=&#8221;light&#8221; text_orientation=&#8221;left&#8221; use_border_color=&#8221;off&#8221; border_color=&#8221;#ffffff&#8221; border_style=&#8221;solid&#8221;]<\/p>\n<p>First you need to figure out where Linux thinks the USB drive lives; in my case it was \/dev\/sda. Plug your drive into your Pi and then run the following command: (Thanks to <a href=\"https:\/\/sgrudadh.blogspot.co.uk\">Tinkerer<\/a> for the better than &#8216;figure it out&#8217; solution)<\/p>\n<pre lang=\"text\">sudo dmesg|tail -n 20|grep sd<\/pre>\n<p>You&#8217;ll get output that will look something like:<\/p>\n<pre lang=\"text\">[ 3.773637] sd 0:0:0:0: [sda] 62333952 512-byte logical blocks: (31.9 GB\/29.7 GiB) \r\n[ 3.783125] sd 0:0:0:0: [sda] Write Protect is off \r\n[ 3.791694] sd 0:0:0:0: [sda] Mode Sense: 0b 00 00 08 \r\n[ 3.792618] sd 0:0:0:0: [sda] No Caching mode page found \r\n[ 3.801020] sd 0:0:0:0: [sda] Assuming drive cache: write through \r\n[ 3.817186] sda: sda1 sda2 \u00a0 \r\n<\/pre>\n<p>Next you&#8217;ll create a mount point somewhere on the file system that you&#8217;ll use to mount the USB disk.<\/p>\n<pre lang=\"text\">sudo mkdir \/mnt\/usb-disk<\/pre>\n<p>Now you&#8217;ll use the mount commad to mount the usb disk to the directory<\/p>\n<pre lang=\"text\">sudo mount \/dev\/sda \/mnt\/usb-disk<\/pre>\n<p>We&#8217;ll want to make this permanent so we&#8217;ll have to add an fstab entry for it. First we&#8217;ll need to figure out what filesystem type to use. We&#8217;ll use fdisk to list out our disks<\/p>\n<pre lang=\"text\">sudo fdisk -l<\/pre>\n<p>Find the part where your drive is and it should look something like this:<\/p>\n<pre lang=\"text\">Disk \/dev\/sda: 7.5 GiB, 8053063680 bytes, 15728640 sectors\r\nUnits: sectors of 1 * 512 = 512 bytes\r\nSector size (logical\/physical): 512 bytes \/ 512 bytes\r\nI\/O size (minimum\/optimal): 512 bytes \/ 512 bytes\r\nDisklabel type: dos\r\nDisk identifier: 0x6f20736b\r\n<\/pre>\n<p>If the Disklabel type: says dos it&#8217;s FAT32. Otherwise it should spell out what the type is.<\/p>\n<pre lang=\"text\">sudo vim \/etc\/fstab<\/pre>\n<p>You&#8217;ll add a new line in the file replacing my example values with your actual values:<\/p>\n<pre lang=\"text\">\/dev\/sda \/mnt\/usb-disk fat32 auto<\/pre>\n<p>The goal here is to move the commonly written stuff to the USB drive to reduce writes to the SD card we&#8217;ll move the home-assistant.log and the database to the drive. Shut down home assistant before doing this stuff.<\/p>\n<pre lang=\"text\">mv \/home\/homeassistant\/.homeassistant\/home-assistant.log \/mnt\/usb-disk\/ \r\nmv \/home\/homeassistant\/.homeassistant\/home-assistant_v2.db \/mnt\/usb-disk<\/pre>\n<p>The files are now on the drive but we have to make sure that home assistant can write to them. We&#8217;ll create a symbolic link from the Home Assistant config directory to the USB disk<\/p>\n<pre lang=\"text\">ln -s \/home\/homeassistant\/.homeassistant\/home-assistant.log \/mnt\/usb-disk\/home-assistant.log \r\nln -s \/home\/homeassistant\/.homeassistant\/home-assistant_v2.db \/mnt\/usb-disk\/home-assistant_v2.db<\/pre>\n<p>Restart home assistant and make sure everything&#8217;s working appropriately. If not, check your permissions on the files on your USB drive. They should be owned by the user that you run Home Assistant as.<\/p>\n<pre lang=\"text\">sudo systemctl restart home-assistant.service<\/pre>\n<p>The logging is a bit trickier. We&#8217;ll need to modify the systemd unit for home assistant and add a new configuration file for journalctl. The venv install method already makes sure that the log messages from home assistant are tagged with the identifier &#8216;hass&#8217;. You can check your syslog by editing \/var\/log\/syslog and look for home assistant messages. If they&#8217;re already tagged at the beginning with something that looks like Oct 20 06:25:28 ubuntu hass[21411]: you can skip modifying the system unit. To modify the system unit you just need to edit the file:<\/p>\n<pre lang=\"text\">sudo vim \/etc\/systemd\/system\/home-assistant.service<\/pre>\n<p>and add (or modify) the following<\/p>\n<pre lang=\"text\">StandardOutput=syslog \r\nSyslogIdentifier=hass<\/pre>\n<p>This tells it to redirect standard output to the syslog file and to tag the messages with hass.<\/p>\n<p>Make sure to reload the system unit.<\/p>\n<pre lang=\"text\">sudo systemctl daemon-reload<\/pre>\n<p>Now we need to configure journalctl to split those messages out to a different log location. Create a hass config for journalctl:<\/p>\n<pre lang=\"text\">sudo vim \/etc\/rsyslog.d\/hass.conf<\/pre>\n<p>enter the following and save the file<\/p>\n<pre lang=\"text\">if $programname == 'hass' then \/mnt\/usb-disk\/hass-syslog.log<\/pre>\n<p>restart rsyslog<\/p>\n<pre lang=\"text\">sudo systemctl restart rsyslog<\/pre>\n<p>check to make sure it&#8217;s logging appropriately<\/p>\n<pre lang=\"text\">tail -f \/mnt\/usb-disk\/hass-syslog.log<\/pre>\n<p>Everything should be logging and using files on your external drive which should greatly prolong the life of your SD card. Happy automating!<\/p>\n<p>[\/et_pb_text][\/et_pb_column][\/et_pb_row][\/et_pb_section]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[et_pb_section bb_built=&#8221;1&#8243; admin_label=&#8221;Section&#8221; fullwidth=&#8221;on&#8221; specialty=&#8221;off&#8221;][et_pb_fullwidth_header admin_label=&#8221;Fullwidth Header&#8221; title=&#8221;Moving Home Assistant Logs&#8221; subhead=&#8221;Stop eating my SD card&#8221; background_layout=&#8221;light&#8221; text_orientation=&#8221;left&#8221; header_fullscreen=&#8221;off&#8221; header_scroll_down=&#8221;off&#8221; parallax=&#8221;off&#8221; parallax_method=&#8221;off&#8221; content_orientation=&#8221;center&#8221; image_orientation=&#8221;center&#8221; custom_button_one=&#8221;off&#8221; button_one_letter_spacing=&#8221;0&#8243; button_one_use_icon=&#8221;default&#8221; button_one_icon_placement=&#8221;right&#8221; button_one_on_hover=&#8221;on&#8221; button_one_letter_spacing_hover=&#8221;0&#8243; custom_button_two=&#8221;off&#8221; button_two_letter_spacing=&#8221;0&#8243; button_two_use_icon=&#8221;default&#8221; button_two_icon_placement=&#8221;right&#8221; button_two_on_hover=&#8221;on&#8221; button_two_letter_spacing_hover=&#8221;0&#8243; \/][\/et_pb_section][et_pb_section bb_built=&#8221;1&#8243; admin_label=&#8221;section&#8221;][et_pb_row admin_label=&#8221;row&#8221;][et_pb_column type=&#8221;4_4&#8243;][et_pb_text admin_label=&#8221;Text&#8221; background_layout=&#8221;light&#8221; text_orientation=&#8221;left&#8221; use_border_color=&#8221;off&#8221; border_color=&#8221;#ffffff&#8221; border_style=&#8221;solid&#8221;] &#8220;Read only file system&#8221; &#8220;No disk available&#8221; Most [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":364,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-323","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/pages\/323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/comments?post=323"}],"version-history":[{"count":7,"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/pages\/323\/revisions"}],"predecessor-version":[{"id":366,"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/pages\/323\/revisions\/366"}],"up":[{"embeddable":true,"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/pages\/364"}],"wp:attachment":[{"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/media?parent=323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}