Exercise

Backing Up a Laptop with Anacron Exercise

Task

Back up /etc about once a day on a machine that is closed at night. Treat workstation as a laptop: a crontab entry at 10 p.m. would never fire, because nothing is awake to run it. Hand the job to anacron instead, then prove it catches up on a backup it missed.

Host
workstation
Back up
/etc
Destination
a timestamped directory under /tmp
Runs as
root
Schedule
about once a day
  1. Write the daily backup script

    Connect to the machine and become root, because anacron's directories are system-wide.

    ssh workstation
    sudo -i

    Write the script into /etc/cron.daily/. Press i and type it, then Esc and :wq.

    vim /etc/cron.daily/etc_backup.sh
    #!/bin/bash
    cp -r /etc /tmp/$(hostname)_etc_backup_$(date --iso-8601=minutes)

    Make it executable and confirm the mode. A script in these directories that is not executable is skipped without a word.

    chmod a+x /etc/cron.daily/etc_backup.sh
    ls -l /etc/cron.daily/etc_backup.sh
  2. Ask anacron to run what is due

    Anacron records the date it last ran each group of jobs. Read that date, then ask it to run anything overdue.

    cat /var/spool/anacron/cron.daily
    anacron -n
    ls -ld /tmp/workstation.lab.example.com_etc_backup_*

    No snapshot. Today's daily jobs are not overdue, so anacron had nothing to do.

  3. Pretend the laptop has been closed since January

    Backdate the record so the daily jobs are months overdue, then ask again.

    echo 20260101 > /var/spool/anacron/cron.daily
    anacron -n
  4. Confirm the backup ran

    Look for the copied directory and its passwd file, then read the date back.

    ls -ld /tmp/workstation.lab.example.com_etc_backup_*
    ls -l /tmp/workstation.lab.example.com_etc_backup_*/passwd
    cat /var/spool/anacron/cron.daily

    The backup ran and the recorded date caught up to today, so the job is no longer overdue. The run was missed, and anacron noticed.

  5. Clean up

    Remove the job and the directories it produced, then leave the root shell.

    rm -f /etc/cron.daily/etc_backup.sh
    rm -rf /tmp/workstation.lab.example.com_etc_backup_*
    exit