# Preventing SD card corruption in the event of power failure

I have a project that uses Pi's in remote locations (roadside cabinets dotted around a city next to important bits of water infrastructure) to read data via USB from some specialised data collection equipment and then transmit it (via an ADSL link) back up to a central DB. The Pi's are powered from a 5V supply derived from an industrial UPS (a Phoenix Contact MINI-DC-UPS) and the control signals from this UPS are managed by the data collection equipment so that messages can be inserted into the normal data stream from it to the Pi.

When the mains supply is out and the battery has dropped to a certain low voltage the UPS will raise an "ALARM" signal and the data collection equipment will inform the Pi that the power could go out at any time. If the supply recovers without being turned off the ALARM signal will go away and the Pi will be informed that it can go back to normal operation.

Having the Pi suddenly power down while in normal operation could corrupt the SD card so what is the best strategy for handling this? Can I put the Pi into a "safe" mode (remount the disk as read-only?) while waiting for a "power ok" signal or do I need to look at the various powerfail options listed in /etc/inittab?

Can anyone give me any pointers?

• Have you tried booting in read-only mode? from the description I can't see why you need a writable fs on the pi itself... – Gotschi Jul 24 '14 at 18:49
• I use the Pi's filesystem to store a rolling buffer of data in case of communications failure: the comms link is not terribly reliable and I may need to store several days/weeks of data (at ~50MB/day), so read-only mode isn't possible unfortunately. – Asher Hoskins Jul 24 '14 at 18:53
• then boot the system and applications in read-only and mount a writeable usb storage for the data.. – Gotschi Jul 24 '14 at 19:55
• I have not tried it on a Pi but you should be able to mount ram as a file system "mkdir /mnt/ramdisk && mount -t tmpfs none /mnt/ramdisk" google.com/… – rob Jul 25 '14 at 8:33
• @Gotschi - for most Linux and other distros, you need to mount the disk as read-writer as it needs to write to the disk as well to work. – Wilf Jul 26 '14 at 19:06

With multi-site deployment, where data loss is a concern as well as the time and cost of repairs, I wouldn't mess around. I would suggest a hardware solution: equipping each RPi with a hardware shutdown circuit. This could ensure that whenever power is removed from an RPi it will gracefully shut down and wait for power to be restored before bringing up its file system again. Notifications from the UPS could be ignored by the RPi since it would continue to operate until the supply voltage reached the shutdown threshold. This solution should also help keep the RPi safe during any maintenance work inside the cabinet.

There are various designs for these shutdown circuits. You'll find some suggestions on this SE site. These circuits are mostly designed to add convenience for the casual user. You might want to consider some design changes to tailor them to your application. For instance, some of the circuits use a large capacitor to ensure that power is available for the RPi to complete its shutdown sequence successfully: you might want to consider using a small rechargeable battery instead, which would be held fully charged by the 5V supply during normal operation - allowing for plenty of time for the RPi to handle any final data transmissions prior to closing shop.

One approach might be to provide a second small UPS, that is plugged into the first UPS.

I'm talking about the smaller plug strip type UPS. The power for the second UPS comes from the first UPS, therefor the second UPS is not in powerfail/shutdown mode until the first has failed and is no longer delivering any power.

The second UPS would also be connected to the PI via the USB link. Install the appropriate shutdown scripts from the second UPS manufacturers website on the pi.

First UPS runs out of battery, second UPS now kicks in to provide power and tell the pi to shutdown.

The best solution on my opinion is to use the UPS PIco, a specially designed for Raspberry Pi UPS, that offer a plenty of other features.

It is low cost, includes battery, no need for any extra cable, just put it on top of RPi.

I had the same problem and solve it by buying a battery that prevents short in the electrical system shut down http://www.amazon.com/gp/product/B00S4Q9BX4?psc=1&redirect=true&ref_=oh_aui_detailpage_o03_s00

I rather like the look of this item at Amazon: Uninterruptible Power Supply GM312 Mini UPS

It would seem all I would need to add is a simple circuit to detect when the incoming 12 volt supply had failed.

The R Pi could then shut down automatically either straight away or after a delay.

One issue I can see is that it would not be straightforward to get the R Pi to restart when the power was back on.

I am wondering if the solution might be, after a delay to put the Pi into some sort of minimum activity mode waiting for the 12 volts to come back on.