我做了一个Web服务器来显示我的页面在本地,因为位于一个连接不好的地方,所以我想要做的是下载页面内容并replace旧的,所以我让这个脚本运行在后台,但我不是非常确定,如果这将工作24/7(2米是只是为了testing,但我希望它等待6-12小时),所以, 你怎么看这个剧本? 是不安全的? 还是足够的,我在做什么? 谢谢。
#!/bin/bash a=1; while [ $a -eq 1 ] do echo "Starting..." sudo wget http://www.example.com/web.zip --output-document=/var/www/content.zip sudo unzip -o /var/www/content.zip -d /var/www/ sleep 2m done exit
更新:现在我使用这个代码︰(只是一个原型,但我假装不使用sudo)
#!/bin/bash a=1; echo "Start" while [ $a -eq 1 ] do echo "Searching flag.txt" if [ -e flag.txt ]; then echo "Flag found, and erasing it" sudo rm flag.txt if [ -e /var/www/content.zip ]; then echo "Erasing old content file" sudo rm /var/www/content.zip fi echo "Downloading new content" sudo wget ftp://user:password@xx.xx.xx.xx/content/newcontent.zip --output-document=/var/www/content.zip sudo unzip -o /var/www/content.zip -d /var/www/ echo "Erasing flag.txt from ftp" sudo ftp -nv < erase.txt sleep 5s else echo "Downloading flag.txt" sudo wget ftp://user:password@xx.xx.xx.xx/content/flag.txt sleep 5s fi echo "Waiting..." sleep 20s done exit 0
erase.txt
open xx.xx.xx.xx user user password cd content delete flag.txt bye
简单地将新版本的内容unzip
到旧的版本可能不是最好的解决方案。 如果你从你的网站删除一个文件呢? 本地副本将仍然有它。 而且,使用基于压缩的解决方案,您每次复制时都会复制每个文件,而不仅仅是已经更改的文件。
我建议你改用rsync
来同步你的网站内容。
如果您将本地文档/var/www/mysite/
设置为/var/www/mysite/
,则替代脚本可能如下所示:
#!/usr/bin/env bash logtag="`basename $0`[$$]" logger -t "$logtag" "start" # Build an array of options for rsync # declare -a ropts ropts=("-a") ropts+=(--no-perms --no-owner --no-group) ropts+=(--omit-dir-times) ropts+=("--exclude ._*") ropts+=("--exclude .DS_Store") # Determine previous version # if [ -L /var/www/mysite ]; then linkdest="$(stat -c"%N" /var/www/mysite)" linkdest="${linkdest##*\`}" ropts+=("--link-dest '${linkdest%'}'") fi now="$(date '+%Y%m%d-%H:%M:%S')" # Only refresh our copy if flag.txt exists # statuscode=$(curl --silent --output /dev/stderr --write-out "%{http_code}" http://www.example.com/flag.txt") if [ ! "$statuscode" = 200 ]; then logger -t "$logtag" "no update required" exit 0 fi if ! rsync "${ropts[@]}" user@remoteserver:/var/www/mysite/ /var/www/"$now"; then logger -t "$logtag" "rsync failed ($now)" exit 1 fi # Everything is fine, so update the symbolic link and remove the flag. # ln -sfn /var/www/mysite "$now" ssh user@remoteserver rm -f /var/www/flag.txt logger -t "$logtag" "done"
此脚本使用一些外部工具,如果它们尚未安装在您的系统上,则可能需要进行安装:
rsync
提供了很多有用的功能。 尤其是:
--link-dest
选项允许您引用以前的目录以创建“链接”到未更改的文件,以便您可以有多个副本的目录,只有未更改文件的单个副本。 为了做到这一点,无论是rsync
部分和ssh
部分,您将需要设置SSH密钥,允许您连接而不需要密码。 这并不困难,但是如果您不知道它,这是另一个问题的主题。或者用您最喜欢的搜索引擎进行简单的搜索。
你可以每5分钟从crontab运行一次:
*/5 * * * * /path/to/thisscript
如果要更频繁地运行它,请注意,您将用于每个不涉及更新的检查的“流量”是flag.txt文件的HTTP GET。
我会建议设置一个cron工作,这比一个sleep
的脚本更可靠。
简要说明:
如果您拥有/var/www/
写入权限,只需将下载内容放入个人crontab。 运行crontab -e
,粘贴这个内容,保存并退出编辑器:
17 4,16 * * * wget http://www.example.com/web.zip --output-document=/var/www/content.zip && unzip -o /var/www/content.zip -d /var/www/
或者你可以从系统的crontab运行下载。 创建文件/etc/cron.d/download-my-site
并把这个内容放入:
17 4,16 * * * <USERNAME> wget http://www.example.com/web.zip --output-document=/var/www/content.zip && unzip -o /var/www/content.zip -d /var/www/
将<USERNAME>
替换为对/var/www
具有适当权限的登录名。
或者,您可以将所有必要的命令放入单个shell脚本中,如下所示:
#!/bin/sh wget http://www.example.com/web.zip --output-document=/var/www/content.zip unzip -o /var/www/content.zip -d /var/www/
并从crontab中调用它:
17 4,16 * * * /path/to/my/downloading/script.sh
这项任务将每天运行两次:在4点17分和16点17分。 如果你愿意,你可以设定另一个时间表。
更多关于cron作业,crontabs等: