Skip to content

Commit 3b2f87c

Browse files
committed
Merge pull request 'fix gitea-backup : add pre-backup cleanup and simplify rotation logic' (#1450) from gitea-backup-fix into master
Reviewed-on: https://gitea.obmondo.com/EnableIT/LinuxAid/pulls/1450
2 parents cde62a4 + 2fe0995 commit 3b2f87c

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

modules/enableit/common/files/gitea/gitea-backup.sh

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ MAX_LOCAL_BACKUPS=1 # Maximum number of local backups to retain
1010
MAX_S3_BACKUPS=3 # Maximum number of S3 backups to retain
1111
S3_BUCKET="s3://gitea-backup/"
1212
S3_ENDPOINT="https://s3.obmondo.com"
13+
SOURCE_DIR="/opt/gitea/data/git"
1314

1415
# Logging function
1516
log() {
@@ -19,41 +20,55 @@ log() {
1920
logger -t gitea-backup "$1" # Send message to syslog
2021
}
2122

22-
# Function to create backup directory
23+
# 1. Clean disk before creating a new backup
24+
check_and_clean_source() {
25+
26+
# Clean Gitea container's temp folder (Always do this to prevent stuck files)
27+
log "Cleaning Gitea container /tmp directory..."
28+
docker exec -u "$GITEA_USER" "$GITEA_CONTAINER" bash -c "rm -rf /tmp/gitea-dump-*" || true
29+
30+
# Remove leftover zips on the HOST source directory
31+
rm -f "$SOURCE_DIR"/gitea-dump-*.zip
32+
}
33+
34+
# 2. Create local backup directory
2335
create_backup_directory() {
2436
mkdir -p "$BACKUP_DIR"
2537
log "Backup directory created: $BACKUP_DIR"
2638
}
2739

28-
# Function to dump Gitea data
40+
# 3. Dump Gitea data (Generates the NEW backup)
2941
dump_gitea_data() {
3042
log "Dumping Gitea data..."
3143
docker exec -u "$GITEA_USER" "$GITEA_CONTAINER" bash -c \
3244
"cd ~ && /app/gitea/gitea dump -c /data/gitea/conf/app.ini"
3345
}
3446

35-
# Function to find and copy the latest dump
47+
# 4. Move the NEW dump and upload to S3
3648
copy_latest_dump() {
37-
local latest_dump
38-
latest_dump=$(docker exec "$GITEA_CONTAINER" ls -t '/data/git' | grep "$GITEA_DUMP_FILENAME" | head -n 1)
39-
40-
if [ -n "$latest_dump" ]; then
41-
docker cp "$GITEA_CONTAINER:/data/git/$latest_dump" "$BACKUP_DIR"
42-
log "Latest dump copied to $BACKUP_DIR: $latest_dump"
49+
# Move the newly generated backup
50+
if ls "$SOURCE_DIR"/gitea-dump-*.zip 1> /dev/null 2>&1; then
51+
mv "$SOURCE_DIR"/gitea-dump-*.zip "$BACKUP_DIR"
52+
else
53+
log "Error: New backup file was not found after dump command."
54+
exit 1
55+
fi
4356

44-
docker exec "$GITEA_CONTAINER" rm "/data/git/$latest_dump"
45-
log "Backup deleted from the container: $latest_dump"
57+
local latest_dump=$(ls -t "$BACKUP_DIR"/gitea-dump-*.zip 2>/dev/null | head -n 1)
4658

47-
log "Copying backup to S3..."
48-
aws s3 cp "$BACKUP_DIR/$latest_dump" "$S3_BUCKET" --endpoint-url="$S3_ENDPOINT"
59+
if [ -n "$latest_dump" ]; then
60+
log "Latest dump moved to $BACKUP_DIR: $(basename "$latest_dump")"
61+
log "Moving backup to S3..."
62+
63+
aws s3 cp "$latest_dump" "$S3_BUCKET" --endpoint-url="$S3_ENDPOINT"
4964

5065
perform_s3_backup_rotation
5166
else
5267
log "No backup found to copy."
5368
fi
5469
}
5570

56-
# Function to perform S3 backup rotation
71+
# 5. Rotate S3 backups
5772
perform_s3_backup_rotation() {
5873
log "Performing S3 backup rotation: Deleting older backups..."
5974
s3_backups=$(aws s3 ls "$S3_BUCKET" --recursive --endpoint-url="$S3_ENDPOINT" | sort -k1,2 | awk '{print $4}')
@@ -87,6 +102,7 @@ perform_local_backup_rotation() {
87102
}
88103

89104
# Execute functions directly
105+
check_and_clean_source
90106
create_backup_directory
91107
dump_gitea_data
92108
copy_latest_dump

0 commit comments

Comments
 (0)