I enjoy it when people go to absurd lengths to not spend money. There’s so much room for creativity within what we tools we already have available to us.
I also think that it’s important to try new things and see what we can learn from it. Trying and failing is all a part of learning. And we need more ideas and knowledge out there if the idea of open source is to spread and gain popularity.
If a project is not interesting or important to me, that doesn’t mean that someone else can’t benefit from it. I think if one person finds a project useful or interesting then that’s worth sharing alone. And if only one other person wants to contribute their time to that project, then that’s still pretty awesome.
I ended up using rsync to do my backups. I have a laptop, an Android phone with Termux, a HomeAssistant docker image on a Raspberry Pi 4 and a home PieFed instance on a Raspberry Pi 5.
Each RPi board will create a complete backup on it’s on storage. I’ll then make a copy of each backup to my laptop. And finally another copy from my laptop to an external usb storage device. I also made a specific folder on my Android phone for the purpose of syncing with my laptop.
What I like about rsync is that it can be made to only transfer any changed files and not everything each time.
Since I use docker images on both my RPi boards, I made a script that shuts down all the containers before make a backup copy and finally starting the containers again. I even made a script to do something similar to restore from the saved backup.
Took a lot of trail and error to get them working but I am pretty happy with it. I have the scripts here if anyone is interested. I labelled what I did but didn’t really leave comments explaining things. They are pretty simple anyways.
I could have used rsnapshot but learning how to use rsync has been interesting enough to me.
…Continued from PieFed Instructions…
~/pyfedi/.env.docker
file. Look at ~/pyfedi/env.sample
and add the other variables to ~/pyfedi/.env.docker
according to your needs.nano ~/pyfedi/env.sample
nano ~/pyfedi/.env.docker
docker compose down && docker compose up -d
Updating PieFed Docker Container
docker compose down
git pull
docker compose up --build
docker compose down && docker compose up -d
I decided to keep it simple and use the rsync
command which comes already installed on Raspberry Pi OS. The guide linked below does a good job of explaining rsync
in a step by step process.
Below the linked guide I’ll provide an example of the commands I use to Backup and Restore my raspberry Pi. This creates a copy of the /rootfs folders that make up your Raspberry Pi Operating System and User folders. The commands will exclude some folders that may cause issues when restoring a backup. The guide linked below has more details.
Since I am going to power down the Pi and physically connect it’s hard drive to my computer, I don’t have to worry about making backups on a live and running storage.
The below commands assume I also have an additional EXTERNAL_STORAGE hard drive connected to my computer. This means the backup command will copy the contents from the Raspberry Pi drive (/rootfs folder) to the EXTERNAL_STORAGE drive (/EXTERNAL_STORAGE/backup folder). The restore command will copy the contents from the EXTERNAL_STORAGE drive (/EXTERNAL_STORAGE/backup/rootfs folder) to the Raspberry Pi drive (/rootfs folder)
rsync
WILL delete data on the target location to sync all files and folders from the source location. Be mindful of which direction you are going to avoid any losses. I suggest testing it out on some other folders before commiting to backing up and restoring the entire Raspberry Pi. The guide linked below also covers exclusions to minimize backup sizes.
The backup storage MUST be formatted in EXT4 to make sure file permissions and attributes remain the same.
alias rsyncBACKUP="sudo rsync -avxhP --delete --exclude={'proc/','sys/','dev/','tmp/','run/','mnt/','media/','home/USERNAME/.cache','lost+found'} /media/USERNAME/rootfs /media/USERNAME/EXTERNAL_STORAGE/backup/"
rsyncRESTORE="sudo rsync -avxhP --delete --exclude={'proc/','sys/','dev/','tmp/','run/','mnt/','media/','home/USERNAME/.cache','lost+found'} /media/USERNAME/EXTERNAL_STORAGE/backup/rootfs/ /media/USERNAME/rootfs"
. ~/.bashrc
rsBACKUP
rsRESTORE
sudo apt install -y ufw
sudo apt install -y gufw
I haven’t figured out how to properly set this up for myself yet, but I figure it’s probably worth having for an additional layer of protection.
…Continued from PieFed Instructions…
Cloudflare Website Settings
These settings are suggested to help manage traffic. See here for more detailed information.
Create Rule
-> Change the following settings and values on Cloudflare to match what’s listed below:
Allow Inbox
/inbox
Create rule
-> Change the following settings on Cloudflare to match what’s listed below:
ActivityPub
/activities/
Or
/activities/
/api/
/nodeinfo/
/.well-known/webfinger
+ add setting
Ignore cache-control header and use this TTL
Deploy
to completeCreate rule
again
ActivityPub2
application/activity+json
Or
application/activity+json
application/ld+json
+ add setting
Ignore cache-control header and use this TTL
10 seconds
Deploy
to complete.env.docker
File
Create Token
-> Click Get Started
under Create Custom TokenPieFed
Continue to summary
-> Click Create Token
nano ~/pyfedi/.env.docker
CLOUDFLARE_API_TOKEN = 'ZONE.CACHE_PURGE_TOKEN'
CLOUDFLARE_ZONE_ID = 'API_ZONE_ID_TOKEN'
docker compose down && docker compose up -d
Troubleshooting
USERNAME
with your username.
cd ~/pyfedi
sudo chown -R USERNAME:USERNAME ./media
The lemmy instructions are simple and straight forward. When changing the fields asked of you in the instructions, it’s helpful to search and replace the required fields. In nano
when editing a file, press CTRL + \
and follow the instructions at the bottom of the window. This will find and replace text.
The Lemmy instructions show text for editing with {{ Example }}
. To avoid confusion, those curly braces must be removed and replaced with the expected data.
81
to 10633
curl -H 'Accept: application/activity+json' https://DOMAINNAME.COM/u/LEMMY_USERNAME
Updating Lemmy Docker Container
See here for more information.
docker compose down
docker compose pull
docker compose up -d
The PieFed installation instructions will provide more detailed information about each step. This guide does NOT cover any email setup for PieFed.
If you used NPM’s login page to test Cloudflare Tunnels, you will need to login to NPM and change the Port Forward from 81
to 8030
git clone https://codeberg.org/rimu/pyfedi.git
cd pyfedi
cp env.docker.sample .env.docker
nano .env.docker
nano compose.yaml
8030:5000
. You can change the external container port: 8030:
if you are using a custom port. Do NOT touch the internal container port :5000
.
ports:
- '8030:5000'
export DOCKER_BUILDKIT=1
sudo docker compose up --build
database system is ready to accept connections
in your terminal window after PieFed is done installing and loading. This means you are ready to attempt a connection through your Web Browser now.
cd ~/pyfedi
chown -R USERNAME:USERNAME ./pgdata
sudo docker exec -it piefed_app1 sh
export FLASK_APP=pyfedi.py
flask init-db
exit
CTRL + C
to stop PieFed.docker-compose up -d
USERNAME
to your username.sudo nano /etc/cron.d/piefed
5 2 * * * USERNAME docker exec piefed_app1 bash -c "cd /app && ./daily.sh"
5 4 * * 1 USERNAME docker exec piefed_app1 bash -c "cd /app && ./remove_orphan_files.sh"
1 */6 * * * USERNAME docker exec piefed_app1 bash -c "cd /app && ./email_notifs.sh"
!!Only proceed with these instructions after setting Cloudflare as your Primary DNS provider. This process may take up to a day after changing nameservers!!
The following instructions do a few things. First you will install Cloudflared
(with a ‘d’). Then you will be asked to log in, create a tunnel, run a tunnel and then creating a service (while the current tunnel is running) so your tunnel can run automatically from startup.
I’ve noted that this will be installed on the local host (where you are hosting an instance), we will be installing Cloudflared
on multiple devices for reasons I will cover later. Hopefully this reduces confusion later on.
credentials-file: /root/.cloudflared/<Tunnel-UUID>.json
-> credentials-file: /home/USERNAME/.cloudflared/<Tunnel-UUID>.json
CTRL + C
to exit this tunneltunnel: TUNNEL_ID
credentials-file: /home/USERNAME/.cloudflared/TUNNEL_ID.json
ingress:
- hostname: DOMAINNAME.COM
service: http://localhost:5050
- service: http_status:404
Run as a service
sudo cp ~/.cloudflared/config.yml /etc/cloudflared/config.yml
cloudflared service install
systemctl start cloudflared
systemctl status cloudflared
CTRL + C
when done to exitEnable SSL connections on Cloudflare site
If you used NPM as a reverse proxy and it’s set to port 81, go to any Web Browser and type in your DOMAINNAME.COM. You should be directed to NPM’s login page. Check the address bar and your domain name should have a padlock symbol followed by https://DOMAINNAME.COM. Note that it should read HTTPS:// (with an s) and not HTTP:// (without an s). HTTPS along with the padlock symbol means your connections are properly encrypted.
This is the most complicated step for self-hosting. If you can confirm your connection is encrypt, setting up other services and webapps are fairly straight forward.
Pick a port number between 1024-65,535. This is how Cloudflare will send data and remote connections to your instance without worrying about blocked ports. I like to use 5050 because it’s simple, easy to remember and not used by any of my other self-hosted services. To be consistent, for the rest of this guide I will use port 5050 as an example. Feel free to replace it with any port number you feel like using.
Router settings are different for each device, refer to a manual or call your ISP for support depending on your situation.
hostname -I
TCP
connections on port 5050
.
Internal
and External
fields, simply add 5050
to both fields.If you are only hosting a Lemmy or PieFed instance, you will be able to do that without the need of a Reverse Proxy which is described below. In this case you can simply use the default ports for Lemmy or PieFed. Replace my example port 5050
with the following depending on your needs:
10633
8030
A reverse proxy allows the local host machine to distribute incoming user connections to different services hosted on the local machine. For example, all data from Cloudflare comes in on port 5050
when accessing the DOMAINNAME.COM address. I can use Subdomains to redirect incoming connections on port 5050
to open ports on my local host machine.
For example, both Lemmy and PieFed can be hosted at the same time. We can use the subdomains lemmy. and piefed. to redirect traffic. When a user types lemmy.DOMAINNAME.COM into the address bar, Cloudflare will send the connection through 5050
to your home and private router which then continues to the Reverse Proxy. The Reverse Proxy running on the local host machine will catch the subdomain request and immediately switch to port 10633
where a connection to Lemmy will be completed. Typing in piefed.DOMAINNAME.COM will guide all requests to port 8030
where PieFed is running and complete that connection.
For simplicity, Nginx Proxy Manager is docker based with an easy to use web user interface that’s accessible through your local network connected Web Browser. It has it’s limitations but works fine for the current needs.
NPM is extremely simple to set up. Simply create a new folder, create a docker-compose.yml file filled with the necessary information and then run the container.
mkdir ~/npm
cd ~/npm
nano docker-compose.yml
services:
app:
image: 'jc21/nginx-proxy-manager:latest'
restart: always
ports:
- '5050:80'
- '81:81'
- '443:443'
volumes:
- ./data:/data
- ./letsencrypt:/etc/letsencrypt
Note that port 5050:
externally connects to NPM internally through port :80
. Make sure 5050
matches the Cloudflare Tunnel port you have decided on using.
docker compose up -d
and wait for the services to start running:81
into the address bar. For example 192.168.0.100:81
. See Port Fowarding for help finding your local IP address.81
to test Cloudflare Tunnels before installing Lemmy or PieFed. This is the login page for NPM. This can be quickly changed to the ports listed below after confirming a secure connection from Cloudflare Tunnels.
10633
8030
The required software to host Lemmy or PieFed will include
Additional software I will also cover but aren’t necessary are:
The official Docker instructions are clear, quick and simple. The process will also add their repository information for quick and easy updates. This will be installed as a service on your operating system.
I agree with Damage, a Docker containers may be more flexible compared to HomeAssistant add-ons. I currently have two Raspberry Pi’s. A Pi4 with a HomeAssistant container and a Pi5 with a simple file server, and a copy of Wikipedia and an unfederated PieFed instance which are all run from containers.
I bought my Pi5 used and it already had a fan installed and judging by how often the fan runs, it seems like a good idea to have. Especially since it’s more powerful compared to the Pi4.
If you’re comfortable working with terminal commands, you could simply use
rsync
for creating and maintaining backups. Works great for remote backups and file systems too. I’ve automated backups for my phone (Android with Termux) both Pi’s and my laptop. All those backups get sent to a partition on my laptop. Then that partition is completely backed up to an external SSD storage. Rsync compares files between the source folder and destination folder so that only the differences are transferred and sends less data. Tools likersnapshot
may simplify the whole process if you don’t have the time or energy to fiddle with rsync.I can share my docker compose file for HomeAssistant and some examples of my rsync commands if that’s a path you’re interested in.