this post was submitted on 01 Jul 2023
41 points (97.7% liked)

Selfhosted

40296 readers
329 users here now

A place to share alternatives to popular online services that can be self-hosted without giving up privacy or locking you into a service you don't control.

Rules:

  1. Be civil: we're here to support and learn from one another. Insults won't be tolerated. Flame wars are frowned upon.

  2. No spam posting.

  3. Posts have to be centered around self-hosting. There are other communities for discussing hardware or home computing. If it's not obvious why your post topic revolves around selfhosting, please include details to make it clear.

  4. Don't duplicate the full text of your blog or github here. Just post the link for folks to click.

  5. Submission headline should match the article title (don’t cherry-pick information from the title to fit your agenda).

  6. No trolling.

Resources:

Any issues on the community? Report it using the report flag.

Questions? DM the mods!

founded 1 year ago
MODERATORS
 

I call this nonsense host ‘Ghost’, for me it’s similar to a tape backup solution. Fairly simple concept, it’s an old Pi1 + external mechanical drive that sits dormant with its ethernet off. Once a month, at a random time and random date it enables the ethernet, spins up the drive and pulls data from the main server to update its drive then goes black until next month. The only way to check or maintain the pi is a push button that toggles the ethernet interface. I slapped it together with some scrap wood, spare hardware and screwed it to a 2x4 in a dark corner of my basement. It’s my 5th string backup, the ultimate insurance policy because I’m mental.

top 22 comments
sorted by: hot top controversial new old
[–] [email protected] 8 points 1 year ago* (last edited 1 year ago) (1 children)

Sorry, I forgot to post the scripts. I'm a meathead electrical engineer so I don't use GIT or anything so here is the code dump. To summarize the setup's software:

  • cron to run the script that turns the ethernet on and runs rsync to pull data from the server. I have 12 cron entries for the various months/dates/times to run.
  • python script to monitor the button presses for manually running a backup or turning the ethernet port back on
  • bash script that runs the rsync job to pull data from the primary server

The backup script is fairly boring, just runs rsync and pushes the rsync log files back to the primary server. If it fails it sends me an email before turning the ethernet back off and going black.

#So here is my python code that runs the button press:

#!/usr/bin/env python

import RPi.GPIO as GPIO
import subprocess
import time
from multiprocessing import Process


#when this script first runs, at boot, disable ethernet
time.sleep(5) #wait 5 seconds for system to boot, then try and disable ethernet.
subprocess.call(['/home/pi/ethernet_updown.sh'], shell=False)

GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(22, GPIO.OUT) #controls TFT display backlight
GPIO.setup(23, GPIO.IN) #pull up or down is optional, the TFT display buttons have a hardware 10k pull up. Measure low tranisitions 
GPIO.setup(24, GPIO.IN)


#watches the button mounted above the USB port, in the Pi's case. 
def case_button_watch():
    while True:
        GPIO.wait_for_edge(3, GPIO.FALLING)
        #wait 100ms then check if its still low, debounce timer
        time.sleep(.100)
        if GPIO.input(3) == GPIO.LOW:
            #do something as it's a button press
            print('Button is pressed!')
            time.sleep(.900)
            if GPIO.input(3) == GPIO.LOW:
                #if the button is pressed for over 1 second its a long press. Run the backup script
                print('Button long press (greater than 1 second), running an unscheduled backup')
                subprocess.call(['/home/pi/backup.sh'], shell=False)
            else:
                #the press was greater than 100mS but less than 1000mS, just toggle the ethernet
                print('Button short press (less than 1 second), toggling the ethernet')
                subprocess.call(['/home/pi/ethernet_updown.sh'], shell=False)
        else:
            #do nothing as its interference
            print('GPIO3 debounce failed, it was noise')

#watches the buttons in the TFT display 
def TFT_display_button1():
    while True:
        GPIO.wait_for_edge(23, GPIO.FALLING)
        #wait 100ms then check if its still low, debounce timer
        time.sleep(.100)
        if GPIO.input(23) == GPIO.LOW:
            #do something as it's a button press
            print('Button GPIO23 is pressed!')
            GPIO.output(22, GPIO.HIGH) #turn the backlight ON
        else:
            #do nothing as its interference
            print('GPIO23 debounce failed, it was noise')

#watches the buttons in the TFT display
def TFT_display_button2():
    while True:
        GPIO.wait_for_edge(24, GPIO.FALLING)
        #wait 100ms then check if its still low, debounce timer
        time.sleep(.100)
        if GPIO.input(24) == GPIO.LOW:
            #do something as it's a button press
            print('Button GPIO24 is pressed!')
            GPIO.output(22, GPIO.LOW) #turn the backlight OFF
        else:
            #do nothing as its interference
            print('GPIO24 debounce failed, it was noise')

if __name__ == '__main__':

    #run three parallel processes to watch all three buttons with software debounce
    proc1 = Process(target=case_button_watch)
    proc1.start()

    proc2 = Process(target=TFT_display_button1)
    proc2.start()

    proc3 = Process(target=TFT_display_button2)
    proc3.start()

#bash script that toggles the ethernet - if its on, it turns it off. if its off, it turns it on:

#!/bin/bash

if sudo ifconfig | grep 'eth0' | grep 'RUNNING' > /dev/null; 
then 
    wall -n "$(date +"%Y%m%d_%H%M%S"):Ethernet going down"
    sudo ifconfig eth0 down	
else 
    wall -n "$(date +"%Y%m%d_%H%M%S"):Ethernet going up"
    sudo ifconfig eth0 up
fi
[–] [email protected] 2 points 1 year ago (1 children)

I very much feel the desire to stay away from Git repos for singular scripts like this. Maybe consider making it a gist though. Easier to keep track of by starring it in GitHub and perhaps even iterate on it in the future. :)

[–] [email protected] 3 points 1 year ago

I use Joplin notes to track my code revisions. It's incredibly crude but it works and keeps my documention private and is also my wiki for each server so I know what the heck I setup and did.

[–] [email protected] 3 points 1 year ago (2 children)
[–] [email protected] 1 points 1 year ago

We can see the video. Like the idea! I personally use a pi that I plug in manually as a backup solution. Do you have a git with the scripts you used for this pi?

[–] [email protected] 1 points 1 year ago (2 children)

Hmm I’m using wefwef and can’t see it

[–] [email protected] 1 points 1 year ago (1 children)

Me too, but the video is visible for me. Growing pains.

[–] [email protected] 1 points 1 year ago

Yeah I’m sure there will be a lot of those

[–] [email protected] 1 points 1 year ago (1 children)

But you are seeing that there should be a video right? Same for me. 🤔

[–] [email protected] 1 points 1 year ago

Yeah black background with a play button that has a diagonal line through it, as if it’s a broken link or something

[–] [email protected] 3 points 1 year ago (1 children)

That's a cool idea for an automated offline backup. My equivalent is an external hard drive connected to a mechanical timer plug. Every day it turns on for 30 minutes, that triggers a script that mounts the drive, syncs my files, then unmounts the drive. Then the plug turns off the drive until tomorrow.

I like this better though. I've got an old pi1 somewhere, might have to try it.

[–] [email protected] 2 points 1 year ago

This was my first idea but I got uneasy about an external device hard disconnecting. I was worried about data loss and corruption so I settled on just turning Ethernet on and off.

[–] [email protected] 1 points 1 year ago

What software did you use to manage the backups? This seems like a really neat idea!

[–] [email protected] 1 points 1 year ago

Thanks for posting this! Really interesting idea! Don't forget to give us the good stuff - the details on how to implement this :-)

[–] [email protected] 1 points 1 year ago (1 children)

Have you posted about this previously (outside of Lemmy)? I swear I have seen this, or something else implementing a similar concept, before.

[–] [email protected] 2 points 1 year ago (1 children)

Yep! In Reddit about a year ago.

[–] [email protected] 1 points 1 year ago

Well, good to see you (and this post) have made it to this corner of the internet.

[–] [email protected] 1 points 1 year ago

Thanks for posting this! Really interesting idea! Don't forget to give us the good stuff - the details on how to implement this :-)

[–] [email protected] 1 points 1 year ago

I had a longer reply typed, then Lemmy froze or something, but it seems you answered my questions so I'll just leave it at this - really cool solution you set up.

Mine is to just plug an external HDD into my server every few months and run a backup script. I may copy this down the road.

[–] [email protected] 1 points 1 year ago

Thanks for posting this! Really interesting idea! Don't forget to give us the good stuff - the details on how to implement this :-)

[–] [email protected] 1 points 1 year ago

Thanks for posting this! Really interesting idea! Don't forget to give us the good stuff - the details on how to implement this :-)

[–] [email protected] 0 points 1 year ago

Thanks for posting this! Really interesting idea! Don't forget to give us the good stuff - the details on how to implement this :-)