【问题标题】:Writing a bash script to push local files to Amazon EC2 and S3 - needs proper owner/permissions编写 bash 脚本将本地文件推送到 Amazon EC2 和 S3 - 需要适当的所有者/权限
【发布时间】:2013-08-12 03:22:41
【问题描述】:

我的本​​地机器上有一个 PHP 项目,具有以下文件夹结构

  assets/
  |- css/
  |- ...
  |- js/
  |- ...
  dynamic/
  |- scripts/ *contains a bunch of .php files

计划是使用s3cmdassets/ 目录推送到Amazon S3(与CloudFront CDN 一起使用),并将dynamic/ 文件直接推送到Amazon EC2 实例(网络服务器)。

我在下面使用的基本脚本:

[push.sh]

#!/bin/bash

# Source our program variables
. ./_global.sh

# Setup the build directory
echo "======== Preparing the build directory =============="
node build/r.js -o build/build.js

# Start up the rsync to push the dynamic files
echo "==== Syncing dynamic files with the EC2 instance ===="
rsync -rvzh --progress --delete --exclude-from "${BUILD_DIR}exclude-list.txt" \
    -e "ssh -i ${BUILD_DIR}mykey.pem" \
    $WWW_BUILT_DIR ubuntu@$SERVER_IP:$SERVER_DIR

# Push the static assets up to S3
echo "========== Pushing static assets to the S3  ========="
s3cmd sync --delete-removed --exclude-from "${BUILD_DIR}exclude-list.txt" \
    ${WWW_DIR}${ASSETS} s3://mybucketname/${ASSETS}

echo "All done :)"

一切正常,除了推送到 EC2 Web 服务器的文件的权限全部设置为在 ssh 命令 (ubuntu) 中配置的用户。如何将其设置为使用:www-data

【问题讨论】:

  • 也许您可以设置一个以 root 身份定期在正确目录上运行 chownchmod 的 cron 作业?
  • @Paul 是的,我想我可以,但这会留下一个窗口,其中文件存在的权限无效(在它们被推送的时间和 cron 作业运行的时间之间)可能是对生意不利。
  • 那么您需要一个存放此类事情的区域,您指定的某个目录,然后在有效时将这些内容移动或复制到生产目录。
  • @Paul 感谢您的帮助,但JTheRockers 下面的回答很好地解决了我的问题。

标签: bash amazon-web-services amazon-ec2 file-permissions chown


【解决方案1】:

一切完成后,你可以把它放到你的脚本中远程执行命令(例如chown、chmod等),

ssh -l root -i ${BUILD_DIR}mykey.pem" $SERVER_IP "chown whateveruser:whatevergroup $SERVER_DIR"

语法:

ssh -l root -i key $SERVER_IP "your command"

【讨论】:

  • 很高兴它帮助了你!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-14
  • 1970-01-01
  • 2014-12-24
  • 2016-06-05
  • 2019-04-24
  • 2020-06-02
  • 1970-01-01
相关资源
最近更新 更多