zl程序教程

您现在的位置是:首页 >  其他

当前栏目

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

2023-09-27 14:29:23 时间
 # /etc/crontab: system-wide crontab

# Unlike any other crontab you dont 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 


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

这里写图片描述


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

这里写图片描述


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


# 查找 本地备份目录下 时间为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
#追加日志到日志文件 echo "---------------------------------------------------------------------------" $LogFile

这里写图片描述


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


# 查找 本地备份目录下 时间为120分钟之内的,并且后缀为.tar的gitlab备份文件 BACKUPFILE_SEND_TO_REMOTE=$(find /data/gitlabData/backups -type f -mmin -120 -name *.tar*)
#追加日志到日志文件 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 #追加日志到日志文件 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

这里写图片描述

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


现在我们手动触发一下刚才的脚本,发现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

这里写图片描述


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

这里写图片描述


# | | .---------- 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

这里写图片描述


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

这里写图片描述

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


#追加日志到日志文件 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 #追加日志到日志文件 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

这里写图片描述


[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备份文件的脚本


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


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


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

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


#读取mailcontent内容当做邮件正文 ,附件为Log文件

cat $mailcontent | mail -s "Warning! GitLab Backup at local server Failed Report." $mailToUser -A $LogFile

这里写图片描述

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


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 users 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 USERs 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


--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 选项来,指定一个收件人列表。


#读取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命令的用法


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

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


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

这里写图片描述

完整的代码如下所示:


#追加日志到日志文件 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 #追加日志到日志文件 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

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


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

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


记一次使用因为commit提交大文件无法推送到远程库解决问题过程及git rebase使用 首先,故事(事故)的起因是这样的。 执行删除成功会返回Ref refs/heads/master was rewritten提示,如果返回unchanged则表示没有任何更改。 如果文件路径包含空
本地时间现在为:2017-11-28 11:43,查看本地代码提交的时间为:2017-11-28 10:23,确实是一个小时之前,但是在GitLab平台上显示的世界为2017-11-28 02:23。
在上一篇博客中,已经正常安装好了GitLab,然而全部界面都是纯英文的,为了照顾整个团队的英文水平,因此这篇博客的目的是将纯英文的GitLab进行汉化。
字节卷动 You will never know how excellent you are unless you impel yourself once.