【Shell脚本(三)】应用实例


实例一创建文件夹

#/bin/bash
while :
do
    echo "please input file's name:"
    read filename
    if test -e ./$filename
    then
         echo "the file is existing!"
    else
        mkdir $filename
        echo "successful!"
        break 
    fi
Done
后来回头看的时候发现success打错了。。。。。。。。。。。其实filename指的是文件路径,默认是在当前文件夹(脚本所在文件夹)下创建。



实例二备份文件

以下这个脚本并不是笔者写的,因为本人linux水平有限,这个文件定时备份到远程主机的shell脚本是从网上搜集而来,目前只能读懂一部分,希望能促进笔者的学习水平。

#!/bin/bash
#Function:Automatically backup a given list of directories 
#or files, and can retain the N day backup files.
#Can be back to the directory specified by the remote host,
# but the need for free password login to the remote host
#This script contains two files:Executable file "backup.sh" and
# backup project file "bakfile.lst" ,two files must be in the same directory
#Author:JianJie
#Date:2016/8/26
#Usage: sh backup.sh &
#Version:v2
###############################################################
LOG_FILE=/backup/local/log/`date +"%Y-%m"`.log #Log file
LOG_DIR=/backup/local/log
DATE_FORMAT=`date +"%Y-%m-%d"`                     
BAK_FILE_LST=bakfile.lst       #Need to backup the file or directory to this file
BAK_DIR=/backup/local          #Backup directory
KEEP_DAYS=5                    #File retention days
REMOTE_HOST=""                #Split between multiple hosts with ':'
REMOTE_DIR=/backup/$HOSTNAME  #Remote host backup directory
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"  #The directory where the current script is running
FORMAT="tar.gz"  #The compression format can be:tar.xz,tar.gz,tar.bz2
###############################################################
if [ "$REMOTE_HOST" != "" ];then
  #Determine whether there is a backup directory on the remote host
  ssh -t -q -n "$REMOTE_HOST" "[ ! -d "$REMOTE_DIR" ] && mkdir -p "$REMOTE_DIR""
fi
#Determine whether log directory and log file exists
if [ ! -d $LOG_DIR ];then
  mkdir -p $LOG_DIR
  touch $LOG_FILE
else
  if [ ! -e $LOG_FILE ];then
  touch $LOG_FILE
  fi
fi
#LogStart
echo "Backup start at $(date +"%Y-%m-%d %H:%M:%S")" >> $LOG_FILE
echo "--------------------------------------------------------" >> $LOG_FILE
echo " " >> $LOG_FILE
#Determine whether the file list 'bakfile.lst' exists
if [ ! -e $CURRENT_DIR/$BAK_FILE_LST ];then
  echo "Error:Important file [$CURRENT_DIR/$BAK_FILE_LST] has been lost,now creating..." >> $LOG_FILE
  touch $CURRENT_DIR/$BAK_FILE_LST
  echo "The file [$CURRENT_DIR/$BAK_FILE_LST] has been created,but you still need to write data in it!" >> $LOG_FILE
  echo " " >> $LOG_FILE
  echo "--------------------------------------------------------" >> $LOG_FILE
  echo "Backup end at $(date +"%Y-%m-%d %H:%M:%S")" >> $LOG_FILE
  echo " " >> $LOG_FILE
  exit 3
fi
for BACK_FILE in $(cat $CURRENT_DIR/$BAK_FILE_LST)
  do  
    BASE_NAME=`echo "$BACK_FILE" | xargs basename`
    OLD_FILE=$BAK_DIR/$BASE_NAME.$(date --date="$KEEP_DAYS days ago" +"%Y-%m-%d").$FORMAT
    if [ -e "$BACK_FILE" ];then
      cd $(dirname $BACK_FILE)
 #If a backup file already exists, it will no longer be backed up
      if [ -f $BAK_DIR/$BASE_NAME.$DATE_FORMAT.$FORMAT ];then
        echo "Backup file [$BAK_DIR/$BASE_NAME.$DATE_FORMAT.$FORMAT] already exists!" >> $LOG_FILE
      else
        cp -RHpf $BASE_NAME $BAK_DIR
        cd  $BAK_DIR
        case $FORMAT in
        tar.xz)
          tar -cpJf $BASE_NAME.$DATE_FORMAT.$FORMAT $BASE_NAME ;;
        tar.gz)
          tar -cpzf $BASE_NAME.$DATE_FORMAT.$FORMAT $BASE_NAME ;;
        tar.bz2)
          tar -cpjf $BASE_NAME.$DATE_FORMAT.$FORMAT $BASE_NAME ;;
        *)
          echo "Compress FORMAT error,default compression FORMAT:tar.gz" >> $LOG_FILE
          tar -cpzf $BASE_NAME.$DATE_FORMAT.tar.gz $BASE_NAME ;;
        esac
      #Backup files back to the remote host via scp
        if [ "$REMOTE_HOST" != "" ];then
          for host in $(echo "$REMOTE_HOST" | sed 's/:/ /g')
            do
              scp -o ConnectTimeout=10 $BASE_NAME.$DATE_FORMAT.$FORMAT $host:$REMOTE_DIR >>$LOG_FILE 2>/dev/null && \
              echo "Successful backup [$BACK_FILE] to the remote host:[$host:$REMOTE_DIR]" >>$LOG_FILE || \
              echo "Backup [$BACK_FILE] to the remote host:[$host:$REMOTE_DIR] failed,please check it" >>$LOG_FILE
            done
        fi
        #Backup complete, delete the copy of the source file
        rm -rf $BASE_NAME
        echo "Backup [$BACK_FILE] done into [$BAK_DIR] successfuly!" >> $LOG_FILE
       #Existing N days old file, delete it
        if [ -f $OLD_FILE ];then
          rm -rf $OLD_FILE
          echo "[$OLD_FILE],delete old file successfuly!" >> $LOG_FILE
        fi
      fi
    else
      echo "Wrong directory or file:[$BACK_FILE],please check it" >>$LOG_FILE
    fi
  done
echo " " >> $LOG_FILE
echo "--------------------------------------------------------" >> $LOG_FILE
echo "Backup end at $(date +"%Y-%m-%d %H:%M:%S")" >> $LOG_FILE
#LogEnd
echo " " >> $LOG_FILE
exit 0


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值