通过 cronjob 触发时,通过 CURL 上传 S3 失败

S3 Upload via CURL fails when triggered via cronjob

我正在使用脚本 dump-to-s3.sh 将数据库转储放入 S3 存储桶中。 当手动触发时,它工作得很好,但是当我通过这个 cron(作为 root crontab)触发它时,它失败并显示以下错误消息:

crontab

31 12 * * * /home/dokku/.mongodb/dump-to-s3.sh

来自 CURL 的错误

<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>SignatureDoesNotMatch</Code>
   <Message>The request signature we calculated does not match the signature you provided. 
   Check your key and signing method.</Message>...

转储到-s3.sh

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
#cd to dump-folder
cd /dump/folder
file="mydump.tar.gz"

bucket="mybucket"
resource="/${bucket}/dumps/${file}"
contentType="application/x-compressed-tar"
dateValue=`date -R`
stringToSign="PUT\n\n${contentType}\n${dateValue}\n${resource}"
s3Key="xxxxxxxxxxxxxxxx"
s3Secret="xxxxxxxxxxxxxxxx"
signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${s3Secret} -binary | base64`

curl -X PUT -T "${file}" \
  -H "Host: ${bucket}.s3.amazonaws.com" \
  -H "Date: ${dateValue}" \
  -H "Content-Type: ${contentType}" \
  -H "Authorization: AWS ${s3Key}:${signature}" \
  https://${bucket}.s3.amazonaws.com/dumps/${file}

我认为您可能需要手动 运行 bash,例如

/bin/bash /home/dokku/.mongodb/dump-to-s3.sh

echo-en 选项在常规 shell 中不起作用;它可能只是一个 bash 扩展。

sh中:

$ sh
sh-3.2$ echo -en foo
-en foo
sh-3.2$

bash中:

$ bash
bash-3.2$ echo -en foo
foobash-3.2$