Interesting script.
You have to be really careful on using $? on piped commands as you could be getting an unexpected result. There is some really good info on bash and piped results here: http://stackoverflow.com/questions/90418/exit-shell-script-based-on-process-exit-code
One shortcut :-)
ls -l $f/*.$arg1 &> /dev/null
instead of
ls -l $f/*.$arg1 1>/dev/null 2>>/dev/null
LOL
Personally, I always use #!/ffp/bin/sh but there can be slight differences. I tend to be a bit more explicit on some things like EXT="mp4" to ensure it is a string. Doubt it matters. You could also try a shell for assigning your commands to a variable, i.e. fdate=`date +"%Y%m%d"`, or r=`$(echo $x|/ffp/bin/cut -c1-2)` Again, not sure if it matters and it may be because we are using different shells. I'll take another look later but hopefully something here will help.
I would try and stick with either numeric or string. return "$retval" does the conversion so I would quote all the tests. Another thing I always do (old habit) is (other than declaration) I bracket my variables to ensure there is no confusion by the script. example mv \"${f}/*.${EXT}\" \"$r{ule}.${EXT}\"
Here is an example of a full script (than runs daily via cron) in case you can see something else.#!/ffp/bin/sh
#
# Daily maintenance script
#
# v1.0.0 Initial implementation
FLAG="/tmp/daily.run"
if [ -f "${FLAG}" ]; then
exit
else
echo "`date`" >"${FLAG}"
fi
echo "`date` Running script [$0]"
# Global Variables
SMNT="/mnt/HD_a2"
DMNT="/mnt/HD_b2"
UMNT="/mnt/usb"
LMNT="${UMNT}/logs"
LLOG="daily_rsync.log"
TLLOG="temp.${LLOG}"
HOSTS="/etc/hosts"
hostname="`hostname`"
export cnd=`date +"%Y%m%d"`
UserPass="username=archive,password=secret"
aHost="NAS40"
if [ -f "${LMNT}/${LLOG}" ]; then
echo "`date` Clearing old log file [${LMNT}/${LLOG}]"
rm "${LMNT}/${LLOG}"
fi
# Check for FFP startup log and move if required
for _f in "${SMNT}/Nas_Prog/logs/ffp.log" "${SMNT}/ffp.log"; do
if [ -f "${_f}" ]; then
if [ -d "${LMNT}" ]; then
echo "`date` Moving [${_f}] to [${LMNT}/ffp.log]"
mv "${_f}" "${LMNT}/"
fi
fi
done
if [ "${hostname}" = "NAS40" ]; then
# For NAS40 Only
echo "`date` Rsyncing [${SMNT}] to [${DMNT}] Log:[${LMNT}/${LLOG}]"
echo "`date` Rsyncing [${SMNT}] to [${DMNT}]" >>${LMNT}/${LLOG}
/ffp/bin/rsync -av --stats --delete ${SMNT}/ ${DMNT}/ >${LMNT}/${TLLOG} 2>&1
if [ -f "${LMNT}/${TLLOG}" ]; then
echo -n "`date` "
/ffp/bin/grep "Number of files:" ${LMNT}/${TLLOG}
echo -n "`date` "
/ffp/bin/grep "Number of files transferred:" ${LMNT}/${TLLOG}
echo -n "`date` Number of files removed: "
grep -c "deleting " ${LMNT}/${TLLOG}
cat ${LMNT}/${TLLOG} >>${LMNT}/${LLOG}
rm ${LMNT}/${TLLOG}
fi
echo "`date` Rsyncing [inreto.de::dns323/fun-plug/0.5] to [/ffp/0.5] Log:[${LMNT}/${LLOG}]"
echo "`date` Rsyncing [inreto.de::dns323/fun-plug/0.5] to [/ffp/0.5]" >>${LMNT}/${LLOG}
/ffp/bin/rsync -av --stats --delete inreto.de::dns323/fun-plug/0.5/ ${SMNT}/ffp/0.5/ >${LMNT}/${TLLOG} 2>&1
if [ -f "${LMNT}/${TLLOG}" ]; then
echo -n "`date` "
/ffp/bin/grep "Number of files:" ${LMNT}/${TLLOG}
echo -n "`date` "
/ffp/bin/grep "Number of files transferred" ${LMNT}/${TLLOG}
echo -n "`date` Number of files removed: "
grep -c "deleting " ${LMNT}/${TLLOG}
cat ${LMNT}/${TLLOG} >>${LMNT}/${LLOG}
rm ${LMNT}/${TLLOG}
fi
echo "`date` Rsyncing [inreto.de::dns323/fun-plug/0.7] to [/ffp/0.7] Log:[${LMNT}/${LLOG}]"
echo "`date` Rsyncing [inreto.de::dns323/fun-plug/0.7] to [/ffp/0.7]" >>${LMNT}/${LLOG}
/ffp/bin/rsync -av --stats --delete inreto.de::dns323/fun-plug/0.7/oarm/ ${SMNT}/ffp/0.7/ >${LMNT}/${TLLOG} 2>&1
if [ -f "${LMNT}/${TLLOG}" ]; then
echo -n "`date` "
/ffp/bin/grep "Number of files:" ${LMNT}/${TLLOG}
echo -n "`date` "
/ffp/bin/grep "Number of files transferred" ${LMNT}/${TLLOG}
echo -n "`date` Number of files removed: "
grep -c "deleting " ${LMNT}/${TLLOG}
cat ${LMNT}/${TLLOG} >>${LMNT}/${LLOG}
rm ${LMNT}/${TLLOG}
fi
else
for _f in ".bootstrap" "ffp/my-packages" "ffp/scripts" "ffp/ipkg" "ffp/0.5" "ffp/0.7"; do
# Rsync DIRECTORIES
if [ -d "${SMNT}/${_f}" ]; then
echo "`date` Rsyncing [NAS40::root/mnt/HD_a2/${_f}] to [${SMNT}/${_f}] Log:[${LMNT}/${LLOG}]"
echo "`date` Rsyncing [NAS40::root/mnt/HD_a2/${_f}] to [${SMNT}/${_f}]" >>${LMNT}/${LLOG}
/ffp/bin/rsync -av --stats --delete 192.168.1.40::root/mnt/HD_a2/${_f}/ ${SMNT}/${_f}/ >${LMNT}/${TLLOG} 2>&1
if [ -f "${LMNT}/${TLLOG}" ]; then
echo -n "`date` "
/ffp/bin/grep "Number of files:" ${LMNT}/${TLLOG}
echo -n "`date` "
/ffp/bin/grep "Number of files transferred:" ${LMNT}/${TLLOG}
echo -n "`date` Number of files removed: "
grep -c "deleting " ${LMNT}/${TLLOG}
cat ${LMNT}/${TLLOG} >>${LMNT}/${LLOG}
rm ${LMNT}/${TLLOG}
fi
fi
done
for _f in "etc/*.conf" "etc/*.save" "etc/*.subr" "etc/fun_plug.*"; do
# Rsync FILES
echo "`date` Rsyncing [NAS40::root/ffp/${_f}] to [${SMNT}/ffp/${_f}] Log:[${LMNT}/${LLOG}]"
echo "`date` Rsyncing [NAS40::root/ffp/${_f}] to [${SMNT}/ffp/${_f}]" >>${LMNT}/${LLOG}
/ffp/bin/rsync -av --stats 192.168.1.40::root/ffp/${_f} ${SMNT}/ffp/etc/ >${LMNT}/${TLLOG} 2>&1
if [ -f "${LMNT}/${TLLOG}" ]; then
echo -n "`date` "
/ffp/bin/grep "Number of files:" ${LMNT}/${TLLOG}
echo -n "`date` "
/ffp/bin/grep "Number of files transferred:" ${LMNT}/${TLLOG}
echo -n "`date` Number of files removed: "
grep -c "deleting " ${LMNT}/${TLLOG}
cat ${LMNT}/${TLLOG} >>${LMNT}/${LLOG}
rm ${LMNT}/${TLLOG}
fi
done
if [ -d "${UMNT}/lighttp" ]; then
echo "`date` Rsyncing [NAS40::${UMNT}/lighttp] to [${UMNT}/lighttp] Log:[${LMNT}/${LLOG}]"
echo "`date` Rsyncing [NAS40::${UMNT}/lighttp] to [${UMNT}/lighttp]" >>${LMNT}/${LLOG}
/ffp/bin/rsync -av --delete --stats 192.168.1.40::root${UMNT}/lighttp/ ${UMNT}/lighttp/ >${LMNT}/${TLLOG} 2>&1
if [ -f "${LMNT}/${TLLOG}" ]; then
echo -n "`date` "
/ffp/bin/grep "Number of files:" ${LMNT}/${TLLOG}
echo -n "`date` "
/ffp/bin/grep "Number of files transferred:" ${LMNT}/${TLLOG}
echo -n "`date` Number of files removed: "
grep -c "deleting " ${LMNT}/${TLLOG}
cat ${LMNT}/${TLLOG} >>${LMNT}/${LLOG}
rm ${LMNT}/${TLLOG}
fi
if [ -f "${UMNT}/lighttp/html/index.html" ]; then
echo "`date` Setting hostname [${hostname}] in [${UMNT}/lighttp/html/index.html]"
/ffp/bin/sed -i "s/NAS40/${hostname}/" ${UMNT}/lighttp/html/index.html
fi
fi
fi
LOG_FILE="${LMNT}/smartctl.log"
if [ -f "${LOG_FILE}" ]; then
echo "`date` Clearing old SmartCtl log [${LOG_FILE}]"
rm "${LOG_FILE}"
fi
for _f in "/dev/sda" "/dev/sdb"; do
echo "`date` Dumping SmartCtl data for [${_f}] to [${LOG_FILE}]"
echo "`date` SmartCtrl Data for [${_f}]" >>${LOG_FILE}
/ffp/sbin/smartctl -a -i -d marvell ${_f} >>${LOG_FILE} 2>&1
done
if [ -f "${LOG_FILE}" ]; then
SCC=`/ffp/bin/grep -c "self-assessment test result: PASSED" ${LOG_FILE}`
if [ "${SCC}" != "2" ]; then
echo "`date` ************************ S.M.A.R.T. FAILURE ***********************"
echo -n "`date` S.M.A.R.T. FAILURE - ${SCC} HDD" >>${LMNT}/SMART_Err.log
if [ ${SCC} != "1" ]; then
echo -n "s" >>${LMNT}/SMART_Err.log
fi
echo -n " passed" >>${LMNT}/SMART_Err.log
cat ${LOG_FILE} | /usr/bin/mutt -s NAS40_SMART_Alert ${EEMAIL}
fi
echo "`date` SmartCtl log Diagnostic Pass count = ${SCC}"
CLCC=`/ffp/bin/grep -c "Load_Cycle_Count" ${LOG_FILE}`
if [ "$CLCC" != "0" ]; then
echo -n "`date` "
/ffp/bin/grep -m 1 "Load_Cycle_Count" ${LOG_FILE}
echo -n "`date` ">>${LMNT}/smart_LLC.log
/ffp/bin/grep -m 1 "Load_Cycle_Count" ${LOG_FILE}>>${LMNT}/smart_LLC.log
fi
if [ "$CLCC" == "2" ]; then
echo -n "`date` "
/ffp/bin/tac ${LOG_FILE}|/ffp/bin/grep -m 1 "Load_Cycle_Count"
echo -n "`date` ">>${LMNT}/smart_LLC.log
/ffp/bin/tac ${LOG_FILE}|/ffp/bin/grep -m 1 "Load_Cycle_Count">>${LMNT}/smart_LLC.log
fi
else
echo "`date` ***WARNING***: SmartCtl log [${LOG_FILE}] does not exist"
fi
LOG_FILE="${LMNT}/dmesg.log"
echo "`date` Dumping dmesg information to [${LOG_FILE}]"
echo "`date` System dmesg Information Dump" >${LOG_FILE}
/ffp/bin/dmesg -c >>${LOG_FILE}
if [ -f "${LOG_FILE}" ]; then
/ffp/bin/sed -i '/^\*\*\*\*\*/d' ${LOG_FILE}
/ffp/bin/sed -i '/^\#\#\#\#\#/d' ${LOG_FILE}
/ffp/bin/sed -i '/stand by now/d' ${LOG_FILE}
/ffp/bin/sed -i '/awake now/d' ${LOG_FILE}
/ffp/bin/sed -i '/deprecated SCSI ioctl/d' ${LOG_FILE}
/ffp/bin/sed -i '/^$/d' ${LOG_FILE}
fi
if [ -d "/ffp/start" ]; then
echo "`date` Setting permissions for [/ffp/start/*] to [666]"
/ffp/bin/chmod 777 /ffp/start/
/ffp/bin/chmod 666 /ffp/start/*
fi
for _f in "addcron" "automatic" "dnsmasq" "kickwebs" "lighttpd" "logrotate" "ntpd" "rsyncd" "telnetd" "transmission"; do
if [ -f "/ffp/start/${_f}.sh.save" ]; then
echo "`date` Copying [/ffp/start/${_f}.sh.save] to [/ffp/start/${_f}.sh]"
cp -p /ffp/start/${_f}.sh.save /ffp/start/${_f}.sh
if [ -f "/ffp/start/${_f}.sh" ]; then
echo "`date` Marking [/ffp/start/${_f}.sh] as executable"
/ffp/bin/chmod +x /ffp/start/${_f}.sh
else
echo "`date` WARNING: [/ffp/start/${_f}.sh] does not exist. The copy may have failed."
fi
fi
if [ -f "/ffp/etc/${_f}.conf.save" ]; then
echo "`date` Copying [/ffp/etc/${_f}.conf.save] to [/ffp/etc/${_f}.conf]"
cp -p /ffp/etc/${_f}.conf.save /ffp/etc/${_f}.conf
fi
done
if [ -d "${SMNT}/Archive" ]; then
echo "`date` Reseting [${SMNT}/Archive] ownership to user [Archive:504]"
/ffp/bin/chown -R archive:504 ${SMNT}/Archive/* &
if [ ! "${hostname}" = "NAS40" ]; then
echo "`date` Rsyncing [NAS40::root/mnt/HD_a2/Archive] to [${SMNT}/Archive] Log:[${LMNT}/${LLOG}]"
echo "`date` Rsyncing [NAS40::root/mnt/HD_a2/Archive] to [${SMNT}/Archive]" >>${LMNT}/${LLOG}
/ffp/bin/rsync -av --stats --delete 192.168.1.40::root/mnt/HD_a2/Archive/ ${SMNT}/Archive/ >${LMNT}/${TLLOG} 2>&1
if [ -f "${LMNT}/${TLLOG}" ]; then
echo -n "`date` "
/ffp/bin/grep "Number of files:" ${LMNT}/${TLLOG}
echo -n "`date` "
/ffp/bin/grep "Number of files transferred:" ${LMNT}/${TLLOG}
echo -n "`date` Number of files removed: "
grep -c "deleting " ${LMNT}/${TLLOG}
cat ${LMNT}/${TLLOG} >>${LMNT}/${LLOG}
rm ${LMNT}/${TLLOG}
fi
fi
fi
for _f in "${SMNT}" "${DMNT}"; do
if [ -d "${_f}/Public" ]; then
echo "`date` Setting permissions on the directory [${_f}/Public]"
/ffp/bin/chown -R nobody:501 ${_f}/Public &
/ffp/bin/chmod -R 777 ${_f}/Public &
fi
done
if [ ! -d "/ffp/packages" ]; then
if [ -d "/ffp/0.5/packages" ]; then
echo "`date` Creating symbolic link for [/ffp/packages]"
ln -s "/ffp/0.5/packages" "/ffp/packages"
fi
fi
if [ ! -d "/ffp/extra-packages" ]; then
if [ -d "/ffp/0.5/extra-packages" ]; then
echo "`date` Creating symbolic link for [/ffp/extra-packages]"
ln -s "/ffp/0.5/extra-packages" "/ffp/extra-packages"
fi
fi
if [ -f "${HOSTS}" ]; then
echo "`date` Running the HOSTS file check on [${HOSTS}]"
for _f in "40" "41" "42"; do
COUNT=`/ffp/bin/grep -c "NAS${_f}" ${HOSTS}`
if [ "${COUNT}" == "0" ]; then
echo "`date` Adding host [NAS${_f}] with IP [192.168.1.${_f}] to [${HOSTS}]"
echo "192.168.1.${_f} NAS${_f} NAS${_f}" >>${HOSTS}
fi
done
fi
for _f in "etc" "ffp" "opt"; do
echo "`date` Rsyncing [/${_f}] to [${UMNT}/backup/${_f}] Log:[${LMNT}/${LLOG}]"
echo "`date` Rsyncing [/${_f}] to [${UMNT}/backup/${_f}]" >>${LMNT}/${LLOG}
/ffp/bin/rsync -av --stats --delete /${_f}/ ${UMNT}/backup/${_f}/ >${LMNT}/${TLLOG} 2>&1
if [ -f "${LMNT}/${TLLOG}" ]; then
echo -n "`date` "
/ffp/bin/grep "Number of files:" ${LMNT}/${TLLOG}
echo -n "`date` "
/ffp/bin/grep "Number of files transferred:" ${LMNT}/${TLLOG}
echo -n "`date` Number of files removed: "
grep -c "deleting " ${LMNT}/${TLLOG}
cat ${LMNT}/${TLLOG} >>${LMNT}/${LLOG}
rm ${LMNT}/${TLLOG}
fi
done
if [ -f "/ffp/start/addcron.sh" ]; then
echo "`date` Running the addcron script [/ffp/start/addcron.sh]"
/ffp/start/addcron.sh skip
fi
if [ -d "${UMNT}/transmission" ]; then
echo "`date` Archiving the USB drive transmission files"
if [ -f "${UMNT}/archive/${hostname}_transmission.tar.gz" ]; then
rm ${UMNT}/archive/${hostname}_transmission.tar.gz
fi
/ffp/bin/tar -cz -f ${UMNT}/archive/${hostname}_transmission.tar.gz ${UMNT}/transmission >/dev/null
fi
if [ -f "/ffp/sbin/logrotate" ]; then
if [ -f "/ffp/etc/logrotate.conf" ]; then
echo "`date` Performing the log rotations [logrotate] Log:[${LMNT}/logrotate.log]"
/ffp/sbin/logrotate -v /ffp/etc/logrotate.conf -s ${LMNT}/logrotate.state >${LMNT}/logrotate.log 2>&1
fi
fi
if [ -d "${SMNT}/www_backup" ]; then
echo "`date` Rsyncing [192.168.1.2::backup] to [${SMNT}/www_backup] Log:[${LMNT}/${LLOG}]"
echo "`date` Rsyncing [192.168.1.2::backup] to [${SMNT}/www_backup]" >>${LMNT}/${LLOG}
/ffp/bin/rsync -av --stats --delete 192.168.1.2::backup ${SMNT}/www_backup/ >${LMNT}/${TLLOG} 2>&1
if [ -f "${LMNT}/${TLLOG}" ]; then
echo -n "`date` "
/ffp/bin/grep "Number of files:" ${LMNT}/${TLLOG}
echo -n "`date` "
/ffp/bin/grep "Number of files transferred:" ${LMNT}/${TLLOG}
echo -n "`date` Number of files removed: "
grep -c "deleting " ${LMNT}/${TLLOG}
cat ${LMNT}/${TLLOG} >>${LMNT}/${LLOG}
rm ${LMNT}/${TLLOG}
fi
echo "`date` Rsyncing [${SMNT}/www_backup] to [${UMNT}/www_backup] Log:[${LMNT}/${LLOG}]"
echo "`date` Rsyncing [${SMNT}/www_backup] to [${UMNT}/www_backup]" >>${LMNT}/${LLOG}
/ffp/bin/rsync -av --stats --delete ${SMNT}/www_backup/ ${UMNT}/www_backup/ >${LMNT}/${TLLOG} 2>&1
if [ -f "${LMNT}/${TLLOG}" ]; then
echo -n "`date` "
/ffp/bin/grep "Number of files:" ${LMNT}/${TLLOG}
echo -n "`date` "
/ffp/bin/grep "Number of files transferred:" ${LMNT}/${TLLOG}
echo -n "`date` Number of files removed: "
grep -c "deleting " ${LMNT}/${TLLOG}
cat ${LMNT}/${TLLOG} >>${LMNT}/${LLOG}
rm ${LMNT}/${TLLOG}
fi
fi
if [ -d "${LMNT}" ]; then
echo "`date` Archiving the USB drive log files to [${UMNT}/archive/${cnd}_${hostname}_Logs.tar.gz]"
/ffp/bin/tar -cz -f ${UMNT}/archive/${cnd}_${hostname}_Logs.tar.gz ${LMNT}/* >/dev/null 2>&1
fi
# Mount the Archive directory and copy files
if [ ! -d "/mnt/${aHost}" ]; then
echo "`date` Creating mount point [/mnt/${aHost}]"
mkdir "/mnt/${aHost}"
fi
echo "`date` Mounting [//${aHost}/archive] to [/mnt/${aHost}]"
/ffp/bin/mount -t cifs //${aHost}/archive /mnt/${aHost} -o ${UserPass}
if [ -f "/mnt/${aHost}/flag.txt" ]; then
echo "`date` Moving [${UMNT}/archive/${cnd}_${hostname}_Logs.tar.gz] to [/mnt/${aHost}/${hostname}_Logs/]"
if [ -f "${UMNT}/archive/${cnd}_${hostname}_Logs.tar.gz" ]; then
/ffp/bin/mv "${UMNT}/archive/${cnd}_${hostname}_Logs.tar.gz" /mnt/${aHost}/${hostname}_Logs/
else
echo "`date` *** Something went wrong. Could not find [${UMNT}/archive/${cnd}_${hostname}_Logs.tar.gz]"
fi
echo "`date` Copying [${UMNT}/archive/*] to [/mnt/${aHost}/${hostname}_Logs/]"
/ffp/bin/cp -uv ${UMNT}/archive/* /mnt/${aHost}/${hostname}_Logs/
else
echo "`date` *** Something went wrong with the mount. Could not find [/mnt/${aHost}/flag.txt]"
fi
if [ ! -f "/mnt/${aHost}/${hostname}_Logs/${cnd}_${hostname}_Logs.tar.gz" ]; then
echo "`date` *** Something went wrong with the move. Could not find [/mnt/${aHost}/${hostname}_Logs/${cnd}_${hostname}_Logs.tar.gz]"
fi
echo "`date` Syncing changed blocks to disk prior to unmounting the share"
sync
echo "`date` Unmounting [/mnt/${aHost}]"
/ffp/bin/umount /mnt/${aHost}
if [ -d "/mnt/${aHost}" ]; then
# Check to ensure the share was fully unmounted before deleting the mount point
if [ ! -f "/mnt/${aHost}/flag.txt" ]; then
echo "`date` Deleting mount point [/mnt/${aHost}]"
rm -fR "/mnt/${aHost}"
else
echo "`date` *** Something went wrong with the unmount. Found [/mnt/${aHost}/flag.txt]"
fi
fi
echo -n "`date` Current System Temperature "
/ffp/bin/dns323-temp
echo "`date` Disk Usage Information"
/bin/df -h
if [ -f "${FLAG}" ]; then
rm "${FLAG}"
fi
echo "`date` End of script [$0]"
#EoF