DEV Community

Dan Han
Dan Han

Posted on • Originally published at programmerdays.com on

Mastodon Backup

I have been active on Mastodon, posting one reactive, misguided, opinion per day. In this blog post, I’ll describe my mastodon backup process.

Why backup posts/toots?

The things I am posting to mastodon are important to me. I do my best to post thoughts and opinions that I think are worthwhile. I hope that they’ll be useful to others, which is why I’m not just posting to a local text file. But even if they don’t have value to others, I hope these old posts can be valuable to me. Maybe they can remind “future me” of something I forgot, or maybe they can help “future me” realize how far I have come. It’s really hard to remember how dumb I used to be. So, I want to have access to all my ramblings for all eternity.

I trust that the amazing folks maintaining my mastodon instance, fosstodon, want to provide a service that gives gives me full access to all of my toots. They surely want to preserve data and have it available for me. With that said, issues can arise. Stuff can happen that may block me from my own toots. Maybe there’s some bad actors that wipe out data. Maybe the instance runs out of funds, I miss all the warning messages, and it’s shut down before I get a chance to get my data. Maybe I turn on some setting that auto deletes my toots.

So, backup is important to me.

My backup process

My backup process depends on a few tools:

  • mastodon-archive provides the scripts to fetch the backup and render it nicely in html.
  • Docker provides a nice environment for running the script.
  • Synology NAS provides a task scheduler where I run Docker and stores the backup files for me.

The backup script

mastodon-archive has a thorough README and help documentation, so you will learn more by reading the docs there. I read them, skimmed them at least, and I found two commands to be particularly useful.

mastodon-archive archive danhan01010@fosstodon.org

Enter fullscreen mode Exit fullscreen mode

This will create a file like fosstodon.org.user.danhan01010.json that contains all of your data. The first time you run it, it will ask you to go through a couple of extra steps to create a “secret” file. This gives the script the information needed to fetch your archive. It’s a one time thing, and the same secret file can be reused in the future.

The json file can be difficult to read. It can be rendered into a nice html format by running

mastodon-archive html danhan01010@fosstodon.org

Enter fullscreen mode Exit fullscreen mode

which gives you a file like fosstodon.org.user.danhan01010.statuses.0.html, which you can open with your web browser. here’s an image of what I see after my latest backup.

Image of recent nicely rendered by mastodon archive html tool

Docker and Synology NAS

I wanted to automatically and periodically run the backup scripts, so I defaulted to tools I’m familiar with and have access to. Here’s my super simple Dockerfile.

FROM python:3.9-slim-buster
WORKDIR /app
COPY . /app
RUN python3.9 -m pip install mastodon_archive

Enter fullscreen mode Exit fullscreen mode

It does two things. It copies secret files over to Docker, and installs the mastodon_archive package. Note, installing from pip will ensure we are using the latest published version of the package. You could pin the install to a particular version or create the package from source files if you worry that a broken change could mess things up. I just went with a simple install for now.

Then, I use docker compose to make things easy to run. Here’s my docker-compose.yaml.

version: "3.9"
services:
  archive:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - .:/app
    command:
      - /usr/local/bin/mastodon-archive
      - archive
      - danhan01010@fosstodon.org
  html:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - .:/app
    command:
      - /usr/local/bin/mastodon-archive
      - html
      - danhan01010@fosstodon.org

Enter fullscreen mode Exit fullscreen mode

It’s a bit longer, but doesn’t really do too much either. It sets up two services: one to fetch a backup file and another to create an html file to view the backup. With all these docker files ready, you can run

docker-compose build && docker-compose run archive && docker-compose run html

Enter fullscreen mode Exit fullscreen mode

I added that command to a scheduled task in my local Synology NAS. It runs once a week, and my backup process is complete. Of course, I have synology also running backups of all my files, so these files are backed up as well. Redundancy is good!

Conclusion

If you put any thought or energy into your Mastodon posts, you will want to back them up. I posted how I do it, but I’m open to hearing about any other better strategies.

Top comments (0)