一个备份脚本
时间:2006-02-19 来源:cnscn2008
#!/bin/bash
#
#Filename: CnsBackup.sh
#Author : cnscn
#Time : 2006-02-19 20:00
#Email : [email protected]
#
#Purpos : to tar and zip a file or dir and make the name of
# current time, and then move it to the right directory
#
#Shell : bash,bsh,sh
#
#Usage : Usage: CnsBackup.sh <-n prefix_of_newname> <-f f1 f2 f3 f4 ...> [-m middir] [-d aimdir] [-vh]
#
#Note : the file name must give the absoulte path
#the usage function
function Usage()
{
echo " Usage: CnsBackup.sh [-i] <-n prefix_of_tarfilename> <-f f1 f2 f3 f4 ...> [-d aimdir] [-vh] "
echo
echo " Note: the file name must give the absoulte path "
echo
echo " Arguments:"
echo " [-i] tar in the file's directory"
echo " <-n prefix_of_tarfilename> the new tar file's prefix flag"
echo " <-f filename> the file name to be tared"
echo " [-d aimdir] the directory to save the file tar file"
echo " [-m middir] the middle dir the tar the file"
echo " [-v] show the version"
echo " [-h] show the help"
echo
echo " Eaxmples:"
echo " CnsBackup.sh -i -n fileflag -f /xxx/xxx/file tar in current dir, and save in current dir"
echo " CnsBackup.sh -i -n fileflag -f /xxx/xxx/file -d /tmp tar in current dir, and save in aim dir"
echo " CnsBackup.sh -n fileflag -f /xxx/xxx/file -d /tmp tar in aim dir, and save in aim dir"
echo " CnsBackup.sh -n fileflag -f /xxx/xxx/file -d /tmp -m /backup/ tar in middle dir, and save in aim dir"
echo
}
isTarInFilesDir='n'
#get the options
while getopts hvif:m:d:n: OPTION
do
case $OPTION
in
v)
echo "CnsBackup.sh Version 1.0"
;;
i)
#if tar in the file's directory
isTarInFilesDir="y"
;;
f)
f="$OPTARG"
;;
m)
#the middle dir
middir="$OPTARG"
;;
d)
#the aim directory
faimdir="$OPTARG"
;;
n)
#the new name
tarname="$OPTARG"
;;
?)
Usage
exit
;;
esac
done
if [ -z "$f" ] || [ -z "$tarname" ] ; then
Usage
exit
fi
#get the time
time=$(date +%Y%m%d%H%M%S)
#get the file's directory
fbasedir=$( dirname $f )
#save in aim directory
if [ -n "$faimdir" ] && [ "." != "$faimdir" ] && [ "./" != "$faimdir" ]
then
if [ "$fbasedir" == "$faimdir" ]
then
issamedir='Y'
else
issamedir='N'
fi
#the aim dir and the file's dir are not same
if [ "$issamedir" == 'N' ] && [ "$isTarInFilesDir" != 'y' ]
then
if [ -n "$middir" ]
then
cp -fr $f ${middir}/
cmdflag=$?
if [ 0 -ne "$cmdflag" ]
then
echo "Copy to the aim directory false, please check the directory: $middir does exist..."
exit 1
fi
cd ${middir}
else
cp -fr $f ${faimdir}/
cmdflag=$?
if [ 0 -ne "$cmdflag" ]
then
echo "Copy to the aim directory false, please check the directory: $faimdir does exist..."
exit 1
fi
cd ${faimdir}
fi
else
cd $fbasedir
fi
tar -cjvf $tarname"_"$time".tar.bz2" $f 2>/dev/null
if [ $? -eq 0 ]
then
echo "Tar ok..."
if [ -n "$faimdir" ]
then
if [ -z "$middir" ] || [ -n "$middir" ] && [ "$middir" != "$faimdir" ]
then
mv $tarname"_"$time".tar.bz2" ${faimdir}/
if [ $? -ne 0 ]
then
echo "mv false..."
exit 1
fi
fi
fi
else
echo "Tar false..."
fi
if [ "$isTarInFilesDir" != 'y' ]
then
rm -fr ${f}
if [ $? -ne 0 ]
then
echo "rm the tmp file false...."
exit 1
fi
fi
else
cd $fbasedir
tar -cjvf $tarname"_"$time".tar.bz2" $f #2>/dev/null
if [ $? -eq 0 ]
then
echo "Tar ok..."
else
echo "Tar false..."
fi
fi
#
#Filename: CnsBackup.sh
#Author : cnscn
#Time : 2006-02-19 20:00
#Email : [email protected]
#
#Purpos : to tar and zip a file or dir and make the name of
# current time, and then move it to the right directory
#
#Shell : bash,bsh,sh
#
#Usage : Usage: CnsBackup.sh <-n prefix_of_newname> <-f f1 f2 f3 f4 ...> [-m middir] [-d aimdir] [-vh]
#
#Note : the file name must give the absoulte path
#the usage function
function Usage()
{
echo " Usage: CnsBackup.sh [-i] <-n prefix_of_tarfilename> <-f f1 f2 f3 f4 ...> [-d aimdir] [-vh] "
echo
echo " Note: the file name must give the absoulte path "
echo
echo " Arguments:"
echo " [-i] tar in the file's directory"
echo " <-n prefix_of_tarfilename> the new tar file's prefix flag"
echo " <-f filename> the file name to be tared"
echo " [-d aimdir] the directory to save the file tar file"
echo " [-m middir] the middle dir the tar the file"
echo " [-v] show the version"
echo " [-h] show the help"
echo
echo " Eaxmples:"
echo " CnsBackup.sh -i -n fileflag -f /xxx/xxx/file tar in current dir, and save in current dir"
echo " CnsBackup.sh -i -n fileflag -f /xxx/xxx/file -d /tmp tar in current dir, and save in aim dir"
echo " CnsBackup.sh -n fileflag -f /xxx/xxx/file -d /tmp tar in aim dir, and save in aim dir"
echo " CnsBackup.sh -n fileflag -f /xxx/xxx/file -d /tmp -m /backup/ tar in middle dir, and save in aim dir"
echo
}
isTarInFilesDir='n'
#get the options
while getopts hvif:m:d:n: OPTION
do
case $OPTION
in
v)
echo "CnsBackup.sh Version 1.0"
;;
i)
#if tar in the file's directory
isTarInFilesDir="y"
;;
f)
f="$OPTARG"
;;
m)
#the middle dir
middir="$OPTARG"
;;
d)
#the aim directory
faimdir="$OPTARG"
;;
n)
#the new name
tarname="$OPTARG"
;;
?)
Usage
exit
;;
esac
done
if [ -z "$f" ] || [ -z "$tarname" ] ; then
Usage
exit
fi
#get the time
time=$(date +%Y%m%d%H%M%S)
#get the file's directory
fbasedir=$( dirname $f )
#save in aim directory
if [ -n "$faimdir" ] && [ "." != "$faimdir" ] && [ "./" != "$faimdir" ]
then
if [ "$fbasedir" == "$faimdir" ]
then
issamedir='Y'
else
issamedir='N'
fi
#the aim dir and the file's dir are not same
if [ "$issamedir" == 'N' ] && [ "$isTarInFilesDir" != 'y' ]
then
if [ -n "$middir" ]
then
cp -fr $f ${middir}/
cmdflag=$?
if [ 0 -ne "$cmdflag" ]
then
echo "Copy to the aim directory false, please check the directory: $middir does exist..."
exit 1
fi
cd ${middir}
else
cp -fr $f ${faimdir}/
cmdflag=$?
if [ 0 -ne "$cmdflag" ]
then
echo "Copy to the aim directory false, please check the directory: $faimdir does exist..."
exit 1
fi
cd ${faimdir}
fi
else
cd $fbasedir
fi
tar -cjvf $tarname"_"$time".tar.bz2" $f 2>/dev/null
if [ $? -eq 0 ]
then
echo "Tar ok..."
if [ -n "$faimdir" ]
then
if [ -z "$middir" ] || [ -n "$middir" ] && [ "$middir" != "$faimdir" ]
then
mv $tarname"_"$time".tar.bz2" ${faimdir}/
if [ $? -ne 0 ]
then
echo "mv false..."
exit 1
fi
fi
fi
else
echo "Tar false..."
fi
if [ "$isTarInFilesDir" != 'y' ]
then
rm -fr ${f}
if [ $? -ne 0 ]
then
echo "rm the tmp file false...."
exit 1
fi
fi
else
cd $fbasedir
tar -cjvf $tarname"_"$time".tar.bz2" $f #2>/dev/null
if [ $? -eq 0 ]
then
echo "Tar ok..."
else
echo "Tar false..."
fi
fi
相关阅读 更多 +