我有一个用于备份一组文件夹/文件的 bash 脚本。该脚本循环通过预先配置的文件来确定如何复制文件夹/文件。
我面临的问题是脚本在处理第一个“文件”条目(第二列值)后退出 while 循环。
这是脚本。
#!/bin/bash
sudo crontab -l > /home/ipaccess/crontab.txt
DATE=`date +%m-%d-%Y`
TARGETDIR="/exports/backup/Web-Server/$DATE/"
BACKUPLIST="/home/ipaccess/backup/backuplist"
ssh 172.29.32.246 mkdir -p $TARGETDIR
scp -r /home/ipaccess/crontab.txt 172.29.32.246:$TARGETDIR
cat $BACKUPLIST
while read dir type args
do
echo "Processing [$dir $type $args]"
echo "Directory: $dir"
echo "Element type: $type"
if [ $type = "folder" ] ; then
echo "Source: $args"
echo "Transfering $args to 172.29.32.246:$TARGETDIR$dir"
scp -r $args 172.29.32.246:$TARGETDIR
fi
if [ $type = "file" ] ; then
# Create folders first
echo "Creating $TARGETDIR$dir"
ssh 172.29.32.246 mkdir -p $TARGETDIR$dir
scp $args 172.29.32.246:$TARGETDIR$dir
fi
echo "Finished processing $dir $type $args"
echo "Next entry.."
done<$BACKUPLIST
echo "Backup complete."
这是预配置的文件(备份列表)
html/GS folder /var/www/html/GS/css
html/GS folder /var/www/html/GS/images
html/GS file /var/www/html/GS/*.html
html/GS file /var/www/html/GS/*.inc
html/GS file /var/www/html/GS/*.png
html/GS file /var/www/html/GS/*.mysql
html/GS file /var/www/html/GS/ac-db
html/HC file /var/www/html/HC/*.php
html/HC file /var/www/html/HC/*.html
html/HC file /var/www/html/HC/images
html/HC file /var/www/html/HC/PM/*.php
html/HC/PM file /var/www/html/HC/PM/jsapi
html/HourlyKpi file /var/www/html/HourlyKpi/*.php
html/KPI folder /var/www/html/KPI
html/KpiQuery folder /var/www/html/KpiQuery
html/wordpress/ folder /var/www/html/wordpress
usr/local/apache2/conf file /usr/local/apache2/conf/httpd.conf
cgi-bin folder /var/www/cgi-bin
html/GS file /var/www/html/GS/*.php
这是脚本的输出
Processing [html/GS file /var/www/html/GS/*.html]
+ echo 'Directory: html/GS'
Directory: html/GS
+ echo 'Element type: file'
Element type: file
+ '[' file = folder ']'
+ '[' file = file ']'
+ echo 'Creating /exports/backup/Web-Server/04-18-2013/html/GS'
Creating /exports/backup/Web-Server/04-18-2013/html/GS
+ ssh 172.29.32.246 mkdir -p /exports/backup/Web-Server/04-18-2013/html/GS
+ scp /var/www/html/GS/gs.html /var/www/html/GS/login_header.html /var/www/html/GS/nologin_header.html /var/www/html/GS/overlaynal.html /var/www/html/GS/tab-test.html 172.29.32.246:/exports/backup/Web-Server/04-18-2013/html/GS
gs.html 100% 3204 3.1KB/s 00:0
login_header.html 100% 418 0.4KB/s 00:0
nologin_header.html 100% 464 0.5KB/s 00:0
overlay-external.html 100% 2308 2.3KB/s 00:0
tab-test.html 100% 1573 1.5KB/s 00:0
+ echo 'Finished processing html/GS file /var/www/html/GS/*.html'
Finished processing html/GS file /var/www/html/GS/*.html
+ echo 'Next entry..'
Next entry..
+ read dir type args
+ echo 'Backup complete.'
Backup complete.