![Cronjob 权限被拒绝(夜间备份 shell 脚本)](https://linux22.com/image/1132020/Cronjob%20%E6%9D%83%E9%99%90%E8%A2%AB%E6%8B%92%E7%BB%9D%EF%BC%88%E5%A4%9C%E9%97%B4%E5%A4%87%E4%BB%BD%20shell%20%E8%84%9A%E6%9C%AC%EF%BC%89.png)
我正在尝试创建一个 shell 脚本以便每晚使用 tar 进行备份。
我收到的错误是:
From shady@wserver1 Wed Dec 30 04:01:01 2015
Return-Path: <shady@wserver1>
X-Original-To: shady
Delivered-To: shady@wserver1
Received: by wserver1 (Postfix, from userid 1000)
id 23DBE15620F5; Wed, 30 Dec 2015 04:01:01 -0500 (EST)
From: root@wserver1 (Cron Daemon)
To: shady@wserver1
Subject: Cron <shady@wserver1> /home/shady /backup.sh
Content-Type: text/plain; charset=ANSI_X3.4-1968
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/shady>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=shady>
Message-Id: <20151230090101.23DBE15620F5@wserver1>
Date: Wed, 30 Dec 2015 04:01:01 -0500 (EST)
/bin/sh: 1: /home/shady: Permission denied
脚本如下:
#!/bin/sh
####################################
#
# Backup to NFS mount script.
#
####################################
# What to backup.
backup_files="/home /var/www /var/lib/mysql"
# Where to backup to.
dest="/media/backups"
# Create archive filename.
date=$(date +"%m_%d_%Y_%H_%M_%S")
hostname=$(hostname -s)
archive_file="$hostname-$date.tgz"
# Print start status message.
echo "Backing up $backup_files to $dest/$archive_file"
date
echo
# Backup the files using tar.
tar czf $dest/$archive_file $backup_files
# Print end status message.
echo
echo "Backup finished"
date
# Long listing of files in $dest to check file sizes.
ls -lh $dest
如果我使用 运行命令sudo su
,它会正常工作,但我无法使用 cron 作业来做到这一点。 的权限非常严格/var/lib/mysql
,但如果需要,我总是可以手动备份。但是,/home
和/var/www
都设置为775
,它仍然不起作用。有什么帮助吗?
答案1
可疑用户没有权限执行自动 cron 作业的 backup.sh 文件
尝试使用可疑用户手动运行它,并为 backup.sh 设置权限
chmod +x backup.sh