#!/bin/bash # Use gitea dump to make a backup of Gitea # https://docs.gitea.io/en-us/backup-and-restore/ set -eu set -o pipefail IFS="$(printf '\n\t')" PROGRAM="${0##*/}" INSTALL_DIR=/usr/local/bin TMP_DIR=/home/git/tmp WORK_DIR=/var/lib/gitea/ DATA_DIR=/var/lib/gitea/data/ LOG_DIR=/var/lib/gitea/log/ REPO_DIR=/home/git/gitea-repositories/ CONFIG_FILE=/etc/gitea/app.ini DUMP_DIR=/home/git/gitea-dumps MYSQL_USER={{ user_gitea_db }} MYSQL_DB={{ name_gitea_db }} MYSQL_PW={{ pass_gitea_db }} SCRIPT_LOGFILE=/home/git/gitea_backup.log SCRIPT_ERRLOGFILE=/home/git/gitea_backup.err {% raw %} if [ "$USER" != git ]; then echo "You must run this as user 'git'" && exit 1 fi print_help() { cat <<-_EOF $PROGRAM Usage: $PROGRAM {backup|restore |prune } Commands: backup Create a backup. Will save to $DUMP_DIR. restore Restore a previously created backup, given on the command line: , absolute path or relative to $PWD. prune Remove previously dumped backups and keep backups. Options: -h Print this help message and quit. _EOF } do_backup() { echo "Backing up Gitea..." stop_service output_dump_file="$DUMP_DIR/gitea-dump-$(date +'%Y%m%d').zip" cd "$INSTALL_DIR" || exit 1 ./gitea dump -V -f "$output_dump_file" -c "$CONFIG_FILE" -t "$TMP_DIR" --skip-attachment-data --skip-lfs-data -w "$WORK_DIR" chmod 640 "$output_dump_file" # Make the file readable by group for scp to different host by different user restart_service echo "Done." } do_restore() { echo "Restoring Gitea backup..." zip_file="$1" zip_dir="${zip_file%.*}" # Remove file extension chmod 600 "$zip_file" # Backup has changed permissions, restore them here echo "Unzipping $zip_file..." && unzip "$zip_file" -d "$zip_dir" && echo " OK." cd "$zip_dir" || exit 1 echo -n "Restoring $CONFIG_FILE..." && mv -f app.ini "$CONFIG_FILE" && echo " OK." echo -n "Restoring $DATA_DIR..." && mv -f data/* "$DATA_DIR" && rmdir data && echo " OK." echo -n "Restoring $LOG_DIR..." && mv -f log/* "$LOG_DIR" && rmdir log && echo " OK." echo -n "Restoring $REPO_DIR..." && mkdir -p "$REPO_DIR" && mv -f repos/* "$REPO_DIR" && rmdir repos && echo " OK." echo -n "Changing ownership..." && chown -R git:git "$CONFIG_FILE" /var/lib/gitea && echo " OK." echo -n "Restoring MySQL database..." && mysql --default-character-set=utf8mb4 -u"$MYSQL_USER" -p"$MYSQL_PW" "$MYSQL_DB" < gitea-db.sql && rm gitea-db.sql && echo " OK." rmdir "$zip_dir" restart_service regenerate_hooks echo "Done." } do_prune() { echo "Pruning Gitea backups and keeping $1 of them..." dumps=("$DUMP_DIR"/gitea-dump-*.zip) nb_of_dumps="${#dumps[@]}" nb_to_keep="$1" nb_to_prune=$((nb_of_dumps - nb_to_keep)) if [ "$nb_to_prune" -gt "$nb_of_dumps" ]; then echo "There are only $nb_of_dumps backups, quitting." && exit 1 fi if [ "$nb_to_prune" -le 0 ]; then echo "Nothing to do" && exit 1 else for ((i=0; i < nb_to_prune; i++)); do echo -n "Removing ${dumps[$i]}..." && rm "${dumps[$i]}" && echo " OK." done fi } regenerate_hooks() { echo "Regenerating hooks..." cd "$INSTALL_DIR" || exit 1 ./gitea admin regenerate hooks --config "$CONFIG_FILE" && echo "Done." } stop_service() { echo -n "Stopping service..." && sudo systemctl stop gitea.service && echo " OK." } restart_service() { echo -n "Restarting service..." && sudo systemctl restart gitea.service && echo " OK." } [ "$#" = 0 ] || [ "$1" = '-h' ] && print_help && exit 1 if [ "$#" = 2 ]; then if [ "$1" = restore ]; then echo "STARTING restore" >> "$SCRIPT_LOGFILE" do_restore "$2" >> "$SCRIPT_LOGFILE" 2>> "$SCRIPT_ERRLOGFILE" echo "DONE restore" >> "$SCRIPT_LOGFILE" elif [ "$1" = prune ]; then echo "STARTING prune" >> "$SCRIPT_LOGFILE" do_prune "$2" >> "$SCRIPT_LOGFILE" 2>> "$SCRIPT_ERRLOGFILE" echo "DONE prune" >> "$SCRIPT_LOGFILE" else print_help && exit 1 fi elif [ "$#" = 1 ]; then if [ "$1" != backup ]; then print_help && exit 1 fi echo "STARTING backup" >> "$SCRIPT_LOGFILE" do_backup >> "$SCRIPT_LOGFILE" 2>> "$SCRIPT_ERRLOGFILE" echo "DONE backup" >> "$SCRIPT_LOGFILE" else print_help && exit 1 fi exit 0 {% endraw %}