Configuring Raspberry Pi Zero W as NAS/backup server using Unison
"Free" cloud storage: overview
I built a super cheap linux box for backing up my files remotely. I used the cheapest components, mostly what I had on hand. My costs were less than $50, one time, if you factor in my friend who's willing to let this live at his house permanently ($free/pricele$$). The total power draw is about 3.4W at idle which works out to about $7/year at least in southern California where rates are sky high.
Parts:
- Raspberry pi Zero W: ~$12
- 2GB Micro SD card ($3?)
- Samsung S7 phone charger
- WD Elements 500GB HD (<$30)
- Old USB hub with power adapter ($10?)
Linux on a raspberry pi
1) wpa_supplicant.conf with unix line endings:
country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="YourNetworkSSID"
psk="Your Network's Passphrase"
key_mgmt=WPA-PSK
}
2) ssh (file is empty).
On my first boot of the Pi didn't manage to log into my network. Checking the SD card is confusing because on boot the Pi deletes the ssh and wpa file from the fat partition. To find out what's going wrong connect your pi to HDMI and run journalctl (dmesg reported wlan0 wasn't ready but not why). I had a typo in wpa_supplicant. I wasted a lot of time trying to find solutions, which involved lots of commands and configuration files which are all no longer used in a modern raspbian-stretch image. Don't believe what you read online.
The Pi's green light flashes quite a bit during boot but once it is solid the machine was ssh'able (about 1 minute). I removed the pi user (userdel) and made one for me (adduser), adding it to /etc/sudoers with sudoedit.
The idle power usage at this point was less than 1w if you can believe my killawatt.
Plugging in a hard drive
In /boot/config.txt I added max_usb_current=1 so that enough power is available to run the spinning HD. Actually, this wasn't enough for my drive, so I had to "backpower" the Pi. That means that I plugged the drive into a 1A powered hub and the hub into the pi's usb (not power) port. The hub powered both the pi and the drive. Some people say this is a bad idea, but only because it bypasses a fuse that would protect the Pi from a surge. My Pi didn't cost that much. Unfortunately in this configuration the power draw was too high to consistently start up the hard drive, so I ended up forward and backward powering the Pi. Which seems to cause no problems, but probably isn't the most efficient.
This explains how to mount and format an USB drive (not that different from any other type of drive).
Adding /dev/sda /mnt ext4 defaults,nofail to /etc/fstab makes the drive mount automatically at boot. chown user /mnt makes it so that your user can write to the mounted drive.
- hdparm is the standard program you see everywhere, but mostly doesn't work with USB drives
- sdparm is specific to SCSI drives in theory, but apparently many USB mass storage devices emulate SCSI. Not in my case anyway.
- sg3-utils is another way of sending SCSI commands. I'm really starting to doubt this whole USB pretends to be SCSI since it didn't work either
- udisks2 is another generic disk control program & deamon (not just for SCSI!) that didn't work.
- smartmontools from the name you'd think it's only for SMART (drive health) but apparently they threw the kitchen sink in there (total install footprint is 30MB!). This worked.
Cutting to the chase:
apt-get install smartmontools
add this line to /etc/rc.local:
smartctl --set=standby,15 /dev/sda
When spun up the total power usage reached 6W, but more importantly the idle consumption is just 3.4 W. That's around 3W for the HD for those keeping track. I suspect it could be much better but for the low-efficiency powered hub I'm using. The power adapter actually feels warm to the touch even at idle. When I was using the coolmax drive my idle consumption was just 2.4W, total using a LG phone USB charger, but the drive didn't always spin up on reboot.
When spun up the total power usage reached 6W, but more importantly the idle consumption is just 3.4 W. That's around 3W for the HD for those keeping track. I suspect it could be much better but for the low-efficiency powered hub I'm using. The power adapter actually feels warm to the touch even at idle. When I was using the coolmax drive my idle consumption was just 2.4W, total using a LG phone USB charger, but the drive didn't always spin up on reboot.
Attempting to save more power (made a tiny difference)
https://scribles.net/disabling-bluetooth-on-raspberry-pi/
https://www.jeffgeerling.com/blogs/jeff-geerling/raspberry-pi-zero-conserve-energy
https://www.jeffgeerling.com/blogs/jeff-geerling/raspberry-pi-zero-conserve-energy
Installing unison
apt-get install unisonBest to read the manual because there's lots of options to consider. Here's the basics of my pi.prf file on my PC. There's really too much to cover here sorry.
# Unison preferencesauto = true
fastcheck = yes
# Skip files ending in Name (mind the /)
ignore = Name *.tmp
ignore = Name Thumbs.db
ignore = Name ZbThumbnail.info
ignore = Name Release/*.pch
ignore = Name Debug/*.pch
ignore = Name Release/*.bsc
ignore = Name Debug/*.bsc
ignore = BelowPath $RECYCLE.BIN
ignore = BelowPath RECYCLER
ignore = Name System Volume Information
ignore = Name lost+found
root = h:/
root = ssh://user@DNS//mnt
times = true
xferbycopying = true
perms = 0
maxthreads = 1
Dynamic DNS if you don't have static IP
Afraid.org offers free and reliable dynamic DNS so that you can connect to the machine remotely even when it gets assigned a new IP. I used the wget/cron script and crontab -e to install it. Just editing the cron file doesn't work, even though comments in the file suggest it would.
Conclusion
Cheap. Reliable? I hope so. My last attempt at this using a netbook wasn't reliable, but I think that was the hardware. Plus the netbook used about 3W more power at idle so in the long term this will be a better option (I guess that depends on who pays for the power though).
update: worked great for a year and then the hard drive died. I suspect I wasn't powering it well enough since it was driven by the same USB adapter that also powered the pi. I switched to a new hard drive with it's own external brick, described here. While the power consumption was higher when on, I was able to make it overall much lower by hot-unplugging the USB hardware when not in use.