XMPP/Jabber: fireshell[at]linux[dot]monster

OTR fingerprints: C47CFCDC D9F67D17 4C08AA1A C2500250 AB361153

Matrix/Element: [at]fireshell:matrix[dot]hostux[dot]net

IRC: fireshell on Libera Chat

OTR fingerprints: 1A66175C 7E713B1E 6D15079 87FB1952 C6866E05

  • 1 Post
  • 17 Comments
Joined 5 months ago
cake
Cake day: June 2nd, 2024

help-circle
  • Example of a Bash script that performs the following tasks

    1. Checks the availability of an important web server.
    2. Checks disk space usage.
    3. Makes a backup of the specified directories.
    4. Sends a report to the administrator’s email.

    Example script:

    #!/bin/bash
    
    # Settings
    WEB_SERVER="https://example.com"
    BACKUP_DIR="/backup"
    TARGET_DIRS="/var/www /etc"
    DISK_USAGE_THRESHOLD=90
    ADMIN_EMAIL="admin@example.com"
    DATE=$(date +"%Y-%m-%d")
    BACKUP_FILE="$BACKUP_DIR/backup-$DATE.tar.gz"
    
    # Checking web server availability
    echo "Checking web server availability..."
    if curl -s --head $WEB_SERVER | grep "200 OK" > /dev/null; then
    echo "Web server is available."
    else
    echo "Warning: Web server is unavailable!" | mail -s "Problem with web server" $ADMIN_EMAIL
    fi
    
    # Checking disk space
    echo "Checking disk space..."
    DISK_USAGE=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g')
    if [ $DISK_USAGE -gt $DISK_USAGE_THRESHOLD ]; then
    echo "Warning: Disk space usage exceeded $DISK_USAGE_THRESHOLD%!" | mail -s "Problem with disk space" $ADMIN_EMAIL
    else
    echo "There is enough disk space."
    fi
    
    # Creating backup
    echo "Creating backup..."
    tar -czf $BACKUP_FILE $TARGET_DIRS
    
    if [ $? -eq 0 ]; then
    echo "Backup created successfully: $BACKUP_FILE"
    else
    echo "Error creating backup!" | mail -s "Error creating backup" $ADMIN_EMAIL
    fi
    
    # Sending report
    echo "Sending report to $ADMIN_EMAIL..."
    REPORT="Report for $DATE\n\n"
    REPORT+="Web server status: $(curl -s --head $WEB_SERVER | head -n 1)\n"
    REPORT+="Disk space usage: $DISK_USAGE%\n"
    REPORT+="Backup location: $BACKUP_FILE\n"
    
    echo -e $REPORT | mail -s "Daily system report" $ADMIN_EMAIL
    
    echo "Done."
    

    Description:

    1. Check web server: Uses curl command to check if the site is available.
    2. Check disk space: Use df and awk to check disk usage. If the threshold (90%) is exceeded, a notification is sent.
    3. Create a backup: The tar command archives and compresses the directories specified in the TARGET_DIRS variable.
    4. Send a report: A report on all operations is sent to the administrator’s email using mail.

    How to use:

    1. Set the desired parameters, such as the web server address, directories for backup, disk usage threshold and email.
    2. Make the script executable:
    chmod +x /path/to/your/script.sh
    
    1. Add the script to cron to run on a regular basis:
    crontab -e
    

    Example to run every day at 00:00:

    0 0 * * * /path/to/your/script.sh
    

  • fireshell@lemmy.mltoLinux@lemmy.mlWhat is your linux backup strategy?
    link
    fedilink
    English
    arrow-up
    1
    ·
    edit-2
    11 hours ago

    I do it externally with this script

    #!/bin/bash
    # Sample file backup-documents.sh
    cd ${HOME}/documents
    tar -cJpf /run/media/fireshell/EAGET/mybackups/documents-$(date '+%Y-%m-%d').tar.xz .
    sync
    
    #!/bin/bash
    # Sample file restore-documents.sh
    backup_dir="/run/media/fireshell/EAGET/mybackups/"
    mkdir -p ~/documents
    last_documents="$(ls -1t ${backup_dir}/documents-*.tar.xz | head -n1)"
    cd ~/documents && \
      tar -xpf ${last_documents}