diff --git a/backup-gitea.sh b/backup-gitea.sh index 5c00c7a..44974c6 100755 --- a/backup-gitea.sh +++ b/backup-gitea.sh @@ -15,6 +15,7 @@ REMOTE_USER="tom" REMOTE_HOST="nordicdatarefinement.com" REMOTE_PORT="23" REMOTE_DIR="/mnt/hdd/gitea-backup" +REMOTE_KEEP_OLD=7 REQUIRED_PROGRAMS=("rsync" "mysqldump" "zstd" "tar" "systemctl" "openssl") for prog in "${REQUIRED_PROGRAMS[@]}"; do @@ -87,3 +88,19 @@ log "Backups sent successfully." log "Removing dumps locally" rm -r "${BACKUP_DIR}" + +log "Cleaning up old backups on remote server, keeping ${REMOTE_KEEP_OLD}..." + +ssh -p ${REMOTE_PORT} ${REMOTE_USER}@${REMOTE_HOST} </dev/null) + if [ \$(echo "\${sqls}" | wc -l) -gt ${REMOTE_KEEP_OLD} ]; then + echo "\${sqls}" | sort | head -n -${REMOTE_KEEP_OLD} | xargs -I {} rm -- {} + fi + tars=\$(ls ${REMOTE_DIR}/gitea-backup-*.tar.zst.enc 2>/dev/null) + if [ \$(echo "\${tars}" | wc -l) -gt ${REMOTE_KEEP_OLD} ]; then + echo "\${tars}" | sort | head -n -${REMOTE_KEEP_OLD} | xargs -I {} rm -- {} + fi +EOF