Jamie Dobbs wrote:

A while ago (ok maybe a long time ago!) someone gave me a piece of code that would go through the files in a directory (actually having it traverse an entire directory tree would be better) and rename any files that contained "illegal" characters (ie. ones Windows/DOS doesn't like).
I have since misplaced the code and would appreciate any help that people can give me to fix these filenames that contain illegal characters (such as * $ % etc).

Hmm, this is close to what you want. I wrote it for backups to a USB disk that contains such a broken fs. It handles broken directory names too. Watch out for linewrap etc etc...


Cheers, Rex

<paste>
# cat backup.sh
#!/bin/sh
# remount the disk as /dev/sdb1 by removing then adding the usb-storage kernel module
umount /home/backup/USB
/sbin/rmmod usb-storage
/sbin/modprobe usb-storage


export TODAY=`/bin/date +%Y%m%d`
echo
echo "Backup for "$TODAY" started at "`date`
ls -l /home/backup/USB > /dev/null 2>&1
export BACKUPDIR=/home/backup/USB
export DEVICE=/dev/sdb1
echo Using USB
export DESTDIR=$BACKUPDIR/$TODAY

#lets try using autofs instead of this...
#fuser -k $DEVICE
#umount < /dev/null $DEVICE > /dev/null 2>&1 &
#mount $DEVICE $BACKUPDIR

if [ -d $DESTDIR ] ; then
        echo "Removing old backup for today at "$DESTDIR
        rm -rf $DESTDIR
fi

mkdir $DESTDIR
SIZE=$((`du -s /home/common | cut -f1` + `du -s /home/mail | cut -f1`))
AVAILABLE=`df $DEVICE | tail -1 | sed -e 's/ [ ]*/ /g' | cut -f4 -d' '`
echo backup size $SIZE available $AVAILABLE

if [ $SIZE -gt $AVAILABLE ] ; then
LASTDIR=`ls -1r $BACKUPDIR | tail -1`
if [ X$LASTDIR != "X" ]; then
echo rm -rf $BACKUPDIR/$LASTDIR
rm -rf $BACKUPDIR/$LASTDIR
echo "Running again"
exec $0 $*
else
echo "Oops can't delete "$BACKUPDIR/$LASTDIR
fi
else
# /bin/cp -r /home/common $DESTDIR
# we need to do it this way as the FAT32 target fs cannot handle some chars the filenames, look in trans.sh
# for a list of valid FAT32 filename characters
cd /home/common
echo "Copying directories across"
find . -type d -exec /root/scripts/trans.sh $DESTDIR/common {} \;
echo "Copying files across"
find . -type f -exec /root/scripts/trans2.sh $DESTDIR/common {} \;


# This fails as dos will not let a directory end in a space, and outlook will let an IMAP folder end in one.
# /bin/cp -r /home/mail $DESTDIR
echo "Saving IMAP mail"
mkdir $DESTDIR/mail
tar zcf $DESTDIR/mail/mail.tgz /home/mail
# cd /home/mail
# find . -type d -exec /root/scripts/trans.sh $DESTDIR/mail {} \;
# find . -type f -exec /root/scripts/trans2.sh $DESTDIR/mail {} \;
fi
echo "Backup for "$TODAY" finished at "`date`


# cat trans.sh
#!/bin/bash

dir=$(echo "$2" | cut -c3- | tr -c [:alnum:]"$%\'/\`\-\.,[EMAIL PROTECTED](\)\&_^\n " "#")
mkdir "$1/$dir"
#echo "Creating directory $1/$dir"


# cat trans2.sh
#!/bin/bash

dest=$(echo "$2" | cut -c3- | tr -c [:alnum:]"$%\'/\`\-\.,[EMAIL PROTECTED](\)\&_^\n " "#")
cp "$2" "$1/$dest"
#echo cp "$2" "$1/$dest"

Reply via email to