🚧 Under construction — VoTechWorks is being built. A free tech program — complimentary, as our commitment to get America working.
voTechWorksGrade 13 · a vocational technology learning center, by urbanicity
newSchoolWorks.com › VoTechWorks › Technology & Design › Data Management / Database Administration › Database Backup and Recovery › Lesson 6
TEC-DATA86-04 · Lesson 6 · 10 clock hours · practice

Backup Automation with Cron, SQL Server Agent, and Shell Scripts

Objective. Students will write and schedule automated backup scripts using cron for PostgreSQL and Linux-based Oracle, and SQL Server Agent jobs for SQL Server, including logging and email-alert placeholders.

⚠ Pending professional review

This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.

VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.

Why Automate Backups?

Manual backups depend on a person remembering to run them. That dependency is a single point of failure. Automated backup schedules run whether or not anyone is at the keyboard, and they create audit trails — logs that prove a backup happened and record whether it succeeded. Every major certification exam and every production environment expects DBAs to automate backups and to capture evidence of each run.

Cron and Shell Scripts for PostgreSQL (and Linux-hosted Oracle)

Cron is the standard Linux/Unix job scheduler. Each user has a crontab — a table of scheduled commands. You edit it with `crontab -e`. Each line has five time fields (minute, hour, day-of-month, month, day-of-week) followed by the command to run.

A well-written backup shell script should do four things:

1. Call the backup utility with a timestamped filename. For PostgreSQL, the utility is `pg_dump`. A timestamped name such as `mydb_2025-07-14_02-00.dump` prevents files from overwriting each other and makes it easy to identify when each backup was created.

2. Check the exit code. Every Linux command sets an exit code when it finishes: `0` means success; anything else means an error. The shell variable `$?` holds the exit code of the last command. Your script must read `$?` immediately after the backup command and branch accordingly.

3. Write a result to a log file. Append a timestamped line to a persistent log file for every run — success and failure. Operations staff and auditors rely on this log.

4. Delete backup files older than the retention window. Storage is not free. Use `find /backup/dir -name "*.dump" -mtime +N -delete` where `N` is the number of days in your retention policy. Make `N` a variable at the top of the script so it is easy to change.

After you write the script, make it executable (`chmod +x backup.sh`), then add a crontab entry. For example, to run at 2:00 AM every day:

```

0 2 * /opt/scripts/pg_backup.sh >> /var/log/pg_backup_cron.log 2>&1

```

Verify the schedule by checking `crontab -l` and — after the scheduled time passes — inspecting the log file.

Security note: Store database credentials in a restricted file (such as `~/.pgpass` for PostgreSQL, with permissions `600`) rather than in plain text inside the script. Never hard-code passwords where other users can read them.

SQL Server Agent Jobs

SQL Server Agent is a Windows service that ships with SQL Server (including Developer Edition). It must be started and set to start automatically before any scheduled job will run. Verify its status in SQL Server Configuration Manager or Services before building jobs.

A job is a named container for one or more steps and one or more schedules. Each step runs a T-SQL statement, an operating-system command, or another subsystem action. You can view all recent executions in the Job Activity Monitor and in the job history stored in the `msdb` system database.

A classic three-tier backup strategy inside a single Agent job uses separate steps:

Use job-step conditions ("on success go to next step," "on failure write to history") to control flow. Configure the job to write history so that the Activity Monitor shows success or failure for every execution. Set the maximum job-history rows high enough that you retain at least several days of evidence.

Logging and Email-Alert Placeholders

Both platforms support alerting. In bash scripts, you can add a placeholder call to a mail utility (such as `sendmail` or `mail`) inside the failure branch, commented with a note explaining what SMTP configuration is needed. In SQL Server Agent, you can configure Operators (named email addresses) and attach notifications to a job so that the Agent emails the operator on failure. Even if email is not yet configured in your lab, your job definition should include the notification step as a placeholder.

Good log entries include: timestamp, database name, backup file path, file size if available, and success/failure status. Consistent log formats make it far easier to write monitoring scripts or feed entries into a centralized logging system later.

Code Quality and Peer Review

Automation scripts are production assets. They should be:

Peer review catches mistakes before they reach production. Use the provided checklist systematically: timestamped filenames, exit-code checking, log writing, retention cleanup, and commented code. Give specific, constructive written feedback, then revise your own script based on what your reviewer found.

A Note on Safety and Access Control

Backup scripts run with real database credentials and write files to disk. Follow the principle of least privilege: the OS user running the script should have only the database permissions needed to perform the backup, and only write access to the backup directory. Restrict the backup directory so that other OS users cannot read the dump files, which may contain sensitive data. Log files should also be protected from unauthorized access.

Key terms9

pg_dumpThe PostgreSQL command-line utility that exports a single database to a file; supports multiple output formats and is the standard tool for logical PostgreSQL backups.
crontabA per-user configuration file on Linux/Unix systems that lists commands and their schedules for execution by the cron daemon; edited with the command `crontab -e`.
exit codeAn integer value returned by a Linux command or script when it finishes; 0 conventionally means success and any non-zero value indicates an error; stored in the shell variable `$?`.
SQL Server AgentA Windows service included with SQL Server that stores and executes scheduled jobs, alerts, and operators; job history is kept in the msdb system database.
differential backupA backup that captures only the data pages that have changed since the most recent full backup, making it smaller than a full backup and faster to create.
transaction-log backupA backup of the SQL Server transaction log that records all committed transactions since the last log backup; enables point-in-time recovery and controls log file growth in the Full recovery model.
retention periodThe defined length of time that backup files are kept before being deleted; enforced in shell scripts with the `find -mtime` flag to remove files older than the specified number of days.
Job Activity MonitorA SQL Server Management Studio panel that displays the current status and recent run history of all SQL Server Agent jobs on an instance.
least privilegeA security principle stating that a user account or process should be granted only the minimum permissions required to perform its function, limiting the damage that can result from an error or compromise.
In class: Write a bash script that calls pg_dump with a timestamped filename, checks the exit code, writes a success or failure message to a log file, and deletes backup files older than a configurable retention period using find -mtime; schedule it with crontab -e and verify it runs at the correct time. · Create a SQL Server Agent job with three steps: full backup on Sunday, differential backup Monday–Saturday, and transaction-log backup every 15 minutes; configure a job history and review the job activity monitor after two scheduled runs. · Peer-review a classmate's backup script against a provided checklist covering: timestamped filenames, exit-code checking, log writing, retention cleanup, and commented code; give written feedback and revise your own script based on feedback received.
Materials: Linux lab workstation with cron; PostgreSQL 15+; SQL Server Developer Edition with SQL Server Agent enabled; bash reference; Microsoft SQL Server Agent documentation; peer-review checklist
Check: Instructor runs each student's cron job live, inspects the resulting log file, and confirms old-file deletion works; SQL Server Agent job history must show at least one successful run; 80% on peer-review checklist required.

Practice check8

Answer each question, then check it. Nothing is recorded; this is your own practice.

Backup and Recovery / Scripting
1. In a bash backup script, which shell variable holds the exit code of the most recently executed command?
Backup Automation / Cron Scheduling
2. A DBA wants to run a PostgreSQL backup script every day at 3:15 AM. Which crontab entry is correct?
Backup Retention / Shell Scripting
3. Which `find` command correctly deletes PostgreSQL dump files in /backup that are older than 14 days?
SQL Server Backup Strategy / Oracle DBA CA / DP-300
4. In a SQL Server Agent three-tier backup job, what is the correct purpose of the transaction-log backup step?
Security / Data Governance / CDMP
5. Why should database credentials not be stored as plain text inside a backup shell script?
SQL Server Backup Strategy / DP-300
6. A SQL Server Agent job step is set to run a full backup on Sunday and differential backups Monday through Saturday. On which day would restoring to Wednesday noon require the fewest backup sets?
Backup Automation / Data Governance / CDMP
7. When reviewing a classmate's backup script against the peer-review checklist, which item is MOST critical for proving that a backup actually completed successfully?
SQL Server Administration / DP-300 / Oracle DBA CA
8. Before relying on a SQL Server Agent job to run scheduled backups, what must the DBA confirm first?
Record this lesson on my transcript. Sign in with your key and enrollment email to record lessons (enroll free if you have no key). This page stays open to read and practise.