Linux自动备份文件

根据日期创建目录

并根据执行次数自动创建子目录

若有异常并提示错误行号

#!/bin/bash
sourcedir=/
backupdir=/var/backup/`date +%Y%m%d`
logfile=/usr/bi/import/log_`date +%Y%m%d`.log
today=`date '+%Y-%m-%d %H:%M:%S'`
trap 'echo line:¥LINENO failed >>$logfile' ERR
echo "$today : backup start" >> $logfile

if [ -d $backupdir ]; then
  echo  "$today : $backupdir is already exsist" >> $logfile
else
  mkdir -p $backupdir
fi

echo "$today : backup parent dir $backupdir ." >> $logfile

cd $backupdir

for i in `seq 1 100`;
do
   if [ -d $i ]; then
        echo  "$today : $i is already exsist" >> $logfile
   else
        mkdir $backupdir/$i
        newbackup=$backupdir'/'$i
        break
   fi
done

echo "$today : backup dir $newbackup ." >> $logfile
cp -R /home/ftp/ftptest/* $newbackup

echo "$today : backup end" >> $logfile
echo -e "\n" >>$logfile
exit 0

相关推荐