Forgejo-Backup

This small tool for Forgejo creates a backup of all repositores and the packages of a given user. For repositories, it not only handles the code (all branches) but also metadata (pull requests, issues, releases and wiki pages). Release artifacts are also saved. For packages, only the metadata is saved – there’s no possibility to get the content via Forgejo’s API.

Installation

  1. Clone the project to your computer, or download the repository content as ZIP file.
  2. Make sure that at least Python 3.9 is installed.
  3. Optionally can now set up a virtual environment (venv) and install everything inside it (recommended). For creating it, execute python3 -m venv venv and afterwards activate it by executing . venv/bin/activate – the following steps stay the same, you just have to remember to activate the venv before using forgejo-backup later.
  4. Install the dependencies by executing pip3 install -r requirements.txt
  5. Make sure you have git installed (via your distribution’s package manager)!
  6. Test your installation by executing python3 backup.py or ./backup.py

An error message should be written to stderr pointing out that environment variables are missing.

Configuration / Usage

The necessary configuration has to be provided either as prepopulated environment variables or in a .env file:

  • BASE_URL the URL of a Forgejo API, e.g. https://codeberg.org/api/v1
  • USERNAME the name of the user of which the repositories should be backed up
  • API_KEY a valid key to access the API for the given user, also known as application tokens or access tokens
  • TARGET_DIR the location at which the backup should be created

It’s easy to run the script via Cron – use something like this as cron command (assuming you have a virtual environment set up in .venv):

/usr/bin/sh -c 'cd /path/to/forgejo-backup; . .venv/bin/activate; ./backup.py >/dev/null'

Any error messages will be printed on stderr, so it’s not a problem to ignore stdout, but without muting stdout you will always get an email about the backup process and not only when an error happened.