Files
GiteaBackup/giteabackup.sh
Lucas Jensen b19fb48271 initial commit
2025-06-19 00:06:48 +00:00

37 lines
1.5 KiB
Bash
Executable File

#!/bin/bash
# Define paths
GITEA_BACKUP_DIR="/home/git/giteabackups"
MOUNTED_BACKUP_DIR="/mnt/gitea/giteabackups"
LUCAS_HOME="/home/lucas"
GITEA_DUMP_CMD="sudo -u git /usr/local/bin/gitea dump -c /etc/gitea/app.ini"
# Step 1: Stop the Gitea service
echo "Stopping Gitea service..."
sudo /bin/systemctl stop gitea || { echo "Failed to stop Gitea"; exit 1; }
# Step 2: Change to the backup directory
cd "$GITEA_BACKUP_DIR" || { echo "Failed to cd to $GITEA_BACKUP_DIR"; exit 1; }
# Step 3: Run the Gitea dump command
echo "Running Gitea dump..."
$GITEA_DUMP_CMD || { echo "Gitea dump failed"; sudo /bin/systemctl start gitea; exit 1; }
# Step 4: Start the Gitea service
echo "Starting Gitea service..."
sudo /bin/systemctl start gitea || { echo "Failed to start Gitea"; exit 1; }
# Step 5: Clean up old backups in the source directory (keep latest 5)
echo "Cleaning up old backups in $GITEA_BACKUP_DIR..."
ls -tp "$GITEA_BACKUP_DIR"/gitea-dump-*.zip 2>/dev/null | grep -v '/$' | tail -n +6 | xargs -r rm -f
# Step 6: Sync the backup files to mounted directory
echo "Syncing backups to $MOUNTED_BACKUP_DIR..."
sudo rsync -av --chown=lucas:lucas "$GITEA_BACKUP_DIR/" "$MOUNTED_BACKUP_DIR/"
# Step 7: Clean up old backups in the destination directory (keep latest 5)
echo "Cleaning up old backups in $MOUNTED_BACKUP_DIR..."
ls -tp "$MOUNTED_BACKUP_DIR"/gitea-dump-*.zip 2>/dev/null | grep -v '/$' | tail -n +6 | xargs -r rm -f
echo "✅ Gitea backup complete, synced, and pruned to 5 most recent dumps."