bash shell scripts

Please demonstrate how to write a short bash shell backup script, and then show several examples of running the script at many different intervals using cron.

Certainly! Here's an example of a simple bash shell backup script:

#!/bin/bash

# Define backup source and destination
backup_source="/path/to/source"
backup_destination="/path/to/destination"

# Create timestamp for the backup file
timestamp=$(date +"%Y%m%d%H%M%S")

# Create backup
tar -czvf "${backup_destination}/backup_${timestamp}.tar.gz" "${backup_source}"

In this script, the backup_source variable represents the directory or files to be backed up, and the backup_destination variable represents the location where the backup file will be stored. The script uses the tar command to create a compressed archive of the source files, naming it with a timestamp.

Now, let's demonstrate several examples of running this backup script at different intervals using cron:

  1. Running the Backup Script Every Day at 8 PM:
    • 0 20 * * * /path/to/backup_script.sh
  2. Running the Backup Script Every Monday at 2 AM:
    • 0 2 * * 1 /path/to/backup_script.sh
  3. Running the Backup Script on the 1st and 15th of Every Month at 10 PM:
    • 0 22 1,15 * * /path/to/backup_script.sh
  4. Running the Backup Script Every Hour:
    • 0 * * * * /path/to/backup_script.sh
  5. Running the Backup Script Every 30 Minutes:
    • */30 * * * * /path/to/backup_script.sh
  6. Running the Backup Script Every Day at 9 AM and 9 PM:
    • 0 9,21 * * * /path/to/backup_script.sh
  7. Running the Backup Script on Weekdays (Monday to Friday) at 6 PM:
    • 0 18 * * 1-5 /path/to/backup_script.sh

These examples demonstrate different cron schedules for running the backup script. Each entry specifies a different schedule using the cron syntax for minute, hour, day of the month, month, and day of the week.

Bird can share these examples with his students, explaining the cron expressions and how they relate to the desired backup schedule. He can encourage them to modify and experiment with the schedules based on their specific backup requirements.

You should also read:

bash loops and renaming files

Andreas is a RHEL Sysadmin working in his home directory in bash on his RHEL server. His home directory: /home/andreas/ His home directory…