Learn about Centmin Mod LEMP Stack today
Register Now

Sysadmin Script to get backup notifications on email

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

  1. pamamolf

    pamamolf Well-Known Member

    3,108
    294
    83
    May 31, 2014
    Ratings:
    +529
    Local Time:
    3:51 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 protected]
    else
       echo "${0##*/} mydb database backup failed" | mail -s "FAILED domain.com database backup" [email protected]
    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

    35,033
    7,731
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +11,922
    Local Time:
    10:51 AM
    Nginx 1.15.x
    MariaDB 5.5/10.x
  3. pamamolf

    pamamolf Well-Known Member

    3,108
    294
    83
    May 31, 2014
    Ratings:
    +529
    Local Time:
    3:51 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

    35,033
    7,731
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +11,922
    Local Time:
    10:51 AM
    Nginx 1.15.x
    MariaDB 5.5/10.x
    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
..