玩蛇网提供最新Python编程技术信息以及Python资源下载!
您现在的位置: 玩蛇网首页 > linux入门教程 > 正文内容

Linux系统自动备份shell脚本示例

玩蛇网推荐图文教程:python 列表

如何自动完成Linux系统的备份操作?看完Linux系统自动备份shell脚本示例就会知道啦。

在之前的一段时间里,我写的使用python脚本自动备份系统文件,今天来写一个使用shell脚本增量备份系统文件,顺便复习一下shell脚本相关的命令,这个脚本可以根据自己的需求来备份不同的文件或者文件夹,进行完整备份和增量备份的操作。

使用Python自动化备份系统及网站操作如下。

直接上脚本代码更直观,代码如下:
#!/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

文章中介绍的脚本使用方法仅供参考,还有很多需要优化、完善和调整的地方,欢迎大家交流共进步。

玩蛇网文章,转载请注明出处和文章网址:http://www.iplaypy.com/linux/l6704.html

相关文章 Recommend

玩蛇网Python互助QQ群,欢迎加入-->: 106381465 玩蛇网Python新手群
出炉日期:2017-02-28 11:36 玩蛇网 www.iplaypy.com

我要分享到:

必知PYTHON教程 Must Know PYTHON Tutorials

必知PYTHON模块 Must Know PYTHON Modules