Scheduled automatic database backup setup

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.
Development and maintenance of all types of websites:
Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Our competencies:
Development stages
Latest works
  • image_website-b2b-advance_0.png
    B2B ADVANCE company website development
    1212
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_websites_belfingroup_462_0.webp
    Website development for BELFINGROUP
    852
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1041
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    822
  • image_bitrix-bitrix-24-1c_fixper_448_0.png
    Website development for FIXPER company
    815

Setting Up Automatic Database Backups on Schedule

Data loss due to missing backups is one of the most expensive mistakes in web application operations. Configuring automatic scheduled backups is a basic requirement for any production server.

Choosing a tool

DB Tool Format
PostgreSQL pg_dump / pg_dumpall SQL / custom
MySQL/MariaDB mysqldump / Percona XtraBackup SQL / binary
MongoDB mongodump BSON
Redis BGSAVE / AOF snapshot RDB / AOF
SQLite sqlite3 .backup binary

PostgreSQL: basic setup

#!/bin/bash
# /opt/scripts/backup-postgres.sh
BACKUP_DIR="/var/backups/postgres"
DB_NAME="myapp_production"
DATE=$(date +%Y%m%d_%H%M%S)
FILENAME="$BACKUP_DIR/${DB_NAME}_${DATE}.dump"

mkdir -p "$BACKUP_DIR"
pg_dump -U postgres -Fc "$DB_NAME" > "$FILENAME"

# Delete backups older than 7 days
find "$BACKUP_DIR" -name "*.dump" -mtime +7 -delete

# Check success
if [ $? -eq 0 ]; then
  echo "Backup successful: $FILENAME"
else
  echo "Backup FAILED" >&2
  exit 1
fi

Cron task:

0 2 * * * /opt/scripts/backup-postgres.sh >> /var/log/backup.log 2>&1

MySQL: setup with password via .my.cnf

# ~/.my.cnf (permissions 600)
[mysqldump]
user=backup_user
password=secret_password
mysqldump --single-transaction --routines --triggers myapp_db | \
  gzip > "/var/backups/mysql/myapp_$(date +%Y%m%d_%H%M%S).sql.gz"

--single-transaction is critical for InnoDB — creates consistent snapshot without table locking.

Rotation and storage

GFS (Grandfather-Father-Son) strategy:

  • Daily — stored 7 days
  • Weekly — stored 4 weeks
  • Monthly — stored 12 months

Implementation via logrotate or script with find -mtime.

Upload to S3/object storage

aws s3 cp "$FILENAME" "s3://company-backups/postgres/${DB_NAME}/" \
  --storage-class STANDARD_IA \
  --server-side-encryption AES256

# Or via rclone for any provider
rclone copy "$FILENAME" remote:backups/postgres/

Automatic rotation in S3 is configured through Lifecycle Policy: move to Glacier after 30 days, delete after 1 year.

Error notifications

# Send notification to Slack on failure
if [ $? -ne 0 ]; then
  curl -X POST "$SLACK_WEBHOOK" \
    -d '{"text": "CRITICAL: Database backup failed on '"$(hostname)"'"}'
fi

Alternative — healthcheck via Healthchecks.io or Better Uptime: script pings URL after successful backup, service raises alert if ping doesn't arrive.

Backup verification

Backup without restore testing — not a backup. Weekly test:

# Restore to test DB
pg_restore -U postgres -d test_restore --clean "$FILENAME"
# Check record count
psql -U postgres -d test_restore -c "SELECT COUNT(*) FROM users;"

Timeline

Setting up backup of one DB with rotation and S3 upload — 1 business day.