Git学习-->如何通过Shell脚本实现 监控Gitlab备份整个过程并且通过邮件通知得到备份结果?

简介: 一、背景Git学习–>如何通过Shell脚本自动定时将Gitlab备份文件复制到远程服务器? http://blog.csdn.net/ouyang_peng/article/details/77334215git学习——> Gitlab如何进行备份恢复与迁移? http://blog.

一、背景

通过前面的几篇博客,我已经实现了整个Gitlab本机备份、迁移、恢复以及备份到远程服务器的整个流程。

1、每天凌晨2点在Gitlab服务器上执行Gitlab备份功能。
2、每天凌晨3点在Gitlab服务器上执行scp命令将最新的Gitlab备份文件复制到远程的文件备份服务器。
3、每天凌晨4点在远程的文件备份服务器上检测备份文件的时间,自动删除超过30天的备份文件。

但是这三个步骤,有可能每个过程都会出现失败的情况。因此领导第二天在我将整个Gitlab备份自动化运行之后,并且三个步骤都成功运行之后,要求我增加监控Gitlab备份整个过程并且邮件提醒备份结果的功能。

二、修改Gitlab服务器上的脚本

首先 我们查看Gitlab服务器上的定时任务,使用vi /etc/crontab命令打开 /etc/crontab文件

vi /etc/crontab

这里写图片描述

 # /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

# edited by ouyang 2017-8-11 添加定时任务,每天凌晨两点,执行gitlab备份
#0  2    * * *   root    /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1  

#也可以按照如下所示的方法,定时执行 auto_backup.sh脚本,脚本内容就填写: /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1 
0 2    * * *   root    /data/gitlabData/backups/auto_backup.sh -D 1    


# edited by ouyang 2017-8-17 添加定时任务,每天凌晨三点,执行gitlab备份到远程服务器
0  3    * * *   root   /data/gitlabData/backups/auto_backup_to_remote.sh 

可以看到

1、每天凌晨2点在Gitlab服务器上执行Gitlab备份功能,对应着 /data/gitlabData/backups/auto_backup.sh 脚本
2、每天凌晨3点在Gitlab服务器上执行scp命令将最新的Gitlab备份文件复制到远程的文件备份服务器,对应着 /data/gitlabData/backups/auto_backup_to_remote.sh 脚本

2.1 修改Gitlab本机自动备份脚本

/data/gitlabData/backups/auto_backup.sh 脚本

2.1.1 原来的脚本内容

gitlab-rake gitlab:backup:create

2.1.2 增加邮件提醒后的脚本内容

#!/bin/bash

# gitlab 机房备份路径
LocalBackDir=/data/gitlabData/backups

#当前系统日期
DATE=`date +"%Y-%m-%d"`

#邮件写入的文件
mailcontent=$LocalBackDir/mail/mailcontent_$DATE

mailToUser=ouyangpeng@oaserver.dw.gdbbk.com

#Log存放路径
LogFile=$LocalBackDir/log/backup_$DATE.log

#新建日志文件
touch $LogFile

#追加日志到日志文件
echo "Gitlab auto backup at local server, start at  $(date +"%Y-%m-%d %H:%M:%S")" >  $LogFile
echo "---------------------------------------------------------------------------" >> $LogFile


#执行gitlab本地备份功能
gitlab-rake gitlab:backup:create
# $?符号显示上一条命令的返回值,如果为0则代表执行成功,其他表示失败
if [ $? -eq 0 ];then
   #追加日志到日志文件
   echo "-----------------------------------Success!----------------------------------------" >> $LogFile
   echo "Gitlab auto backup at local server, end at  $(date +"%Y-%m-%d %H:%M:%S")" >>  $LogFile

   #写Email的正文内容
   > "$mailcontent"
   echo "GitLab Backup Daily Report,backup at local server Success ! Please Check your Email and read the following log file" >> $mailcontent

   #读取mailcontent内容当做邮件正文 ,附件为Log文件
   cat $mailcontent | mail -s "Congratulation! GitLab backup at local server Success Report." $mailToUser -A $LogFile
else
   #追加日志到日志文件
   echo "-----------------------------------Failed!---------------------------------------" >> $LogFile
   echo "Gitlab auto backup at local server failed at  $(date +"%Y-%m-%d %H:%M:%S")" >>  $LogFile

   #写Email的正文内容
   > "$mailcontent"
   echo "GitLab Backup Daily Report,Backup at local server failed Failed !  Please Check your Email and read the following log file !" >> $mailcontent

   #读取mailcontent内容当做邮件正文 ,附件为Log文件
   cat $mailcontent | mail -s "Warning! GitLab Backup at local server Failed Report." $mailToUser -A $LogFile
fi

这里写图片描述

2.1.3 测试执行成功后触发邮件提醒

手动执行脚本

root@ubuntu4146:/data/gitlabData/backups# ./auto_backup.sh 

这里写图片描述

这里写图片描述

本次备份成功,因此OA收到的邮件为:

这里写图片描述

附件log为:

Gitlab auto backup at local server, start at  2017-08-18 18:57:39
---------------------------------------------------------------------------
-----------------------------------Success!----------------------------------------
Gitlab auto backup at local server, end at  2017-08-18 18:58:11

这里写图片描述

2.1.4 测试执行失败后触发邮件提醒

测试了很久,没出现过备份失败的情况,因此比较难以复现,但是脚本应该没问题。如果出现备份失败的话,我也会收到警告邮件的!

2.2 修改Gitlab自动备份到远程文件备份服务器的脚本

之前的 /data/gitlabData/backups/auto_backup_to_remote.sh 脚本,脚本内容如下

2.2.1 原来的脚本内容

#!/bin/bash

# gitlab 机房备份路径
LocalBackDir=/data/gitlabData/backups

# 远程备份服务器 gitlab备份文件存放路径
RemoteBackDir=/root/gitlabDataBackup

# 远程备份服务器 登录账户
RemoteUser=root

# 远程备份服务器 IP地址
RemoteIP=(你的远程服务器地址)请自己修改

#当前系统日期
DATE=`date +"%Y-%m-%d"`

#Log存放路径
LogFile=$LocalBackDir/log/$DATE.log

# 查找 本地备份目录下 时间为60分钟之内的,并且后缀为.tar的gitlab备份文件
BACKUPFILE_SEND_TO_REMOTE=$(find /data/gitlabData/backups -type f -mmin -60  -name '*.tar*')

#新建日志文件
touch $LogFile

#追加日志到日志文件
echo "Gitlab auto backup to remote server, start at  $(date +"%Y-%m-%d %H:%M:%S")" >>  $LogFile
echo "---------------------------------------------------------------------------" >> $LogFile

# 输出日志,打印出每次scp的文件名
echo "---------------------The file to scp to remote server is: $BACKUPFILE_SEND_TO_REMOTE-------------------------------" >> $LogFile


#备份到远程服务器
scp $BACKUPFILE_SEND_TO_REMOTE $RemoteUser@$RemoteIP:$RemoteBackDir

#追加日志到日志文件
echo "---------------------------------------------------------------------------" >> $LogFile

这里写图片描述

2.2.2 增加邮件提醒后的脚本内容

现在要对scp命令的执行结果进行判断,根据命令执行是否成功,发送不同的Email邮件通知相关负责人。

#!/bin/bash

# gitlab 机房备份路径
LocalBackDir=/data/gitlabData/backups

# 远程备份服务器 gitlab备份文件存放路径
RemoteBackDir=/root/gitlabDataBackup

# 远程备份服务器 登录账户
RemoteUser=root

# 远程机房代码备份服务器 IP地址
RemoteIP1=(你的远程服务器地址)请自己修改

# 远程备份服务器 IP地址 (办公室)
#RemoteIP2=(你的远程服务器地址2)请自己修改

#当前系统日期
DATE=`date +"%Y-%m-%d"`

#Log存放路径
LogFile=$LocalBackDir/log/$DATE.log

# 查找 本地备份目录下 时间为120分钟之内的,并且后缀为.tar的gitlab备份文件
BACKUPFILE_SEND_TO_REMOTE=$(find /data/gitlabData/backups -type f -mmin -120  -name '*.tar*')


#邮件写入的文件
mailcontent=$LocalBackDir/mail/mailcontent_$DATE

mailToUser=ouyangpeng@oaserver.dw.gdbbk.com

#新建日志文件
touch $LogFile

#追加日志到日志文件
echo "Gitlab auto backup to remote server, start at  $(date +"%Y-%m-%d %H:%M:%S")" >  $LogFile
echo "---------------------------------------------------------------------------" >> $LogFile

# 输出日志,打印出每次scp的文件名
echo "---------------------The file to scp to remote server is: $BACKUPFILE_SEND_TO_REMOTE-------------------------------" >> $LogFile

#备份到 远程备份服务器 (办公室)
#scp $BACKUPFILE_SEND_TO_REMOTE $RemoteUser@$RemoteIP2:$RemoteBackDir

#备份到 远程机房代码备份服务器
scp $BACKUPFILE_SEND_TO_REMOTE $RemoteUser@$RemoteIP1:$RemoteBackDir
# $?符号显示上一条命令的返回值,如果为0则代表执行成功,其他表示失败
if [ $? -eq 0 ];then
   #追加日志到日志文件
   echo "-----------------------------------Success!----------------------------------------" >> $LogFile
   echo "Gitlab auto backup to remote server, end at  $(date +"%Y-%m-%d %H:%M:%S")" >>  $LogFile

   #写Email的正文内容
   > "$mailcontent"
   echo "GitLab Backup Daily Report, backup to remote server Success ! Please Check your Email and read the following log file" >> $mailcontent

   #读取mailcontent内容当做邮件正文 ,附件为Log文件
   cat $mailcontent | mail -s "Congratulation! GitLab backup to remote server Success Report." $mailToUser -A $LogFile
else
   #追加日志到日志文件
   echo "-----------------------------------Failed!---------------------------------------" >> $LogFile
   echo "Gitlab auto backup to remote server failed at  $(date +"%Y-%m-%d %H:%M:%S")" >>  $LogFile

   #写Email的正文内容
   > "$mailcontent"
   echo "GitLab Backup Daily Report,Backup to remote server Failed !  Please Check your Email and read the following log file !" >> $mailcontent

   #读取mailcontent内容当做邮件正文 ,附件为Log文件
   cat $mailcontent | mail -s "Warning! GitLab Backup to remote server Failed Report." $mailToUser -A $LogFile
fi

这里写图片描述

加上最后一段判断scp命令是否正常,然后根据命令执行结果,发送不同的Email邮件到相关责任人。

2.2.3 测试执行失败后触发邮件提醒

现在我们手动触发一下刚才的脚本,发现scp命令执行失败,因为find命令没有找到匹配的要传输到远程服务器的文件,因此失败。

root@ubuntu4146:/data/gitlabData/backups# ./auto_backup_to_remote.sh 
usage: scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
           [-l limit] [-o ssh_option] [-P port] [-S program]
           [[user@]host1:]file1 ... [[user@]host2:]file2
root@ubuntu4146:/data/gitlabData/backups# 

这里写图片描述

接着我们在OA邮件收到 失败的报警邮件,如下所示:

这里写图片描述

上面的主题以及正文还有附件log文件,这样我就可以根据收到这封邮件的话就去紧急查看下为什么会失败,并进行相应的补救措施。

log附件内容为:

Gitlab auto backup to remote server, start at  2017-08-18 18:25:51
---------------------------------------------------------------------------
---------------------The file to scp to remote server is: -------------------------------
-----------------------------------Failed!---------------------------------------
Gitlab auto backup to remote server failed at  2017-08-18 18:25:51

这里写图片描述

2.2.4 测试执行成功后触发邮件提醒

root@ubuntu4146:/data/gitlabData/backups# vi auto_backup_to_remote.sh 
root@ubuntu4146:/data/gitlabData/backups# touch -t 201708181830 test2.tar
root@ubuntu4146:/data/gitlabData/backups# ./auto_backup_to_remote.sh 
test2.tar                                                                                                                                                  100%    0     0.0KB/s   00:00    
root@ubuntu4146:/data/gitlabData/backups# 

这里写图片描述

新建一个test2.tar,文件创建时间为18:30分,现在时间是18点34分,因此可以执行成功。

接着我们在OA邮件收到 成功的邮件,如下所示:

这里写图片描述

log附件内容为:

Gitlab auto backup to remote server, start at  2017-08-18 18:33:07
---------------------------------------------------------------------------
---------------------The file to scp to remote server is: /data/gitlabData/backups/test2.tar-------------------------------
-----------------------------------Success!----------------------------------------
Gitlab auto backup to remote server, end at  2017-08-18 18:33:32

这里写图片描述

三、修改远程备份服务器上的脚本

切换到远程备份服务器,查看定时任务

#编辑 /etc/crontab
vi /etc/crontab 

可以看到已经在计划中的定时任务为 每天凌晨4点,执行删除过期的Gitlab备份文件

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed


# edited by ouyang 2017-8-17 添加定时任务,每天凌晨4点,执行删除过期的Gitlab备份文件
0  4    * * *   root  /root/gitlabDataBackup/auto_remove_old_backup.sh

这里写图片描述

2.1 修改自动删除过期的Gitlab备份文件的脚本

之前的 /root/gitlabDataBackup/auto_remove_old_backup.sh 脚本内容为:

#!/bin/bash

# 远程备份服务器 gitlab备份文件存放路径
GitlabBackDir=/root/gitlabDataBackup

# 查找远程备份路径下,超过7天 且文件后缀为.tar 的 Gitlab备份文件 然后删除
find $GitlabBackDir -type f -mtime +7 -name '*.tar*' -exec rm {} \;

这里写图片描述

现在新增加邮件通知的脚本内容为:

#!/bin/bash

#当前系统日期
DATE=`date +"%Y-%m-%d"`

# 远程备份服务器 gitlab备份文件存放路径
GitlabBackDir=/root/gitlabDataBackup

#Log存放路径
LogFile=$GitlabBackDir/log/$DATE.log

#邮件写入的文件
mailcontent=$GitlabBackDir/mail/mailcontent_$DATE

mailToUser=ouyangpeng@oaserver.dw.gdbbk.com

#新建日志文件
touch $LogFiled

#追加日志到日志文件
echo "Gitlab auto clean old backupFiles , start at  $(date +"%Y-%m-%d %H:%M:%S")" >  $LogFile
echo "---------------------------------------------------------------------------" >> $LogFile



# 查找远程备份路径下,超过7天 且文件后缀为.tar 的 Gitlab备份文件 然后删除
find $GitlabBackDir -type f -mtime +7 -name '*.tar*' -exec rm {} \;

# $?符号显示上一条命令的返回值,如果为0则代表执行成功,其他表示失败
if [ $? -eq 0 ];then
   #追加日志到日志文件
   echo "-----------------------------------Success!----------------------------------------" >> $LogFile
   echo "Gitlab auto clean old backupFiles, end at  $(date +"%Y-%m-%d %H:%M:%S")" >>  $LogFile

   #写Email的正文内容
   > "$mailcontent"
   echo "GitLab Backup Daily Report, auto clean old backupFiles Success ! Please Check your Email and read the following log file" >> $mailcontent

   #读取mailcontent内容当做邮件正文 ,附件为Log文件
   cat $mailcontent | mail -s "Congratulation! GitLab clean old backupFiles Success Report." $mailToUser -A $LogFile
else
   #追加日志到日志文件
   echo "-----------------------------------Failed!---------------------------------------" >> $LogFile
   echo "Gitlab auto backup to remote server failed at  $(date +"%Y-%m-%d %H:%M:%S")" >>  $LogFile

   #写Email的正文内容
   > "$mailcontent"
   echo "GitLab Backup Daily Report,auto clean old backupFiles Failed !  Please Check your Email and read the following log file !" >> $mailcontent

   #读取mailcontent内容当做邮件正文 ,附件为Log文件
   cat $mailcontent | mail -s "Warning! GitLab clean old backupFiles Failed  Report." $mailToUser -A $LogFile
fi

这里写图片描述

2.2 手动执行自动删除过期的Gitlab备份文件的脚本触发失败后的邮件通知

[root@localhost gitlabDataBackup]# ./auto_remove_old_backup.sh 
touch: missing file operand
Try `touch --help' for more information.

这里写图片描述

手动执行该自动删除过期的Gitlab备份文件的脚本,执行失败,因为没有超过七天的文件存在。

这里写图片描述

此时收到的OA邮件如下所示:

这里写图片描述

2.3 手动执行自动删除过期的Gitlab备份文件的脚本触发成功后的邮件通知

[root@localhost gitlabDataBackup]# touch -t 201707031230 test3.tar
[root@localhost gitlabDataBackup]# touch -t 201707041230 test4.tar
[root@localhost gitlabDataBackup]# ll
total 4154376
-rw-------. 1 root root 1401958400 Aug 17 20:05 1502906429_2017_08_17_9.4.3_gitlab_backup.tar
-rw-------. 1 root root 1425950720 Aug 18 11:59 1502972105_2017_08_17_9.4.3_gitlab_backup.tar
-rw-------. 1 root root 1426145280 Aug 18 14:27 1502992830_2017_08_18_9.4.3_gitlab_backup.tar
-rwxrwxrwx. 1 root root       2108 Aug 18 19:20 auto_remove_old_backup.sh
drwxr-xr-x. 2 root root       4096 Aug 18 19:20 log
drwxr-xr-x. 2 root root       4096 Aug 18 19:20 mail
-rw-r--r--. 1 root root          0 Aug 18 15:27 test1.tar
-rw-r--r--. 1 root root          0 Aug 18 18:33 test2.tar
-rw-r--r--. 1 root root          0 Jul  3 12:30 test3.tar
-rw-r--r--. 1 root root          0 Jul  4 12:30 test4.tar
[root@localhost gitlabDataBackup]# 

先手动创建两个7月份的test3.tar,test4.tar文件用于测试。
这里写图片描述

接着手动执行自动删除过期的Gitlab备份文件的脚本

[root@localhost gitlabDataBackup]# ll
total 4154376
-rw-------. 1 root root 1401958400 Aug 17 20:05 1502906429_2017_08_17_9.4.3_gitlab_backup.tar
-rw-------. 1 root root 1425950720 Aug 18 11:59 1502972105_2017_08_17_9.4.3_gitlab_backup.tar
-rw-------. 1 root root 1426145280 Aug 18 14:27 1502992830_2017_08_18_9.4.3_gitlab_backup.tar
-rwxrwxrwx. 1 root root       2108 Aug 18 19:20 auto_remove_old_backup.sh
drwxr-xr-x. 2 root root       4096 Aug 18 19:20 log
drwxr-xr-x. 2 root root       4096 Aug 18 19:20 mail
-rw-r--r--. 1 root root          0 Aug 18 15:27 test1.tar
-rw-r--r--. 1 root root          0 Aug 18 18:33 test2.tar
-rw-r--r--. 1 root root          0 Jul  3 12:30 test3.tar
-rw-r--r--. 1 root root          0 Jul  4 12:30 test4.tar
[root@localhost gitlabDataBackup]# ./auto_remove_old_backup.sh 
touch: missing file operand
Try `touch --help' for more information.
[root@localhost gitlabDataBackup]# vi auto_remove_old_backup.sh 
[root@localhost gitlabDataBackup]# ll
total 4154376
-rw-------. 1 root root 1401958400 Aug 17 20:05 1502906429_2017_08_17_9.4.3_gitlab_backup.tar
-rw-------. 1 root root 1425950720 Aug 18 11:59 1502972105_2017_08_17_9.4.3_gitlab_backup.tar
-rw-------. 1 root root 1426145280 Aug 18 14:27 1502992830_2017_08_18_9.4.3_gitlab_backup.tar
-rwxrwxrwx. 1 root root       2108 Aug 18 19:20 auto_remove_old_backup.sh
drwxr-xr-x. 2 root root       4096 Aug 18 19:20 log
drwxr-xr-x. 2 root root       4096 Aug 18 19:20 mail
-rw-r--r--. 1 root root          0 Aug 18 15:27 test1.tar
-rw-r--r--. 1 root root          0 Aug 18 18:33 test2.tar
You have new mail in /var/spool/mail/root
[root@localhost gitlabDataBackup]# 

这里写图片描述

此时收到的OA邮件如下所示:

这里写图片描述

1、每天凌晨2点在Gitlab服务器上执行Gitlab备份功能。
2、每天凌晨3点在Gitlab服务器上执行scp命令将最新的Gitlab备份文件复制到远程的文件备份服务器。
3、每天凌晨4点在远程的文件备份服务器上检测备份文件的时间,自动删除超过30天的备份文件。

至此,Gitlab备份与远程备份以及定期清理过期备份文件的三个流程都增加了邮件提醒功能。当然如果嫌邮件收的太多,可以自己修改逻辑,当成功的时候都不发邮件提醒了,只有失败的时候才去发送邮件提醒。

验证自动化脚本

第二天查看公司的OA邮箱,可以看到

这里写图片描述

1、每天凌晨2点在Gitlab服务器上执行Gitlab备份功能。
2、每天凌晨3点在Gitlab服务器上执行scp命令将最新的Gitlab备份文件复制到远程的文件备份服务器。
3、每天凌晨4点在远程的文件备份服务器上检测备份文件的时间,自动删除超过30天的备份文件。

上面的三个步骤都成功了,发了三封邮件通知我!

这里写图片描述

这里写图片描述

这里写图片描述

Gitlab服务器 凌晨2点自动备份成功截图
这里写图片描述

Gitlab服务器凌晨3点 scp 备份文件到 远程的文件备份服务器 成功截图
这里写图片描述

而且可以看到, 远程的文件备份服务器上已经没有了超过7天的Gitlab备份文件。

新增邮件接收者

上面的几个shell脚本,我都是只发送给我自己的OA邮箱。但是领导说这些邮件也要给他们发送一份,因此我需要修改脚本来改变收件人列表,使领导也能够收到相应的邮件

之前最后发送的相关命令如下所示,只发送给我自己的OA邮箱。

#读取mailcontent内容当做邮件正文 ,附件为Log文件
cat $mailcontent | mail -s "Warning! GitLab Backup at local server Failed Report." $mailToUser -A $LogFile

这里写图片描述

Gitlab服务器是部署在Ubuntu系统,

root@ubuntu4146:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.3 LTS
Release:    14.04
Codename:   trusty

这里写图片描述

因此我们查看下 mail 的帮助文档

root@ubuntu4146:/data/gitlabData/backups# mail --help
Usage: mail [OPTION...] [address...]
  or:  mail [OPTION...] -f [OPTION...] [file]
  or:  mail [OPTION...] --file [OPTION...] [file]
  or:  mail [OPTION...] --file=file [OPTION...]
GNU mail -- process mail messages.
If -f or --file is given, mail operates on the mailbox named by the first
argument, or the user's mbox, if no argument given.

  -a, --append=HEADER: VALUE append given header to the message being sent
  -A, --attach=FILE          attach FILE
      --content-type=TYPE    set content type for subsequent --attach options
  -e, --exist                return true if mail exists
      --encoding=NAME        set encoding for subsequent --attach options
  -E, --exec=COMMAND         execute COMMAND
  -F, --byname               save messages according to sender
  -H, --headers              write a header summary and exit
  -i, --ignore               ignore interrupts
  -n, --norc                 do not read the system mailrc file
  -N, --nosum                do not display initial header summary
  -p, --print, --read        print all mail to standard output
  -q, --quit                 cause interrupts to terminate program
  -r, --return-address=ADDRESS   use address as the return address when sending
                             mail
  -s, --subject=SUBJ         send a message with the given SUBJECT
  -t, --to                   precede message by a list of addresses
  -u, --user=USER            operate on USER's mailbox

 Common options
      --config-file=FILE, --rcfile=FILE
                             load this configuration file
      --config-help          show configuration file summary
      --config-lint, --rcfile-lint
                             check configuration file syntax and exit
      --config-verbose, --rcfile-verbose
                             verbosely log parsing of the configuration files
      --no-site-config, --no-site-rcfile
                             do not load site configuration file
      --no-user-config, --no-user-rcfile
                             do not load user configuration file
      --set=PARAM=VALUE      set configuration parameter
      --show-config-options  show compilation options


 Global debugging settings
      --debug-level=LEVEL    set Mailutils debugging level
      --debug-line-info      show source info with debugging messages

  -?, --help                 give this help list
      --usage                give a short usage message
  -V, --version              print program version

Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.

Report bugs to <bug-mailutils@gnu.org>.
root@ubuntu4146:/data/gitlabData/backups# 

这里写图片描述

查看上面的mail帮助文档,我们可以使用 mail 命令的 -t 选项来,指定一个收件人列表。

mailToUser=ouyangpeng@oaserver.dw.gdbbk.com
mailToUser1=领导1邮箱
mailToUser2=领导2邮箱

#读取mailcontent内容当做邮件正文 ,附件为Log文件
cat $mailcontent | mail -s "Warning! GitLab Backup at local server Failed Report." -t $mailToUser $mailToUser1 $mailToUser2 -A $LogFile

这里写图片描述

这样发送之后,领导也能收到相应的邮件。如果出现什么问题的话,也有多个责任人知道,能够及时处理备份问题。

但是备份服务器是部署在Center OS系统上,两个不同系统的mail命令有所不同,在Center OS上的mail命令的 -t 选项不是指定一个userlist。

首先通过如下命令lsb_release -a 查看系统版本

[root@localhost gitlabDataBackup]# lsb_release -a
LSB Version:    :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID: CentOS
Description:    CentOS release 6.8 (Final)
Release:    6.8
Codename:   Final

这里写图片描述

然后使用命令 man help 查看mail命令的用法

[root@localhost gitlabDataBackup]# mail --help
mail: illegal option -- -
Usage: mail -eiIUdEFntBDNHRV~ -T FILE -u USER -h hops -r address -s SUBJECT -a FILE -q FILE -f FILE -A ACCOUNT -b USERS -c USERS -S OPTION users
[root@localhost gitlabDataBackup]# 

这里写图片描述

按照如上所示的用法,我们改写脚本

之前的发送给我一个人的脚本为

#读取mailcontent内容当做邮件正文 ,附件为Log文件
   cat $mailcontent | mail -s "Warning! GitLab clean old backupFiles Failed  Report." $mailToUser -A $LogFile

现在新增两个领导的邮箱,然后脚本改为

mailToUser=ouyangpeng@oaserver.dw.gdbbk.com
mailToUser1=领导1邮箱
mailToUser2=领导2邮箱

#读取mailcontent内容当做邮件正文 ,附件为Log文件
cat $mailcontent | mail -s "Congratulation! GitLab clean old backupFiles Success Report." -a $LogFile $mailToUser -c $mailToUser1,$mailToUser2

这里写图片描述

完整的代码如下所示:

#!/bin/bash

#当前系统日期
DATE=`date +"%Y-%m-%d"`

# 远程备份服务器 gitlab备份文件存放路径
GitlabBackDir=/root/gitlabDataBackup

#Log存放路径
LogFile=$GitlabBackDir/log/$DATE.log

#邮件写入的文件
mailcontent=$GitlabBackDir/mail/mailcontent_$DATE

mailToUser=ouyangpeng@oaserver.dw.gdbbk.com
mailToUser1=领导1邮箱
mailToUser2=领导2邮箱

#新建日志文件
touch $LogFiled

#追加日志到日志文件
echo "Gitlab auto clean old backupFiles , start at  $(date +"%Y-%m-%d %H:%M:%S")" >  $LogFile
echo "---------------------------------------------------------------------------" >> $LogFile



# 查找远程备份路径下,超过7天 且文件后缀为.tar 的 Gitlab备份文件 然后删除
find $GitlabBackDir -type f -mtime +7 -name '*.tar*' -exec rm {} \;

# $?符号显示上一条命令的返回值,如果为0则代表执行成功,其他表示失败
if [ $? -eq 0 ];then
   #追加日志到日志文件
   echo "-----------------------------------Success!----------------------------------------" >> $LogFile
   echo "Gitlab auto clean old backupFiles, end at  $(date +"%Y-%m-%d %H:%M:%S")" >>  $LogFile

   #写Email的正文内容
   > "$mailcontent"
   echo "GitLab Backup Daily Report, auto clean old backupFiles Success ! Please Check your Email and read the following log file" >> $mailcontent

   #读取mailcontent内容当做邮件正文 ,附件为Log文件
   #cat $mailcontent | mail -s "Congratulation! GitLab clean old backupFiles Success Report." -a $LogFile $mailToUser -c $mailToUser1,$mailToUser2

   #如果成功的话,只需要发送给我一个人即可
   cat $mailcontent | mail -s "Congratulation! GitLab clean old backupFiles Success Report." -a $LogFile $mailToUser
else
   #追加日志到日志文件
   echo "-----------------------------------Failed!---------------------------------------" >> $LogFile
   echo "Gitlab auto backup to remote server failed at  $(date +"%Y-%m-%d %H:%M:%S")" >>  $LogFile

   #写Email的正文内容
   > "$mailcontent"
   echo "GitLab Backup Daily Report,auto clean old backupFiles Failed !  Please Check your Email and read the following log file !" >> $mailcontent

   #读取mailcontent内容当做邮件正文 ,附件为Log文件
   cat $mailcontent | mail -s "Warning! GitLab clean old backupFiles Failed  Report." -a $LogFile $mailToUser -c $mailToUser1,$mailToUser2
fi

执行脚本后,OA收到的邮件信息为:
这里写图片描述

相关链接


这里写图片描述

作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng/article/details/77371161

如果觉得本文对您有所帮助,欢迎您扫码下图所示的支付宝微信支付二维码对本文进行随意打赏。您的支持将鼓励我继续创作!

这里写图片描述

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
3月前
|
Java 大数据 Shell
Shell基础学习---2、运算符、条件判断、流程控制(第一天学习)
大数据开发学习 Shell基础学习---2、运算符、条件判断、流程控制
37 1
|
3月前
|
监控 Linux Shell
【Linux技术专题】「夯实基本功系列」带你一同学习和实践操作Linux服务器必学的Shell指令(排查问题指令 - 下)
在线上排查问题时,查询日志、查看系统配置和分析操作系统信息是至关重要的。这些操作可以帮助我们深入了解软件和服务的兼容性,并解决潜在的问题。在本次学习中,我们将介绍并深入学习一些我在处理类似问题时常用的指令。通过掌握这些指令,你将能够更加高效地定位和解决线上问题,提高系统的稳定性和性能。让我们一同进入这个学习过程吧!
43 0
【Linux技术专题】「夯实基本功系列」带你一同学习和实践操作Linux服务器必学的Shell指令(排查问题指令 - 下)
|
27天前
|
Shell Linux 数据库
【Shell 命令集合 网络通讯 】Linux 更新邮件别名数据库 newaliases命令 使用指南
【Shell 命令集合 网络通讯 】Linux 更新邮件别名数据库 newaliases命令 使用指南
27 1
|
2月前
|
安全 Shell 网络安全
Git学习---Git快速入门、Git基础使用、Git进阶使用、Git服务器使用(IDEA集成GitHub、Gitee、GitLab)、GitHub Desktop客户端
Git学习---Git快速入门、Git基础使用、Git进阶使用、Git服务器使用(IDEA集成GitHub、Gitee、GitLab)、GitHub Desktop客户端
130 0
|
2月前
|
监控 Shell Linux
Linux如何系统的学习shell方法
Linux如何系统的学习shell方法
28 0
|
3月前
|
关系型数据库 Linux Shell
【Linux技术专题】「夯实基本功系列」带你一同学习和实践操作Linux服务器必学的Shell指令(文件处理指令-上)
在当今的数字化时代,Linux已成为服务器、云计算、物联网等众多领域的核心操作系统。对于技术从业者、开发者以及系统管理员来说,掌握Linux指令不仅是一项基本技能,更是打开专业领域大门的关键。
49 3
【Linux技术专题】「夯实基本功系列」带你一同学习和实践操作Linux服务器必学的Shell指令(文件处理指令-上)
|
3月前
|
Shell Linux Perl
Shell基础学习---3、Read读取控制台输入、函数、综合应用案例:归档文件、正则表达式入门(第二天学习)
Shell基础学习---3、Read读取控制台输入、函数、综合应用案例:归档文件、正则表达式入门
55 1
|
3月前
|
机器学习/深度学习 大数据 Shell
Shell基础学习---1、Shell概述、脚本入门、变量
大数据学习 Shell基础学习---1、Shell概述、脚本入门、变量
49 1
|
3月前
|
Shell Linux 测试技术
一个案例学习bat和shell脚本的编写
一个案例学习bat和shell脚本的编写