#!/bin/bash set -euo pipefail sudo apt install postgresql postgresql-client unzip -y GITEA_HOME="/var/lib/gitea" GITEA_CONF="$GITEA_HOME/app.ini" GITEA_USER="git" GITEA_VERSION="1.25.3" GITEA_BINARY="/usr/local/bin/gitea" GITEA_SERVICE="/etc/systemd/system/gitea.service" DB_NAME="giteadb" DB_USER="gitea" GITEA_BACKUPS_DIR="/backups/gitea" # Gitea user if ! id -u $GITEA_USER >/dev/null 2>&1; then adduser \ --system \ --shell /bin/bash \ --gecos 'Git Version Control' \ --group \ --disabled-password \ --home /home/git \ $GITEA_USER fi echo "---- Gitea user created ----" # Gitea folder structure mkdir -p $GITEA_HOME/{custom,data,log} chown -R $GITEA_USER:$GITEA_USER $GITEA_HOME chmod -R 750 $GITEA_HOME if [ ! -f $GITEA_BINARY ]; then wget -O /tmp/gitea "https://dl.gitea.com/gitea/$GITEA_VERSION/gitea-$GITEA_VERSION-linux-amd64" chmod +x /tmp/gitea mv /tmp/gitea $GITEA_BINARY fi echo "---- Gitea folder structure created ----" # Postgres first config DB_PASS=$(openssl rand -base64 12) sudo -u postgres psql < /usr/local/bin/restore-backup.sh </dev/null | sort | tail -n1) # Restore backup database, data, repos, logs if exist if [ -n "\$LATEST_BACKUP" ] && [ -f "\$LATEST_BACKUP" ]; then TMP_DIR=$(mktemp -d) unzip -o \$LATEST_BACKUP -d \$TMP_DIR if [ -d \$TMP_DIR/data ]; then cp -a \$TMP_DIR/data/* /var/lib/gitea/data/ fi if [ -d \$TMP_DIR/log ]; then cp -a \$TMP_DIR/log/* /var/lib/gitea/log/ fi if [ -d \$TMP_DIR/repos ]; then cp -aH \$TMP_DIR/repos/. /var/lib/gitea/data/repositories/ fi chown -R $GITEA_USER:$GITEA_USER $GITEA_HOME sudo -u postgres psql -d $DB_NAME < \$TMP_DIR/gitea-db.sql sudo -u postgres psql </dev/null 2>&1; then echo "---- Backup found, restoring Gitea ----" /usr/local/bin/restore-backup.sh else echo "---- No backup found in $GITEA_BACKUPS_DIR, skipping restore ----" fi # Save restore backup service cat > /etc/systemd/system/weekly-backup.service < /usr/local/bin/backup.sh < /etc/systemd/system/weekly-backup.service < /etc/systemd/system/weekly-backup.timer < "$GITEA_CONF" < "$GITEA_SERVICE" ${GITEA_SERVICE_CONTENT} EOF systemctl daemon-reload systemctl enable gitea fi # Enable timer for backup sudo systemctl enable --now weekly-backup.timer sudo systemctl status weekly-backup.timer systemctl is-active --quiet gitea || systemctl start gitea echo "---- Gitea installation completed ----"