Welcome to Centmin Mod Community
Become a Member

Sysadmin Script to get backup notifications on email

Discussion in 'System Administration' started by pamamolf, Nov 15, 2016.

  1. pamamolf

    pamamolf Well-Known Member

    2,721
    242
    63
    May 31, 2014
    Ratings:
    +433
    Local Time:
    6:37 AM
    Nginx-1.13.x
    MariaDB 10.1.x
    Hello :)

    At the moment i am using this bash code (notify.sh) to get an email notification for backup status:

    Code:
    if /usr/bin/mysqldump bla bla bla /database.sql
    then
       echo "${0##*/} mydb database backup worked" | mail -s "WORKED domain.com database backup" email@mymail.com
    else
       echo "${0##*/} mydb database backup failed" | mail -s "FAILED domain.com database backup" email@mymail.com
    fi
    It works but i know that is not the best possible way to do it :(

    So i want to ask if anyone has any other ways to do that or any optimizations for the above code :)

    Also it will be great if it is possible to get the exact output error if the backup command fails on my email.....

    Thank you
     
    Last edited: Nov 15, 2016
  2. eva2000

    eva2000 Administrator Staff Member

    30,157
    6,785
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,136
    Local Time:
    1:37 PM
    Nginx 1.13.x
    MariaDB 5.5
  3. pamamolf

    pamamolf Well-Known Member

    2,721
    242
    63
    May 31, 2014
    Ratings:
    +433
    Local Time:
    6:37 AM
    Nginx-1.13.x
    MariaDB 10.1.x
    Ok got it but there is no info there on how to get the backup command exact output error and mail it to my email..... :(

    I am using a cron for this script....
     
  4. eva2000

    eva2000 Administrator Staff Member

    30,157
    6,785
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,136
    Local Time:
    1:37 PM
    Nginx 1.13.x
    MariaDB 5.5
    read up on shell/bash and piping standard input/output, tee and mail commands

    give you a hint using tee
    Code (Text):
    whoami | tee whoami.log
    root
    

    Code (Text):
    cat whoami.log
    root
    

    Code (Text):
    tee --help
    Usage: tee [OPTION]... [FILE]...
    Copy standard input to each FILE, and also to standard output.
    
      -a, --append              append to the given FILEs, do not overwrite
      -i, --ignore-interrupts   ignore interrupt signals
          --help     display this help and exit
          --version  output version information and exit
    
    If a FILE is -, copy again to standard output.
    
    GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
    For complete documentation, run: info coreutils 'tee invocation'
     
    • Informative Informative x 1