Skip to content

Latest commit

 

History

History
39 lines (32 loc) · 1.03 KB

File metadata and controls

39 lines (32 loc) · 1.03 KB

Day 7: Shell Scripting

Task: Write an automation script

  1. Create a script (backup.sh) that:

    • Takes a directory as an argument
    • Creates a timestamped backup in /tmp
  2. Example script:

    Click to view Hint !!
    #!/bin/bash
    backup_dir=$1
    timestamp=$(date +%Y%m%d_%H%M%S)
    backup_file="/tmp/backup_$timestamp.tar.gz"
    
    if [[ ! -d "$backup_dir" ]]; then
        printf "Error: Directory does not exist\n" >&2
        exit 1
    fi
    
    tar -czf "$backup_file" "$backup_dir"
    printf "Backup created at %s\n" "$backup_file"
  3. Make it executable and run it:

    Click to view Hint !!
    chmod +x backup.sh
    ./backup.sh ~/Documents
  4. Bonus: Schedule it in a cron job to run every night at 2 AM.

Tip

Use Hint section whenever stuck, google the command and go through its usage and functionality.