shell 中批量根据mysql中的记录copy数据
因为程序设计缺陷,将学生的原始图片视频信息放到了教师的ftp目录下,现在学生要转学,到其他学校中上学,所以,需要根据数据库中的记录,进行批量的copy动作,第一次写shell研究了好几天,记录下来,如果大家看到有可以改进的地方,留言交流
moment.cvs 中是数据库中的元数据 图片名称用,分割,像这样
teacher moment 2013-12-14 25BD252AA43D1AFE77BE22614D441543.jpg,CED0C769009FF7AAF9222367A8C9B3C.jpg,FDC7D9064A3222C8E6B4EF21092C2637.jpg
momentF 中的数据像这样
teacher/moment/2013-12-20/99D3CF40A1111DE251F3331B4BDDCCB1.jpg
数据库中数据为略所图地址,md5以后为原图
1 #! /bin/bash 2 3 basePath="/home/" #ftp根目录 4 srcTer="yuanteacher" #原教师目录-对应教师帐号 5 targetTer="xinteacher" #新教师目录-对应教师帐号 6 ####################################################################### 7 #export 8 targetFile="moment.cvs" 9 echo "开始导出元数据" 10 [ -f $targetFile ] && { rm $targetFile; echo "目标文件存在;删除"; } || { :>$targetFile; } 11 mysql -hip -uroot -ppwd --default-character-set='utf8' --database=bbts -N -e"SELECT *from table where 12 ORDER BY m.shoottime DESC" >> $targetFile 13 echo "导出完成" 14 read -p "将要对元数据进行格式,是否继续?y/n : " isY 15 if [ $isY != "y" ]; then 16 exit 1; 17 fi 18 19 #formatData 20 srcFile=$targetFile 21 outFile="momentF.src" 22 ptext='ajfnaljaouo' 23 [ ! -n $srcFile ] && { echo "输入数据源";exit 1;} 24 [ -f $outFile ] && { echo "$outFile 已存在,删除"; rm $outFile;} 25 count=0 26 echo "开始读取数据" 27 while read line 28 do 29 row=(${line//\s+/ } ) 30 user=${row[0]} 31 momentPath=${row[1]} 32 datePath=${row[2]} 33 paths=${row[3]} 34 paths=(${paths//,/ }) 35 36 for item in ${paths[@]} 37 do 38 p=${user}"/"${momentPath}"/"${datePath}"/"${item} 39 echo $p >> $outFile 40 ((count++)) 41 filetype=${item##*.} 42 if [[ "$filetype" != "mp4" && "$filetype" != "mov" ]]; then 43 44 hdp=$(echo -n ${item%.*}${ptext}|md5sum|cut -d ' ' -f1) 45 hdp=$(echo $hdp | tr '[a-z]' '[A-Z]') 46 p=${user}"/"${momentPath}"/"${datePath}"/"${hdp}"."${filetype} 47 echo $p >> $outFile 48 ((count++)) 49 fi 50 51 52 done 53 done < $srcFile 54 55 echo "发现"$count"条需要转移的数据" 56 echo "#"$count >> $outFile 57 58 read -p "将要对数据进行复制,是否继续?y/n : " isY 59 if [ $isY != "y" ]; then 60 exit 1; 61 fi 62 #cp 63 srcPath="$outFile" #数据源 64 count=0 65 [ ! -n "$srcPath" ] && { echo "请输入数据源"; exit 1;} 66 echo "开始读取文件" 67 while read line 68 do 69 tmpPath="${basePath}""${line}" #源文件 70 targetPath=$basePath${line/#$srcTer/$targetTer} #目标文件 71 targetPath=${targetPath%/*}"/" #目标路径 72 73 #不知道为什么变色了,后面不是注释 74 if [ -f "${tmpPath}" ]; then 75 [ ! -d $targetPath ] && { echo "路径不存在,创建"; mkdir -p $targetPath; chmod -R 777 $targetPath;} 76 cp $tmpPath $targetPath 77 if [ $? == 0 ]; then 78 echo "$tmpPath 复制到 $targetPath" 79 ((count++)) 80 else 81 echo "复制失败" 82 echo $tmpPath >> fail 83 fi 84 else 85 echo "$tmpPath不存在或者不是文件" 86 fi 87 done < $srcPath 88 89 echo "共成功操作$count 个文件"

浙公网安备 33010602011771号