backup.sh 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/bin/bash
  2. set -e
  3. : "${BACKUP_DIR:=/var/tmp/zammad}"
  4. : "${BACKUP_TIME:=03:00}"
  5. : "${HOLD_DAYS:=10}"
  6. function zammad_backup {
  7. TIMESTAMP="$(date +'%Y%m%d%H%M%S')"
  8. echo "${TIMESTAMP} - backing up zammad..."
  9. # delete old backups
  10. if [ -d "${BACKUP_DIR}" ] && [ -n "$(ls "${BACKUP_DIR}")" ]; then
  11. find "${BACKUP_DIR}"/*_zammad_*.gz -type f -mtime +"${HOLD_DAYS}" -delete
  12. fi
  13. if [ "${NO_FILE_BACKUP}" != "yes" ]; then
  14. # tar files
  15. tar -czf "${BACKUP_DIR}"/"${TIMESTAMP}"_zammad_files.tar.gz /opt/zammad/storage
  16. fi
  17. #db backup
  18. pg_dump --dbname=postgresql://"${POSTGRESQL_USER}:${POSTGRESQL_PASS}@${POSTGRESQL_HOST}:${POSTGRESQL_PORT}/${POSTGRESQL_DB}" | gzip > "${BACKUP_DIR}"/"${TIMESTAMP}"_zammad_db.psql.gz
  19. echo "backup finished :)"
  20. }
  21. while true; do
  22. NOW_TIMESTAMP=$(date +%s)
  23. TOMORROW_DATE=$(date -d@"$((NOW_TIMESTAMP + 24*60*60))" +%Y-%m-%d)
  24. zammad_backup
  25. NEXT_TIMESTAMP=$(date -d "$TOMORROW_DATE $BACKUP_TIME" +%s)
  26. NOW_TIMESTAMP=$(date +%s)
  27. SLEEP_SECONDS=$((NEXT_TIMESTAMP - NOW_TIMESTAMP))
  28. echo "sleeping $SLEEP_SECONDS seconds until the next backup run..."
  29. sleep $SLEEP_SECONDS
  30. done