Centos 7 备份脚本 cron 问题

Centos 7 backup script cron issue

提问人:Sjors 提问时间:11/13/2023 更新时间:11/14/2023 访问量:27

问:

我找到了一个很棒的脚本来每天创建一个完整的 Centos 7 备份:

First, log in as root and create a directory where our backups will land:

mkdir /backups

Now, we will create the following shell script file and open it for editing:

mkdir ~/bin;vi ~/bin/mybackup.sh

Put in the following content, replacing /backups in the environment variable DEST and SOURCE with the one you would like to backup as well as the recipient’s EMAIL:

#!/bin/bash
SBJT="cron backup report for `hostname -s` from $(date +%Y%m%d:%T)"
FROM=root@domain
[email protected]
SOURCE=/root
DEST=/backups
LFPATH=/tmp
LF=$LFPATH/$(date +%Y%m%d_%T)_logfile.log
rsync --delete --log-file=$LF -avzq $SOURCE $DEST
(echo "$SBJT"; echo; cat $LF ) | sendmail -f $FROM -t $EMAIL

Make the script executable:

chmod a+x /root/bin/mybackup.sh

Now, open crontab using:

crontab -e

Next, create the following entry by adding the following line to the end of the document, then save and close it:

30 20 * * * /root/bin/mybackup.sh

如果我手动启动脚本,它可以正常工作,但通过 cron/root/bin/mybackup.sh

[email protected]
30 20 * * * /root/bin/mybackup.sh

它发送一封带有错误的奇怪邮件,并且不会进行备份。我不赞成,因为当我进入时,我的收件箱中会收到邮件。/root/bin/mybackup.sh: line 9: sendmail: command not foundecho "Subject: sendmail test" | sendmail -v [email protected]

cron centos7 发送邮件

评论


答:

0赞 Romeo Ninov 11/14/2023 #1

的 PATH 比普通用户受到的限制要大得多。在脚本中添加如下内容作为第二行:cron

source /path/to/johndoe/.bashrc

如果脚本由 运行。或者,如果它的运行方式是更改路径johndoeroot

评论

0赞 Sjors 11/14/2023
感谢 @Romeo Ninov 的帮助。我已经添加了第二行,但它仍然给出sendmail错误。我做错了什么吗?source /root/.bashrc/root/bin/mybackup.sh
0赞 Romeo Ninov 11/14/2023
@Sjors,你已经安装了吗?你有没有试过只是使用sendmailmail
0赞 Sjors 11/14/2023
我已经更改为并且错误消失了。我假设安装了 sendmail,因为手动备份正在工作并且还发送了测试邮件。sendmail -f $FROM -t $EMAILmail -s $FROM $EMAIL
0赞 Romeo Ninov 11/14/2023
@Sjors,也许(因为奇怪的原因)不在 PATH 中sendmail