easy mc backup
Posted: Mon Nov 29, 2004 10:52 am
I wrote a simple wrapper script for ps2client to easily do a complete backup of a memory card. It simply recurses through all the directories and creates an identical structure in the local directory.
Code: Select all
#!/bin/bash
#
# wrapper for ps2client to do complete MC backup
#
# USAGE: mc-backup.sh [dest] [src-mc]
# [dest] is local destination directory. Default is "mc0.YYYY-MM-DD.HHMM"
# [src-mc] is memory card to backup. Default is "mc0:"
#
# no warranty... unlikely to break anything,
# no copyright... most people can whip this up in their sleep
#
PS2C="ps2client"
DATE="date"
AWK="awk"
Fcnt=0
Dcnt=0
# make sure we have what we need
for X in $PS2C $AWK $DATE ; do
Z=`$X --version`
if [ $? -eq 127 ]; then
echo "### ERROR: '$X' not found"
exit -1
fi
done
DoDir() { # $1 mc dir to process
PS2Cmd="$PS2C dir $1"
T=`$PS2Cmd`
if [ $? -ne 0 ]; then
echo -n "ERROR: '$PS2Cmd' returned: "
$PS2Cmd
echo "PS2 probably need to be reset by running 'ps2client reset'"
exit -1
fi
IFS=$'\x0A'$'\x0D'
G=`echo "$T" | awk ' \$1 ~ /^-r/ { A=substr($0,45); print A } '`
for F in `echo "$G"` ; do
Z=`$PS2C copyfrom "$1/$F" "$F"`
if [ $? -ne 0 ]; then echo "ERROR on copy."; fi
# change to ' echo "$Z" ' to get long output
echo $Z
let Fcnt+=1
done
unset IFS
for D in `$PS2Cmd | awk ' \$1 ~ /^d/ && \$5 != "." && \$5 != ".." { print $5 } '` ; do
echo "INFO: Descending into '$D' "
Descend $D $1
let Dcnt+=1
done
}
Descend () { # $1 dir name, $2 current dir
Z=`mkdir $1 2>&1`
if [ $? -ne 0 ]; then echo "ERROR: $Z"; exit -1; fi
cd $1
DoDir "$2/$1"
cd ..
}
SRC="mc0:" # default source (mc) ($2)
DEST="$SRC."`$DATE "+%F.%k%M"` # default dest (local dir) ($1)
if [ -n "$1" ] ; then DEST=$1; fi
if [ -n "$2" ] ; then SRC=$2; fi
echo "Backing up '$SRC' to '$DEST'"
Z=`mkdir $DEST 2>&1`
if [ $? -ne 0 ]; then echo "ERROR: $Z"; exit -1; fi
cd $DEST
DoDir $SRC
echo
echo "SUMMARY: $Dcnt directories, $Fcnt files processed."