Discover Centmin Mod today
Register Now

Sysadmin Script to get backup notifications on email

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

  1. pamamolf

    pamamolf Premium Member Premium Member

    4,087
    428
    83
    May 31, 2014
    Ratings:
    +834
    Local Time:
    4:31 AM
    Nginx-1.25.x
    MariaDB 10.3.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

    54,909
    12,240
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,811
    Local Time:
    12:31 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
  3. pamamolf

    pamamolf Premium Member Premium Member

    4,087
    428
    83
    May 31, 2014
    Ratings:
    +834
    Local Time:
    4:31 AM
    Nginx-1.25.x
    MariaDB 10.3.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

    54,909
    12,240
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,811
    Local Time:
    12:31 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    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'