我试图编写的脚本需要执行以下操作
转到特定的目录,找到所有*.mp4扩展名为30分钟以上的文件,并将它们移动到一个变量中。
变量中的所有文件都需要上传到FTP中,并发出电子邮件通知。下一个if需要查看文件并找到扩展名为*1280x720_3500_h32.mp4的所有文件,这些文件随后将被上载到不同的FTP服务器。
最后,脚本需要将所有文件移动到网络上的一个单独位置进行存档。
然后,该进程需要每30分钟运行一次。这就是为什么我添加了一个单独的脚本来运行30分钟睡眠进程并调用初始脚本。实际上,我有一个上传脚本和一个睡眠脚本。因此,有两个问题浮现在脑海中:
*1280x720_3500_h32.mp4文件-它会上传所有的文件名比30分钟新的文件吗?睡眠脚本:
#!/bin/bash
ScripLocal='/Users/jonathan/scripts/Event_Scripts'
while true
do
echo "running Upload script: find_newerthan_mv_toFtp_v2.sh"
cd $ScripLocal
. find_newerthan_mv_toFtp_v2.sh
echo "finished running script: ffind_newerthan_mv_toFtp_v2.sh - next run is in 30 minutes"
sleep 30m
done 上传脚本:
#!/bin/bash
#File Origin
GSPORIGIN='/Volumes/HCHUB/Episode/Watchfolders/Output/Profile_01/'
#To location on the SAN where the files are being stored
DESTDIRSAN='/Volumes/HCHUB/Digitalmedia/online_encodes'
# 1st FTP Details
HOST='ftp.upload01.com'
USER='xxxx'
PASSWD='xxxx'
DESTDIR="/8619/_!/intlod/upload/"
#2nd FTP details
HOST01='ftp.upload02.com'
USER01='xxxx'
PASSWd01='xxxx'
cd $GSPORIGIN
for file in `find . -type f -name "*.mp4" -mmin -30`
do
echo $file
if [ -f $file ] ; then
echo "Uploading to FTP 01"
ftp -n -v $HOST << EOT
ascii
user $USER $PASSWD
prompt
cd $DESTDIR
mput $file
EOT
echo "$file has been copied to FTP 01" | mail -s "$file has been copied to FTP 01 in Directory $DESTDIR" xxxx@xxxx.com xxxx@xxxx.com xxxx@xxxx.com;
if [[ $file == *1280x720_3500_h32.mp4 ]] ; then
echo "Uploading FTP 02 version"
ftp -n -v $HOST01 << EOT
ascii
user $USER01 $PASSWD01
prompt
mput $file
EOT
echo "$file has been copied to FTP 02" | mail -s "$file has been copied to FTP 02" xxxx@xxxx.com;
fi
echo "moving files to SAN $DESTDIRSAN"
mv -v $file $DESTDIRSAN
else exit 1
fi
done发布于 2011-10-31 10:56:05
如果你担心比赛的情况,不要使用FTP。FTP是一种非常非常糟糕的数据交换协议--当发生错误时,它不会给出正确的错误消息,退出代码总是0(即使有些东西不起作用),没有防止重复上传或丢失上传的保护,等等。
使用rsync代替。rsync将开始以临时名称编写目标文件(因此,在上传完成之前,接收方不会看到它),它可以恢复上载(对于大文件很重要),它使用校验和来确保上传是正确的,如果有任何错误,它会失败并使用非空错误代码。
最重要的是,它可以使用ssh来保护连接,这样您就不必通过网络发送纯文本密码(即使在Intranet中,这也是一个非常糟糕的主意)。
rsync有强大的过滤选项(上传什么),它可以存档任何处理过的文件,这样您就不会上传两次了。它甚至可以检测用户何时第二次上传文件并失败(如果文件100%相同,则悄悄跳过)。
要确保不同时运行两个上传,请使用锁:
mkdir .lock || { echo "Can't lock - process is already running" ; exit 1 ; }
trap "rmdir .lock" EXIT您可能还想看看FTAPI,它做了所有这些和更多的事情。
发布于 2011-10-31 10:53:32
使用crontab,而不是休眠线程。sleep很容易失败,而nohupping无论如何也是个麻烦。
如果可以的话,可以使用rsync而不是自己寻找更改。而且,您不需要像使用rsync那样编写ftp脚本。
发布于 2011-11-01 11:03:50
实现了脚本的python变体(谢谢,Daniel!!)
#!/usr/bin/python
# Created in a rush, on the 31st October
# HALLOWEEN SPECIAL - QUICK AND DIRTY
import os
import shutil
from ftplib import FTP
# File Origin
ENCODE_OUTPUT='xxxxxxx'
# To location on the SAN where the files are being cached on the network
CACHE_DIR='xxxxxxxx'
#ENCODE_OUTPUT='encode/'
#CACHE_DIR='cache/'
#FTP01 FTP Details
FTP01_HOST='upload.FTP01.com'
FTP01_USER='xxxxx'
FTP01_PASSWD='xxxxxxx'
FTP01_DESTDIR="dir/dir/"
#Fingerprinting FTP details
FINGERPRINT_HOST='ftp.ftp02.com'
FINGERPRINT_USER='xxxxx'
FINGERPRINT_PASSWD='xxxxxx'
FINGERPRINT_DEST_DIR='/'
dirList = os.listdir(ENCODE_OUTPUT)
dirList.sort()
# Move the files out of the encoded located and into the cache dir
for video_file in dirList:
print "Moving file %s to %s" % (video_file, CACHE_DIR)
shutil.move(ENCODE_OUTPUT + video_file, CACHE_DIR)
# Upload the file to FTP01
print "Logging into %s FTP" % FTP01_HOST
ftp = FTP(FTP01_HOST)
ftp.login(FTP01_USER, FTP01_PASSWD)
ftp.cwd(FTP01_DESTDIR)
print "Uploading file %s to %s" % (video_file, FTP01_HOST+FTP01_DESTDIR)
ftp.storbinary('STOR ' + FTP01_DESTDIR+video_file, open(CACHE_DIR+video_file, "rb"), 1024)
# Close the connection
ftp.close
# If the file ands in "1280x720_3500_h32.mp4" copy it to the finger printing service
if video_file[-21:]=="1280x720_3500_h32.mp4":
# Upload the file to the fingerprinting service
print "File is HD. Logging into %s FTP" % FINGERPRINT_HOST
ftp = FTP(FINGERPRINT_HOST)
ftp.login(FINGERPRINT_USER, FINGERPRINT_PASSWD)
ftp.cwd(FINGERPRINT_DEST_DIR)
print "Uploading file %s to %s" % (video_file, FINGERPRINT_HOST)
ftp.storbinary('STOR ' + FINGERPRINT_DEST_DIR+video_file, open(CACHE_DIR+video_file, "rb"), 1024)
ftp.closehttps://stackoverflow.com/questions/7952635
复制相似问题