歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> SHELL編程

使用SHELL腳本自動備份Linux系統

前段時間寫的使用python腳本自動備份系統文件,今天來寫一個使用shell腳本增量備份系統文件,順便復習一下shell腳本相關的命令,這個腳本可以根據自己的需求來備份不同的文件或者文件夾,進行完整備份和增量備份。

使用Python自動化備份系統及網站 http://www.linuxidc.com/Linux/2013-11/92457.htm

直接上腳本如下:
#!/bin/sh
#Automatic Backup Linux System Files
#Author wugk 2013-11-22
#Define Variable
SOURCE_DIR=(
    $*
)
TARGET_DIR=/data/backup/
YEAR=`date +%Y`
MONTH=`date +%m`
DAY=`date +%d`
WEEK=`date +%u`
FILES=system_backup.tgz
CODE=$?
if
    [ -z "$*" ];then
    echo -e "Please Enter Your Backup Files or Directories\n--------------------------------------------\nExample $0 /boot /etc ......"
    exit
fi
#Determine Whether the Target Directory Exists
if
    [ ! -d $TARGET_DIR/$YEAR/$MONTH/$DAY ];then
    mkdir -p $TARGET_DIR/$YEAR/$MONTH/$DAY
    echo "This $TARGET_DIR is Created Successfully !"
fi
#EXEC Full_Backup Function Command
Full_Backup()
{
if
    [ "$WEEK" -eq "7" ];then
    rm -rf $TARGET_DIR/snapshot
    cd $TARGET_DIR/$YEAR/$MONTH/$DAY ;tar -g $TARGET_DIR/snapshot -czvf $FILES `echo ${SOURCE_DIR[@]}`
    [ "$CODE" == "0" ]&&echo -e  "--------------------------------------------\nThese Full_Backup System Files Backup Successfully !"
fi
}
#Perform incremental BACKUP Function Command
Add_Backup()
{
  cd $TARGET_DIR/$YEAR/$MONTH/$DAY ;
if
    [ -f $TARGET_DIR/$YEAR/$MONTH/$DAY/$FILES ];then
    read -p "These $FILES Already Exists, overwrite confirmation yes or no ? : " SURE
    if [ $SURE == "no" -o $SURE == "n" ];then
    sleep 1 ;exit 0
    fi
#Add_Backup Files System
    if
        [ $WEEK -ne "7" ];then
        cd $TARGET_DIR/$YEAR/$MONTH/$DAY ;tar -g $TARGET_DIR/snapshot -czvf $$_$FILES `echo ${SOURCE_DIR[@]}`
        [ "$CODE" == "0" ]&&echo -e  "-----------------------------------------\nThese Add_Backup System Files Backup Successfully !"
  fi
else
  if
      [ $WEEK -ne "7" ];then
      cd $TARGET_DIR/$YEAR/$MONTH/$DAY ;tar -g $TARGET_DIR/snapshot -czvf $FILES `echo ${SOURCE_DIR[@]}`
      [ "$CODE" == "0" ]&&echo -e  "-------------------------------------------\nThese Add_Backup System Files Backup Successfully !"
  fi
fi
}
Full_Backup;Add_Backup

以上腳本僅供參考,還有很多需要優化、完善和調整的地方,歡迎大家提出意見。

Copyright © Linux教程網 All Rights Reserved