捕获并邮寄 bash 脚本错误

发布于 2024-11-25 12:58:55 字数 1156 浏览 0 评论 0原文

我有一个脚本,每晚在 cron 中运行,为网络上的多个主机备份一些 postgres 数据库。我有一种方法可以通过利用退出状态来收到脚本失败的警报,但它没有告诉我失败的原因。

根据以下代码,我如何捕获脚本运行时发生的任何错误,并将其通过电子邮件发送给我,以便我可以更好地了解发生的情况。

FILEDATE=`date '+%Y-%m-%d'`
BASEDIR=/u1/$1/db_dumps
PGDUMP=/path/to/pg_dump
HOST=$1
DB=$2

if [ $DB == all ]
then 
    for ALLDUMPS in db1 db2 db3
        do
        ssh root@$HOST "env PGUSER=pguser PGPASSWORD=pgpassword $PGDUMP -Fc $ALLDUMPS" | pbzip2 > $BASEDIR/$FILEDATE-$HOST-$ALLDUMPS.dump.bz2
            if [ $? -ne 0 ]
                then mutt -s "dbdumper could not create a backup of $ALLDUMP from $HOST" [email protected] < /dev/null
            fi
        done

else 
    ssh root@$HOST "env PGUSER=pguser PGPASSWORD=pgpassword $PGDUMP -Fc $DB" | pbzip2 > $BASEDIR/$FILEDATE-$HOST-$DB.dump.bz2
if [ $? -ne 0 ]
    then mutt -s "dbdumper failed to create a backup of $DB from $HOST" [email protected] < /dev/null
fi
fi

I have a script that I run nightly in cron to backup some postgres databases for several hosts on my network. I have a way of getting alerted that the script fails by leveraging the exit status, but it doesn't tell me WHY it failed.

Based off the following code, how can I capture any errors that occur when the script is run, and email them to me so I can have a better understanding of what happened.

FILEDATE=`date '+%Y-%m-%d'`
BASEDIR=/u1/$1/db_dumps
PGDUMP=/path/to/pg_dump
HOST=$1
DB=$2

if [ $DB == all ]
then 
    for ALLDUMPS in db1 db2 db3
        do
        ssh root@$HOST "env PGUSER=pguser PGPASSWORD=pgpassword $PGDUMP -Fc $ALLDUMPS" | pbzip2 > $BASEDIR/$FILEDATE-$HOST-$ALLDUMPS.dump.bz2
            if [ $? -ne 0 ]
                then mutt -s "dbdumper could not create a backup of $ALLDUMP from $HOST" [email protected] < /dev/null
            fi
        done

else 
    ssh root@$HOST "env PGUSER=pguser PGPASSWORD=pgpassword $PGDUMP -Fc $DB" | pbzip2 > $BASEDIR/$FILEDATE-$HOST-$DB.dump.bz2
if [ $? -ne 0 ]
    then mutt -s "dbdumper failed to create a backup of $DB from $HOST" [email protected] < /dev/null
fi
fi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枯叶蝶 2024-12-02 12:58:55

从 ssh 命令捕获 stderr,并将其通过电子邮件发送给自己。

stderr=$( ssh root@$HOST "env PGUSER=user PGPASSWORD=pw $PGDUMP -Fc $ALLDUMPS" | 
          pbzip2 2>&1 > "$BASEDIR/$FILEDATE-$HOST-$ALLDUMPS.dump.bz2" )
if [ $? -ne 0 ]; then
   subj="dbdumper could not create a backup of $ALLDUMP from $HOST"
   mutt -s "$subj" [email protected] <<< "$stderr"
fi

Capture stderr from the ssh command, and email that to yourself.

stderr=$( ssh root@$HOST "env PGUSER=user PGPASSWORD=pw $PGDUMP -Fc $ALLDUMPS" | 
          pbzip2 2>&1 > "$BASEDIR/$FILEDATE-$HOST-$ALLDUMPS.dump.bz2" )
if [ $? -ne 0 ]; then
   subj="dbdumper could not create a backup of $ALLDUMP from $HOST"
   mutt -s "$subj" [email protected] <<< "$stderr"
fi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文