当前位置:K88软件开发文章中心电脑基础基础应用04 → 文章内容

Linux通过shell脚本备份系统的方法

减小字体 增大字体 作者:华军  来源:不详  发布时间:2019-1-31 0:01:02

  在Linux中,备份系统的方法有很多,除了使用备份工具外,还能通过shell备份Linux系统,今天华军小编给大家展示的是Linux通过shell脚本备份系统的方法,精心挑选的内容希望大家多多支持、多多分享,喜欢就赶紧get哦!。  脚本如下:  #!/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  上面就是小编总结的Linux使用shell脚本备份系统的方法介绍了,通过上面的脚本就能自动备份系统,但脚本可能还存在不足的地方,可自行改良优化。

Linux通过shell脚本备份系统的方法