remove old backups

This commit is contained in:
Love 2024-07-12 15:05:15 +02:00
parent c212a70020
commit d3136a5439

View File

@ -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} <<EOF
REMOTE_KEEP_OLD=${REMOTE_KEEP_OLD}
REMOTE_DIR=${REMOTE_DIR}
sqls=\$(ls ${REMOTE_DIR}/gitea-database-backup-*.sql.zst.enc 2>/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