#!/bin/bash
#Author Buquan Liu, liubuquan@kylinos.cn, walt_lbq@163.com
#本程序本质上是对backup-auto/autobackup.cpp的重写.
#因为采用Qt程序则打包进入内核，相应的库会导致内核超过16M，故改为shell程序.

#backup-auto --autobackup ${rootpath} /backup
#backup-auto --autorestore ${rootpath} /backup

#启用调试模式，以便跟踪脚本的执行过程
#set -x
#在脚本中显式设置 PATH，确保包含 ostree 所在的目录
export PATH=/usr/bin:/bin:/usr/local/bin


echo $*

#xgs备份还原要保留更多的文件或目录:
#kybackup/maindialog.cpp, backup-daemon/mountpoint.cpp, backup-daemon/data/backup-auto-efi
XGS=false

INFO="/etc/.bootinfo"
METAINFO=".METAINFO"
KB=1024
MB=1048576
GB=1073741824

#if [ $# -lt 3 ]; then
#    exit 18
#fi

#是否有/data数据分区
hasDataPartition=0
backupORrestore=$1
rootpath=$2
m_backupPath=/data/.backup
m_default_uuid=$4
m_snapshots="${m_backupPath}/snapshots/"

m_FactorybackuplistPath="${m_backupPath}/snapshots/factorybackuplist.xml"
m_backuplistPath="${m_backupPath}/snapshots/backuplist.xml"
g_system_update_backuplist_xml="${m_backupPath}/snapshots/sys_update_backuplist.xml"
EXCLUDEFILE="${m_backupPath}/snapshots/.exclude"
PLOGFILEDIR="${m_backupPath}/log"
PLOGFILE="$PLOGFILEDIR/log-$(date +%Y%m%d%H%M)"
LOGFILE="${m_backupPath}/log.txt" #LOGFILE="/tmp/log.txt"

#是否是出厂备份
m_isFactory=false
factory_uuid="00000000-0000-0000-0000-000000000000"
auto_uuid="01234567-0123-0123-0123-0123456789ab"
PERSONAL_EXCLUDEFILE=".exclude.user.txt"
PERSONAL_BACKUPFILE=".user.txt"
m_isRetainUserData=false
m_PrefixDestPath=""
m_Position=false

#repo相关路径
FactoryRepoPath="${m_backupPath}/repo_factory"

#如果/backup不存在，则创建该目录
mkdir -p ${m_backupPath}
if [ $? -ne 0 ]; then
    echo "Could not create /data/.backup in initrd!"
    exit 20
fi

#如果/backup不存在，则创建该目录
mkdir -p ${m_snapshots}
if [ $? -ne 0 ]; then
    echo "Could not create /data/.backup/snapshots/ in initrd!"
    exit 20
fi

m_restoreUuid=""
m_enabled=""
global_system_usedDisk=0
m_size=0
newSize=0

#-----------------------------------------------------------------

initFactoryRepo() {
	# 设定要检测的文件夹路径
	FOLDER_PATH=${FactoryRepoPath}

	# 检测文件夹是否存在
	if [ ! -d "$FOLDER_PATH" ]; then
		echo "文件夹不存在，正在执行初始化逻辑..."
		# 初始化逻辑
		# 在这里可以添加任何你想要的初始化操作，例如创建文件夹，复制文件等
		# 执行 ostree 初始化
        /usr/bin/ostree --repo="$FOLDER_PATH" init --mode=archive-z2

        if [ $? -eq 0 ]; then
            echo "文件夹已创建并初始化成功：$FOLDER_PATH"
        else
            echo "初始化失败，请检查 ostree 命令和路径权限。" >&2
            return 1
        fi
		ostree --repo=/data/.backup/repo_factory init --mode=archive-z2
		echo "文件夹已创建：$FOLDER_PATH"
	else
		echo "文件夹已存在，无需初始化。"
	fi
} 


get_is_990_9a0() {
    local ret=false
    # 匹配 kirin 990 5g, kirin990, kirin 9006c
    if egrep -qi 'kirin.?9[09]0' /proc/cpuinfo; then
        ret=true
    elif egrep -qi 'PANGU' /proc/cpuinfo; then
	ret=true

    fi
    echo $ret
}
is_990_9a0=$(get_is_990_9a0)
#see backup-auto/autobackup.cpp
getBackupInfo() {
    if [ "$rootpath" = "/" ]; then
        bootinfo=$INFO
    else
        bootinfo=${rootpath}${INFO}
    fi

    if [ ! -e "$bootinfo" ]; then
	# 系统崩坏，如操作失误删除了/etc目录
	if [ -e $INFO ]; then
	    bootinfo=$INFO
	else
            echo "$bootinfo file not exist!"
            m_restoreUuid=""
            # exit 1
            return
	fi
    fi

    which_line=0
    content=$(cat "$bootinfo" | grep -Ev "^#" | grep "=" | awk '{print $1}')
    for line in $content; 
    do
        #parse_device "$device"
        #只读第1行：RECOVERY_DEV_UUID=c965e712-9903-4139-b8da-c6e1eef0af6a
        if [ $which_line -eq 0 ]; then
            m_restoreUuid=$(echo $line | sed 's:.*=::' | tr -d "\n")
            which_line=$(expr $which_line + 1)
        else
            m_enabled=$(echo $line | sed 's:.*=::' | tr -d "\n")
            which_line=$(expr $which_line + 1)
        fi
    done
}

#-----------------------------------------------------------------

#该函数是对backup-daemon/parsebackuplist.cpp中相应函数的替换
createBackupList() {
    local backuplistDir="${m_backupPath}/snapshots/"

    if [ ! -e "$backuplistDir" ]; then
        mkdir -p $backuplistDir
    fi

    if [ ! -e "$m_backuplistPath" ]; then
        #echo "$m_backuplistPath file not exist!"

        #第1行'>'会清空后写文件
        echo "<?xml version='1.0'?>" >$m_backuplistPath
        #echo "<backupList>" >>$m_backuplistPath
        #echo "</backupList>" >>$m_backuplistPath
        echo "<backupList/>" >>$m_backuplistPath #QDomDocument在节点为空时如此生成根节点
    fi
}

#-----------------------------------------------------------------
#see backup-auto/autobackup.cpp
mountBackup() {

	# 无备份分区时
	if [ "${m_restoreUuid}xy" = "xy" ]; then
    	echo "No backup partation. Log for backuping and restoring...."
		mount -o defaults,bind  ${rootpath}/backup ${m_backupPath}
		sleep 1
	else
		local myuuid="/dev/disk/by-uuid/"$m_restoreUuid
    		#echo "myuuid: $myuuid"

    		#support lvm by zhangze
    		tmp_root_dev=$(mount | grep " /root " | cut -d ' ' -f 1)
    		case "$tmp_root_dev" in
    		/dev/mapper/*)
        		eval $(dmsetup splitname --nameprefixes --noheadings --rows "${tmp_root_dev#/dev/mapper/}")
        		if [ "$DM_VG_NAME" ] && [ "$DM_LV_NAME" ]; then
            			lvm lvchange -aay -y --sysinit --ignoreskippedcluster "$DM_VG_NAME"
        		fi
        		;;
    		esac

    		#mount $myuuid $m_backupPath
    		mount -o defaults,rw -U $m_restoreUuid $m_backupPath
    		if [ $? -ne 0 ]; then
        		echo "Mount backup failed!"
        		exit 21
    		fi
	fi

    mkdir -p $PLOGFILEDIR
    if [ $? -ne 0 ]; then
        echo "Could not create log directory in /backup"
        exit 22
    fi

    touch $PLOGFILE
    if [ $? -ne 0 ]; then
        echo "Could not create log file"
        exit 23
    fi

    echo "Log for backuping and restoring...." > $PLOGFILE
    createBackupList #创建备份信息
}

#-----------------------------------------------------------------
#see backup-auto/autobackup.cpp
umountBackup() {
    umount $m_backupPath
}

#-----------------------------------------------------------------
#see backup-auto/backupcommon.cpp
#在grub时，根分区为/root；在进入系统后，根分区为/
#parameters: rootDiskName
#返回值 elements=( $totalSize $freeDisk $usedDisk )
caculateDiskSize() {
    local origalParas

    origalParas=($(echo "$@"))
    num=$(($#))

    #if [ $# -ne 2 ]; then
    if [ $num -ne 2 ]; then
        echo "You shoud input the rootDiskName and disk"
        exit 19
    fi

    local fullDiskName
    local totalSize
    local freeDisk
    local usedDisk

    if [ "${origalParas[1]}" = "/" ]; then
        fullDiskName=${origalParas[0]}
    else
        fullDiskName=${origalParas[0]}${origalParas[1]}
    fi

    if [ ! -e "$fullDiskName" ]; then
        ##因为要返回数组，所以下面的echo没有上面用，不会显示，只会作为返回值
        #echo "$fullDiskName not exist!"
        elements=(0 0 0)
        echo ${elements[*]}
        return
    fi

    ##因为要返回数组，这里不能echo "fullDiskName: $fullDiskName"
    sss=$(df -k $fullDiskName | sed '1d' | tr -d "\n")
    freeDisk=$(echo $sss | awk '{print $4}')
    freeDisk=$(expr 1024 \* $freeDisk)
    usedDisk=$(echo $sss | awk '{print $3}')
    usedDisk=$(expr 1024 \* $usedDisk)

    #totalSize没有从df命令的第2列来取，该值比下面2个的和还要大
    totalSize=$(expr $usedDisk + $freeDisk)

    ##因为要返回数组，这里不能echo "freeDisk=$freeDisk"
    ##因为要返回数组，这里不能echo "usedDisk=$usedDisk"
    ##因为要返回数组，这里不能echo "totalSize=$totalSize"

    #local elements
    elements=($totalSize $freeDisk $usedDisk)
    echo ${elements[*]}
}

parse_device() {
    device=$1
    if echo $device | grep -E -q "^UUID="; then
        echo $device | sed 's:^UUID=:/dev/disk/by-uuid/:' | tr -d "\n"
    elif echo $device | grep -E -q "^LABEL="; then
        echo $device | sed 's:^LABEL=:/dev/disk/by-label/:' | tr -d "\n"
    else
        echo $device | tr -d "\n"
    fi
}

is_remote() {
    fstype=$1
    if [ nfs = "$fstype" ] || [ nfs4 = "$fstype" ] || [ smbfs = "$fstype" ] || [ cifs = "$fstype" ] || [ coda = "$fstype" ] || [ ncp = "$fstype" ]; then
        echo yes
    elif [ ncpfs = "$fstype" ] || [ ocfs2 = "$fstype" ] || [ gfs = "$fstype" ] || [ gfs2 = "$fstype" ] || [ ceph = "$fstype" ]; then
        echo yes
    else
        echo no
    fi
}

function caculateDirSize() {
  mkdir -p /backup/snapshots/check/data
  local total_file_size=$(rsync  -aAHXrn --stats --ignore-missing-args --exclude=/backup --exclude=/cdrom --exclude=/dev --exclude=/etc/uid_list --exclude=/ghost --exclude=/data/ghost --exclude=/lost+found --exclude=/media --exclude=/mnt --exclude=/proc --exclude=/run --exclude=/swap_file --exclude=/sys --exclude=/tmp --exclude=/var/lib/docker/overlay2 --exclude=/var/lib/kmre/data --exclude=/var/lib/kmre/kmre-*-*/data/media/0/0-麒麟* --exclude=/var/lib/udisks2 --exclude=/var/log --exclude=*/backup/snapshots --exclude=/data/home --exclude=/data/root "${rootpath}/" /backup/snapshots/check/data/ | grep "Total file size:" | awk '{print $4}' | sed 's/,//g')
  total_file_size=$(expr ${total_file_size} + 200000000)
  echo "备份所需空间大小：${total_file_size}" >>$PLOGFILE
  echo "${total_file_size}"
}

#parameters: 无
#返回值：(system_totalSize system_freeDisk system_usedDisk)
caculateSystemSize() {
    local root_totalSize root_freeDisk root_usedDisk
    local system_totalSize system_freeDisk system_usedDisk
    local origalParas
    local found item result
    local device mntdir fstype options dump passno
    #local -a alreadyDisks #数组，已经分析过的磁盘目录
    local -a elements     #存放返回值

    #alreadyDisks=(${alreadyDisks[*]} "/boot") #插入一个元素"/boot"

    #result=(`caculateDiskSize "$rootpath" "$rootpath"`)
    #origalParas=($rootpath "/")
    #arg1=$(echo ${origalParas[*]})

    #result=($(caculateDiskSize $arg1))
    #root_totalSize=${result[0]}
    #root_freeDisk=${result[1]}
    #root_usedDisk=${result[2]}

    #system_totalSize=$root_totalSize
    #system_freeDisk=$root_freeDisk
    #system_usedDisk=$root_usedDisk

    #echo "system_totalSize="$system_totalSize
    #echo "system_freeDisk="$system_freeDisk
    #echo "system_usedDisk="$system_usedDisk

    ##############3#caculateDiskSize / /backup
    fstab_path=${rootpath}/etc/fstab

    if [ ! -e "$fstab_path" ]; then
	fstab_path=/etc/fstab-backup
        if [ ! -e "$fstab_path" ]; then
            echo "$fstab_path file not exist!"
            exit 16
	fi
    fi

    system_totalSize=0
    system_freeDisk=0
    system_usedDisk=0
    while read line; do
        #取第1个字符
        if [ "${line:0:1}" = "#" ]; then
            continue
        fi

        echo $line
	echo $line >>$PLOGFILE

        device=$(echo "$line" | awk '{print $1}')
        mntdir=$(echo "$line" | awk '{print $2}')
        fstype=$(echo "$line" | awk '{print $3}')
        options=$(echo "$line" | awk '{print $4}')
        dump=$(echo "$line" | awk '{print $5}')
        passno=$(echo "$line" | awk '{print $6}')

        device=$(parse_device $device)
	
	if [[ $options =~ "bind" ]]; then
	    continue
        fi

        # nodev filesystems
        (cat /proc/filesystems | grep "$fstype" | grep -q nodev) && continue

        # virtual or network filesystems
        [ none = "$mntdir" ] || [ yes = $(is_remote $fstype) ] && continue

        # swap or rootfs
        [ swap = "$fstype" ] && continue
        #[ swap = "$fstype" ] || [ / = "$mntdir" ] || [ /home = "$mntdir" ] || [ /var/log = "$mntdir" ]&& continue

        #取第1个字符
        if [ "${mntdir:0:1}" != "/" ]; then
            continue
        fi

        if [ $mntdir = "/backup" ]; then #没有写成$m_backupPath,看下面if
            continue
        fi

        if [ $mntdir = $m_backupPath ]; then #没有写成$m_backupPath
            continue
        fi

        #注意: 没有备份数据分区
        if [ $mntdir = "/data" ]; then
            hasDataPartition=1
        fi

        #echo "1: " $device
        #echo "2: " $mntdir
        #echo "3: " $fstype
        #echo "4: " $options
        #echo "5: " $dump
        #echo "6: " $passno

        # not system partition
        #		[ no = $(is_system_partition $mntdir) ] && continue

        ################计算df $target_mntdir的各项值
        origalParas=($rootpath $mntdir)
        arg1=$(echo ${origalParas[*]})

        result=($(caculateDiskSize $arg1))
        disk_totalSize=${result[0]}
        disk_freeDisk=${result[1]}
        disk_usedDisk=${result[2]}

        #echo "disk_totalSize="$disk_totalSize
        #echo "disk_freeDisk="$disk_freeDisk
        #echo "disk_usedDisk="$disk_usedDisk

        system_totalSize=$(expr $system_totalSize + $disk_totalSize)
        system_freeDisk=$(expr $system_freeDisk + $disk_freeDisk)
        system_usedDisk=$(expr $system_usedDisk + $disk_usedDisk)

        #echo "system_totalSize="$system_totalSize
        #echo "system_freeDisk="$system_freeDisk
        #echo "system_usedDisk="$system_usedDisk

    done <"$fstab_path"

    #echo "system_totalSize="$system_totalSize
    #echo "system_freeDisk="$system_freeDisk
    #echo "system_usedDisk="$system_usedDisk

    #local elements
    elements=($system_totalSize $system_freeDisk $system_usedDisk)
    echo ${elements[*]}
}

#-----------------------------------------------------------------
#//检查备份还原分区/backup剩余空间是否满足备份需求
#see backup-auto/backupcommon.cpp
#返回值: 0表示/backup剩余容量不能够满足备份需求；1表示满足
checkBackupCapacity() { #(m_rootPath.toStdString().c_str(), retstatus))
    local origalParas
    local root_totalSize root_freeDisk root_usedDisk
    local backup_totalSize backup_freeDisk backup_usedDisk
    local system_totalSize system_freeDisk system_usedDisk
    ###which_line=`expr $which_line + 1`

    #从函数caculateSystemSize取返回值
    result=($(caculateSystemSize))
    system_totalSize=${result[0]}
    system_freeDisk=${result[1]}
    system_usedDisk=${result[2]}

    #global_system_usedDisk=$system_usedDisk
    global_system_usedDisk=$(caculateDirSize)

    #echo "--system_totalSize="$system_totalSize
    #echo "--system_freeDisk="$system_freeDisk
    #echo "--system_usedDisk="$system_usedDisk

    #--------------------------------------------------------
    #result=(`caculateDiskSize "$rootpath" "$rootpath"`)
    origalParas=($rootpath "/")
    arg1=$(echo ${origalParas[*]})

    result=($(caculateDiskSize $arg1))
    root_totalSize=${result[0]}
    root_freeDisk=${result[1]}
    root_usedDisk=${result[2]}
    #echo "--root_totalSize="$root_totalSize
    #echo "--root_freeDisk="$root_freeDisk
    #echo "--root_usedDisk="$root_usedDisk

    #--------------------------------------------------------
    #caculateDiskSize / /backup
    #origalParas=($rootpath "/backup")
    #origalParas=("/" "/backup") #/backup不会是/root/backup
    origalParas=("/" $m_backupPath) #/backup不会是/root/backup
    arg1=$(echo ${origalParas[*]})

    result=($(caculateDiskSize $arg1))
    backup_totalSize=${result[0]}
    backup_freeDisk=${result[1]}
    backup_usedDisk=${result[2]}

    #echo "--backup_totalSize="$backup_totalSize
    #echo "--backup_freeDisk="$backup_freeDisk
    #echo "--backup_usedDisk="$backup_usedDisk

    if [[ $root_totalSize = $backup_totalSize && $root_freeDisk = $backup_freeDisk && $root_usedDisk = $backup_usedDisk ]]; then #"/backup"是不是一个独立的盘
        echo "Cannot find the /backup disk or not be mounted!"
        exit 12
    fi

    echo "free size : $backup_freeDisk; sys_size : $global_system_usedDisk"
    echo "free size : $backup_freeDisk; sys_size : $global_system_usedDisk" >>$PLOGFILE
    if [ ${backup_freeDisk} -gt ${global_system_usedDisk} ]; then
        return 1
    else
        return 0
    fi
}

updateStateByComment() {
    tmpFile=${m_backuplistPath}".tmp"
    #echo "tmpFile: $tmpFile"
    is_first_line=1

    foundComment=0 #是否发现了要修改的comment

    #如果不定义IFS，则echo $line会去掉前后空格，导致写到文件中去时没有格式
    IFS_old=$IFS
    IFS=$'\n'
    while read line; do
        #去除了前后空格
        xxx=$(echo "$line" | sed "s/^[ \t]*//g" | sed "s/[ \t]*$//g")
        #echo "xxx: $xxx"

        if [[ "$xxx" =~ "<Comment>" ]]; then
            if [ $xxx = "<Comment>${m_comment}</Comment>" ]; then
                foundComment=1 #当前comment是要修改的mycomment
            else
                foundComment=0 #当前comment不是要修改的mycomment
            fi
        fi

	if [[ "$xxx" =~ "<Size>" ]]; then
            if [[ $foundComment -eq 1 ]]; then
                line="        <Size>${newSize}</Size>"
            fi
        fi


        if [[ "$xxx" =~ "<State>" ]]; then
            if [ $foundComment -eq 1 ]; then
                line="        <State>${m_state}</State>"
            fi
        fi

        if [ "$is_first_line" -eq 1 ]; then
            echo "$line" >$tmpFile
        else
            echo "$line" >>$tmpFile
        fi

        is_first_line=0

    done <"$m_backuplistPath"
    IFS=$IFS_old

    cp -f $tmpFile ${m_backuplistPath}
    rm -f $tmpFile
}

#参照backup-daemon/backupmanager.cpp, 写日志文件
writeLogFile() {
    echo $1 >>$LOGFILE
}

#参照backup-daemon/mountpoint.cpp
bFileExists() {
    local theFile theOldFile
    theOldFile=$1 #必须以"/"开始，但本身是个相对路径，因为没有加rootpath.

    if [ $rootpath = "/" ]; then
        theFile=$theOldFile
    else
        theFile=$theOldFile
        theFile="$rootpath""$theFile"
    fi

    if [ -e "$theFile" ]; then
        echo "$theOldFile" >>$EXCLUDEFILE
    fi
}

#参照backup-daemon/mountpoint.cpp
generateExcludeFile() {
    local backupOrRestore
    backupOrRestore=$1         #0: backup 1:restore

    #exclude的必须是相对目录，其实在efi启动时为/root/data
    #see backup-daemon/mountpoint.cpp, backup-daemon/data/backup-auto-efi
    #also kybackup: exclude.ui databackupdirs.ui dataincbackupdirs.ui
    #if [ ! -e "$restoreDir" ]; then
    #注意下面的>和>>-----------------------------------------------
    echo "/efi" >$EXCLUDEFILE 
    echo "/backup" >>$EXCLUDEFILE
    #echo "/boot/efi" >>$EXCLUDEFILE
    echo "/dev" >>$EXCLUDEFILE
    echo "/ghost" >>$EXCLUDEFILE #ghost镜像文件
    echo "/mnt" >>$EXCLUDEFILE
    echo "/proc" >>$EXCLUDEFILE
    echo "/run" >>$EXCLUDEFILE
    echo "/sys" >>$EXCLUDEFILE
    echo "/media" >>$EXCLUDEFILE
    echo "/tmp" >>$EXCLUDEFILE
    echo "*/lost+found" >>$EXCLUDEFILE
    echo "/var/lib/udisks2" >>$EXCLUDEFILE
    #echo "/data/home/*" >>$EXCLUDEFILE
    #echo "/data/root/*" >>$EXCLUDEFILE
    #if [ -e "${rootpath}/data/home" ]; then
    #	echo "/home" >>$EXCLUDEFILE
    #	echo "/root" >>$EXCLUDEFILE
    #fi
    echo "/cdrom" >>$EXCLUDEFILE
    echo "/swap_file" >>$EXCLUDEFILE
    echo "/var/lib/docker/overlay2" >>$EXCLUDEFILE
    echo "*/backup/snapshots" >>$EXCLUDEFILE
    echo "/var/log" >>$EXCLUDEFILE
    echo "/data/security-dir" >>$EXCLUDEFILE

    #bind挂载的目录不进行备份或还原
    if [ -z $fstab_path ]; then
      fstab_path=${rootpath}/etc/fstab

      if [ ! -e "$fstab_path" ]; then
          fstab_path=/etc/fstab-backup
          if [ ! -e "$fstab_path" ]; then
              echo "$fstab_path file not exist!"
              exit 16
          fi
      fi
    fi
    cat $fstab_path | awk '{if($4~/bind/) print $1}' |
    while read excludePath
    do
        echo "$excludePath" >>$EXCLUDEFILE
    done

    #bFileExists "/etc/.bootinfo"
    #bFileExists "/etc/fpb" 	#管控,暂时
    #echo "/etc/.bootinfo" >>$EXCLUDEFILE

    #数据分区是否使用由用户输入，最终放到/backup/snapshots/.excludeuser
    #因为GRUB没有界面，所以是不是先在其他模式下做一次备份，生成这个文件；然后GRUB就可以了。
    #echo "/data/*" >>$EXCLUDEFILE   #用户可以把数据放到该分区或者目录

    #是否覆盖备份还原工具自身，因为grub备份还原使用的工具是initrd.img里面的东西，故不存在时可以还原备份还原工具本身(例如工具被删除的场景)
    #if [ $backupOrRestore -eq 1 ]; then
    #  bFileExists "/usr/bin/backup-daemon"
    #  bFileExists "/usr/bin/kybackup"
    #  bFileExists "/usr/bin/backup-auto"
    #  bFileExists "/usr/bin/mount_fstab_efi"
    #  bFileExists "/usr/bin/backup-auto-efi"
    #  bFileExists "/usr/bin/rsync"
    #  bFileExists "/usr/share/rsync"
    #  bFileExists "/usr/share/initramfs-tools/hooks/kybackup-hooks"
    #  bFileExists "/usr/share/initramfs-tools/scripts/local-bottom/kybackup"
    #fi

    # 安全模块会将文件/usr/share/kysec-utils/data/readonly_list中的文件列表限制只读，无法修改、备份（包含扩展属性时）、删除等
    # 现在里面仅有/etc/uid_list，先暂时排除掉；等后续安全模块有其它保护方案后再进一步修改
    # 新：用安全保护程序/usr/bin/setstatus可以关闭保护，故不再排除此文件
    # echo "/etc/uid_list" >>$EXCLUDEFILE
    #if [ $backupOrRestore -eq 1 ]; then
    #    bFileExists "/usr/bin/backup-daemon"  	#备份还原
    #    bFileExists "/usr/bin/kybackup"		#备份还原
    #    bFileExists "/usr/bin/mount_fstab"  	#备份还原
    #    bFileExists "/usr/bin/backup-auto"  	#备份还原
    #    bFileExists "/usr/bin/mount_fstab_efi" 	#备份还原
    #    bFileExists "/usr/bin/backup-auto-efi" 	#备份还原
    #fi

    #是否使用由用户输入，最终放到/backup/snapshots/.excludeuser
    #if [ "$XGS" = true ]; then
    #    echo "/etc/passwd" >>$EXCLUDEFILE
    #    echo "/etc/group" >>$EXCLUDEFILE
    #    echo "/etc/shadow" >>$EXCLUDEFILE
    #    if [ -e "/etc/uid_list" ]; then
    #	echo "/etc/uid_list" >>$EXCLUDEFILE
    #    fi

    #    echo "/home/*" >>$EXCLUDEFILE
    #    echo "/opt/softmanager/log/log_cur.txt" >>$EXCLUDEFILE
    #    echo "/opt/softmanager/conf/audit/auditLogCur.txt" >>$EXCLUDEFILE
    #    echo "/opt/xgs/Audit/*" >>$EXCLUDEFILE
    #    echo "/opt/LinuxKpc/log/*" >>$EXCLUDEFILE
    #    echo "/var/log/*" >>$EXCLUDEFILE
    #    echo "/var/run/*" >>$EXCLUDEFILE
    #    echo "/root/*" >>$EXCLUDEFILE
    #    echo "/var/mail/*" >>$EXCLUDEFILE
    #    echo "/boot/efi/*" >>$EXCLUDEFILE #xgs出厂还原时会失败
    #fi
    #fi
}

backuping() {
    local src dst

    src=$1
    dst=$2

    #0:backup 1:restore
    generateExcludeFile 0
	echo "/" > ${m_backupPath}/snapshots/$m_uuid/$PERSONAL_BACKUPFILE
	cp $EXCLUDEFILE ${m_backupPath}/snapshots/$m_uuid/$PERSONAL_EXCLUDEFILE

    echo "Begin to backup other directories..." >>$PLOGFILE
    echo "Begin to backup other directories..."
    #是否有数据分区
    if [ $hasDataPartition -eq 0 ]; then
        #exclude的必须是相对目录，其实在efi启动时为/root/data
        #echo rsync -avAXH --ignore-missing-args --exclude-from $EXCLUDEFILE $src $dst
        #rsync -avAXH --ignore-missing-args --exclude-from $EXCLUDEFILE $src $dst

        #不是目录，也不备份
        echo rsync -avAXH --ignore-missing-args --exclude-from $EXCLUDEFILE $src $dst >/dev/null 2>>$PLOGFILE
        rsync -avAXH --ignore-missing-args --exclude-from $EXCLUDEFILE $src $dst >/dev/null 2>>$PLOGFILE
    else
        #exclude的必须是相对目录，其实在efi启动时为/root/data
        #echo rsync -avAXH --ignore-missing-args --exclude data --exclude-from=$EXCLUDEFILE $src $dst
        #The question is that the 'data' directories can not be copied
        echo rsync -avAXH --ignore-missing-args --exclude-from $EXCLUDEFILE $src $dst >/dev/null 2>>$PLOGFILE
        rsync -avAXH --ignore-missing-args --exclude-from $EXCLUDEFILE $src $dst >/dev/null 2>>$PLOGFILE
    fi

    if [ $? -eq 0 -o $? -eq 24 -o $? -eq 23 ]; then
        #updateStateByComment $m_comment 0
	sync
        m_state="backup finished"
        touch $dst/.exectl
	#caculateDirSize $dst

        updateStateByComment

    else
        #将状态从"backup unfinished"改成"backup failed"
        echo "backup update backuplist.xml failed for updateBackupAutoFinishedState!"
        echo "backup update backuplist.xml failed for updateBackupAutoFinishedState!" >>$PLOGFILE
        m_state="backup unfinished"
        updateStateByComment
        echo "System backuping failed, please reboot your system!"
        echo "System backuping failed, please reboot your system!" >>$PLOGFILE
        exit 14
    fi

    #写文件
    metainfo_file="${m_backupPath}/snapshots/$m_uuid/$METAINFO"
    #echo "metainfo_file="$metainfo_file
    #第1行清空写
    #这里写的不是xml文件，是一个文本文件，这时候的状态是0或者backup failed
    echo "COMMENT=$m_comment" >$metainfo_file
    echo "TIME=$m_time" >>$metainfo_file
    echo "UUID=$m_uuid" >>$metainfo_file
    echo "SIZE=$m_size" >>$metainfo_file
    echo "STATE=$m_state" >>$metainfo_file

    #写日志文件
    writeLogFile "${m_time},${m_uuid},0,grub备份,${newSize}" #grub时只有全盘备份，没有增量备份
    sync
}

backupingFactory() {
    local src dst

    src=$1
    dst=$2
	
	ETC_PATH="/etc"
	BOOT_PATH="/boot"
	HOME_PATH="/home"
	
	#执行ostree指令生成对应的repo
	etc_hash=$(ostree --repo=${FactoryRepoPath} commit -b backup_branch --tree=dir=${ETC_PATH} "Factory backup etc")
	echo ${etc_hash}
	boot_hash=$(ostree --repo=${FactoryRepoPath} commit -b backup_branch --tree=dir=${BOOT_PATH} "Factory backup boot")
	echo ${boot_hash}
	home_hash=$(ostree --repo=${FactoryRepoPath} commit -b backup_branch --tree=dir=${HOME_PATH} "Factory backup home")
	echo ${home_hash}
	echo $?    

    if [ $? -eq 0 -o $? -eq 24 -o $? -eq 23 ]; then
        #updateStateByComment $m_comment 0
	sync
        m_state="backup finished"
    else
        #将状态从"backup unfinished"改成"backup failed"
        m_state="backup failed"
        exit 14
    fi

    #写文件
    metainfo_file="${m_backupPath}/snapshots/$METAINFO"
    #echo "metainfo_file="$metainfo_file
    #第1行清空写
    #这里写的不是xml文件，是一个文本文件，这时候的状态是0或者backup failed
    echo "COMMENT=$m_comment" >$metainfo_file
    echo "TIME=$m_time" >>$metainfo_file
    echo "UUID=$m_uuid" >>$metainfo_file
    echo "SIZE=$m_size" >>$metainfo_file
    echo "STATE=$m_state" >>$metainfo_file
	echo "ETC=$etc_hash" >>$metainfo_file
    echo "BOOT=$boot_hash" >>$metainfo_file
    echo "HOME=$home_hash" >>$metainfo_file

    #写日志文件
	writeLogFile "=================================出场备份完成================================="
    writeLogFile "${m_time},${m_uuid},0,grub备份,${newSize}" #grub时只有全盘备份，没有增量备份
    sync
}

CreateUuid() {
	local uuid=`cat /proc/sys/kernel/random/uuid|tr -d "\n"`
	while [ "$uuid" = $factory_uuid -o "$uuid" = $auto_uuid ]
    do
    	uuid=`cat /proc/sys/kernel/random/uuid|tr -d "\n"`
    done
    echo $uuid
}

findCommentByUuid() {

	local ret=1
	local local_uuid=$1
	local comment0
	local ret_comment

	#如果不定义IFS，则echo $line会去掉前后空格，导致写到文件中去时没有格式
	#将 IFS 设置为换行符 \n，确保 read 命令逐行读取文件，而不是按空格或制表符分隔。
	IFS_old=$IFS
	IFS=$'\n'
	while read line;
	do
	#去除了前后空格
		xxx=$( echo "$line" | sed "s/^[ \t]*//g" | sed "s/[ \t]*$//g" )
		#echo "xxx: $xxx"

		if [[ "$xxx" =~ "<Comment>" ]]; then
        	comment0=`echo $xxx | awk -F "<Comment>" '{print $2}' |awk -F "</Comment>" '{print $1}' | tr -d "\n"`
        fi

        if [[ "$xxx" =~ "<Uuid>" ]]; then
        	if [ $xxx = "<Uuid>${local_uuid}</Uuid>" ]; then
            	ret=0
                ret_comment=$comment0
                break
            fi
        fi
    done < "$m_backuplistPath";
    IFS=$IFS_old

	echo $ret_comment
	return $ret
}

deleteItemByComment() {
	local local_comment=$1
	echo "arg local_comment = $local_comment"

	tmpFile=${m_backuplistPath}".tmp"
	cp -f ${m_backuplistPath} $tmpFile
#        echo "tmpFile: $tmpFile"

	local foundComment=0 #是否发现了要修改的comment
	local i=0
	local ii=0
	local iii=0
	local backupPointTmp=0

	#如果不定义IFS，则echo $line会去掉前后空格，导致写到文件中去时没有格式
	IFS_old=$IFS
	IFS=$'\n'
	while read line;
	do
		let i+=1
		#去除了前后空格
		xxx=$( echo "$line" | sed "s/^[ \t]*//g" | sed "s/[ \t]*$//g" )
		#echo "xxx: $xxx"

		if [[ "$xxx" =~ "<BackupPoint>" ]]; then
			backupPointTmp=$i
		fi
		if [[ "$xxx" =~ "<Comment>" ]]; then
			if [ $xxx = "<Comment>$local_comment</Comment>" ]; then
				foundComment=1 #当前comment是要修改的mycomment
					ii=$backupPointTmp
					#echo "delete foundComment = 1"
			else
				foundComment=0 #当前comment不是要修改的mycomment
			fi
		fi
		if [[ "$xxx" =~ "</BackupPoint>" ]]; then
			if [ $foundComment -eq 1 ]; then
				iii=$i
				break
            fi
        fi
	done < "$tmpFile";
	IFS=$IFS_old

#        echo "ii: $ii, iii: $iii"

	if [ $iii -ne 0 ]; then
		sed -i "${ii},${iii}d" $tmpFile
        cp -f $tmpFile ${m_backuplistPath}
    fi
    rm -f $tmpFile
}

DeleteFactoryBackup() {
	if [ -e "${FactoryRepoPath}" ]; then
		rm ${FactoryRepoPath} -rf
	fi

#	local comment=$(findCommentByUuid "{${factory_uuid}}")
#       echo "DeleteFactoryBackup comment is $comment"
#	if [ $? -eq 0 ]; then
#		deleteItemByComment "$comment"
#	fi
}

#see backup-auto/autobackup.cpp
backupAuto() { #备份
    if [ $m_isFactory = true ]; then
		DeleteFactoryBackup
		initFactoryRepo
    fi

    local xxx
    #checkBackupCapacity
    #ret=$?
    #if [ "$ret" -eq 0 ]; then
    #    echo "The backup disk space is not enough"
    #    exit 4
    #fi

    #global_system_usedDisk
    #创建一个uuid
    if [ $m_isFactory = false ]; then
		m_uuid=$(CreateUuid)
    else
		m_uuid="$factory_uuid"
	fi
    m_uuid="{"${m_uuid}"}"
	#echo "BYbobbi: m_uuid is $m_uuid"
    m_time=$(date "+%y-%m-%d %H:%M:%S" | tr -d "\n")
    m_comment=$m_time #这个是全局变量

    state="backup unfinished"

    #写入文件m_backuplistPath=$m_backupPath"/snapshots/backuplist1.xml"
    #bool AutoBackup::backupAuto()251 if (!m_parse->addItem(m_comment, time, m_uuid, newsize, "backup unfinished")) {
    #
    tmpFile=${m_FactorybackuplistPath}".tmp"
    #echo "tmpFile: $tmpFile"
    new_content=""
    is_first_line=1

    hasHead=false #有头吗"<backupList>"

    #如果不定义IFS，则echo $line会去掉前后空格，导致写到文件中去时没有格式
    IFS_old=$IFS
    IFS=$'\n'

    while read line; do
        #去除了前后空格
        xxx=$(echo "$line" | sed "s/^[ \t]*//g" | sed "s/[ \t]*$//g")
        #echo "xxx: $xxx"

        if [[ "$xxx" =~ "<backupList>" ]]; then
            hasHead=true
        fi

        if [[ "$xxx" =~ "<backupList/>" ]]; then
            xxx="</backupList>" #与图形界面一致
            line="</backupList>"
        fi

        #插入新的记录
        if [ $xxx = "</backupList>" ]; then
            if [ "$hasHead" = false ]; then
                echo "<backupList>" >>$tmpFile
                hasHead=true
            fi

            echo "    <BackupPoint>" >>$tmpFile
            echo "        <Comment>$m_comment</Comment>" >>$tmpFile
            echo "        <Time>$m_time</Time>" >>$tmpFile
            echo "        <Uuid>$m_uuid</Uuid>" >>$tmpFile
            echo "        <Size>$newSize</Size>" >>$tmpFile
            echo "        <State>$state</State>" >>$tmpFile
            echo "        <Type>0</Type>" >>$tmpFile
            echo "    </BackupPoint>" >>$tmpFile
        fi

        if [ "$is_first_line" -eq 1 ]; then
            echo "$line" >$tmpFile
        else
            echo "$line" >>$tmpFile
        fi

        is_first_line=0

    done <"$m_backuplistPath"
    IFS=$IFS_old$

    cp -f $tmpFile ${m_FactorybackuplistPath}
    rm -f $tmpFile

    #写文件
    metainfo_file="${m_backupPath}/snapshots/$METAINFO"
    #echo "metainfo_file="$metainfo_file
    #第1行清空写
    #这里写的不是xml文件，是一个文本文件，这时候的状态是backup unfinished
    echo "COMMENT=$m_comment" >$metainfo_file
    echo "TIME=$m_time" >>$metainfo_file
    echo "UUID=$m_uuid" >>$metainfo_file
    echo "SIZE=$m_size" >>$metainfo_file
    echo "STATE=$state" >>$metainfo_file
    echo "TYPE=0" >>$metainfo_file

    backupingFactory 
}

getLastUsefulBackupPoint() {
    local xmlFile=$1
    echo "xmlFile=${xmlFile}"

    local currentUuid=""
    local currentState=false
    local currentType=true
    local currentComment=""
    local backupPositionMode=false
    local backupPrefixDestPath=""
    local Time=""
    
    ret_lastUsefulUuid=""
    ret_time=""
    ret_isPosition=false
    ret_prefixDestPath=""
    ret_lastbackupname=""

    IFS_old=$IFS
    IFS=$'\n'
    while read line; do
        #去除了前后空格和制表符
        line=$(echo "$line" | sed "s/^[ \t]*//g" | sed "s/[ \t]*$//g")
       
        if [[ "$line" =~ "<Comment>" ]]; then
            currentComment=${line#<Comment>}
            currentComment=${currentComment%</Comment>}
        fi

        if [[ "$line" =~ "<Time>" ]]; then
            Time=${line#<Time>}
            Time=${Time%</Time>}
        fi

        if [[ "$line" =~ "<Uuid>" ]]; then
            echo "uuid=${line} , m_default_uuid = ${m_default_uuid}" >>$PLOGFILE
            currentUuid=${line#<Uuid>}
            currentUuid=${currentUuid%</Uuid>}
            currentState=false
            currentType=true
	        backupPositionMode=false
	        backupPrefixDestPath=""
        fi

        if [[ "$line" =~ "<State>backup finished</State>" ]]; then
            currentState=true
        fi

        if [[ "$line" =~ "<Type>2</Type>" ]]; then
            currentType=false
        fi

        if [[ "$line" =~ "<Type>3</Type>" ]]; then
            currentType=false
        fi

        if [[ "$line" =~ "<Position>3</Position>" ]]; then
            backupPositionMode=true
        fi

        if [[ "$line" =~ "<PrefixDestPath>" ]]; then
            backupPrefixDestPath=${line#<PrefixDestPath>}
            backupPrefixDestPath=${backupPrefixDestPath%</PrefixDestPath>}
            # if [[ x"$backupPrefixDestPath" = x"" || "$backupPrefixDestPath" = "/" || $backupPrefixDestPath =~ ^/data || $backupPrefixDestPath =~ ^/autobackup ]]; then
            if [[ ! $backupPrefixDestPath =~ ^/media/ ]]; then
                backupPositionMode=true
            else
                currentType=false
            fi
        fi

        if [[ "$line" =~ "</BackupPoint>" ]]; then
            if [ "$currentState" = "true" -a "$currentType" = "true" ]; then #"/backup"是不是一个独立的盘
                ret_lastUsefulUuid=$currentUuid
		        ret_isPosition=$backupPositionMode
		        ret_prefixDestPath=$backupPrefixDestPath
                ret_lastbackupname=$currentComment
                ret_time=$Time
                if [[ -n "${m_default_uuid}" && "${currentUuid}" = "${m_default_uuid}" ]]; then
                    echo "找到了指定的备份点${currentUuid}" >>$PLOGFILE
                    break
                elif [ -n "${m_default_uuid}" ]; then
                    echo "${currentUuid}不是指定的备份点${m_default_uuid}，继续下一个" >>$PLOGFILE
                    ret_lastUsefulUuid=""
		            ret_isPosition=false
		            ret_prefixDestPath=""
                    ret_lastbackupname=""
                    ret_time=""
                fi
            fi
        fi
    done <"$xmlFile"
    IFS=$IFS_old


    if [ -z "${ret_lastUsefulUuid}" ]; then
        echo "${xmlFile} no usable uuid" 
        echo "${xmlFile} no usable uuid" >> $PLOGFILE
    else
        echo "Found uuid in ${xmlFile}  ：${ret_lastUsefulUuid}" >>$PLOGFILE
        echo "Found uuid in ${xmlFile}  ：${ret_lastUsefulUuid}"
    fi
}

#返回值：
getLastUsefulBackupPointUuid() {
    local xxx=""
    local currentUuid=""
    local currentState=false
    local currentType=true
    lastUsefulBackupPointUuid=""
    foundComment=0 #是否发现了要修改的comment
    local currentComment=""
    lastbackupname=""
    local backupPositionMode=false
    local backupPrefixDestPath=""

    local Time=""
    local backupTime=""

    #如果不定义IFS，则echo $line会去掉前后空格，导致写到文件中去时没有格式
    IFS_old=$IFS
    IFS=$'\n'
    while read line; do
        #去除了前后空格
        xxx=$(echo "$line" | sed "s/^[ \t]*//g" | sed "s/[ \t]*$//g")
        #echo "xxx: $xxx"
       
        if [[ "$xxx" =~ "<Comment>" ]]; then
            currentComment=$xxx
        fi

        if [[ "$xxx" =~ "<Time>" ]]; then
            Time=${xxx#<Time>}
            Time=${Time%</Time>}
        fi

        if [[ "$xxx" =~ "<Uuid>" ]]; then
            echo "uuid=${xxx} , m_default_uuid = ${m_default_uuid}" >>$PLOGFILE
            currentUuid=${xxx}
            currentState=false
            currentType=true
	        backupPositionMode=false
	        backupPrefixDestPath=""
        fi

        if [[ "$xxx" =~ "<State>backup finished</State>" ]]; then
            currentState=true
        fi

        if [[ "$xxx" =~ "<Type>2</Type>" ]]; then
            currentType=false
        fi

        if [[ "$xxx" =~ "<Type>3</Type>" ]]; then
            currentType=false
        fi

	if [[ "$xxx" =~ "<Position>3</Position>" ]]; then
        #    currentType=false
	    backupPositionMode=true
    fi

	if [[ "$xxx" =~ "<PrefixDestPath>" ]]; then
		backupPrefixDestPath=${xxx#<PrefixDestPath>}
		backupPrefixDestPath=${backupPrefixDestPath%</PrefixDestPath>}
		# if [[ x"$backupPrefixDestPath" = x"" || "$backupPrefixDestPath" = "/" || $backupPrefixDestPath =~ ^/data || $backupPrefixDestPath =~ ^/autobackup ]]; then
        if [[ ! $backupPrefixDestPath =~ ^/media/ ]]; then
			backupPositionMode=true
		else
			currentType=false
		fi
	fi

        if [[ "$xxx" =~ "</BackupPoint>" ]]; then
            if [ "$currentState" = "true" -a "$currentType" = "true" ]; then #"/backup"是不是一个独立的盘
                lastUsefulBackupPointUuid=$currentUuid
		        m_Position=$backupPositionMode
		        m_PrefixDestPath=$backupPrefixDestPath
                lastbackupname=$currentComment
                backupTime=${Time}
                if [[ -n "${m_default_uuid}" && "${currentUuid}" =~ "${m_default_uuid}" ]]; then
                    echo "找到了指定的备份点${currentUuid}" >>$PLOGFILE
                    break
                elif [ -n "${m_default_uuid}" ]; then
                    echo "${currentUuid}不是指定的备份点${m_default_uuid}，继续下一个" >>$PLOGFILE
                    lastUsefulBackupPointUuid=""
		            m_Position=false
		            m_PrefixDestPath=""
                    lastbackupname=""
                    backupTime=""
                fi
            fi
        fi
    done <"$m_backuplistPath"
    IFS=$IFS_old

    getLastUsefulBackupPoint ${g_system_update_backuplist_xml}

    if [[ -z "$lastUsefulBackupPointUuid" && -z "${ret_lastUsefulUuid}" ]]; then
        echo "can't find a useful backup for restoring"
        exit 6
    elif [[ -n "$lastUsefulBackupPointUuid" && -n "${ret_lastUsefulUuid}" ]]; then
        if [[ "${backupTime}" < "${ret_time}" ]]; then
            lastUsefulBackupPointUuid=${ret_lastUsefulUuid}
            lastbackupname=${ret_lastbackupname}
            m_Position=${ret_isPosition}
            m_PrefixDestPath=${ret_prefixDestPath}
        else
            lastUsefulBackupPointUuid=$(echo $lastUsefulBackupPointUuid | sed 's:<Uuid>::' | tr -d "\n")
            lastUsefulBackupPointUuid=$(echo $lastUsefulBackupPointUuid | sed 's:</Uuid>.*::' | tr -d "\n")
            lastbackupname=$(echo $lastbackupname | sed 's:<Comment>::' | tr -d "\n")
            lastbackupname=$(echo $lastbackupname | sed 's:</Comment>.*::' | tr -d "\n")
        fi
    elif [[ -n "${ret_lastUsefulUuid}" ]]; then
        lastUsefulBackupPointUuid=${ret_lastUsefulUuid}
        lastbackupname=${ret_lastbackupname}
        m_Position=${ret_isPosition}
        m_PrefixDestPath=${ret_prefixDestPath}
    else
        lastUsefulBackupPointUuid=$(echo $lastUsefulBackupPointUuid | sed 's:<Uuid>::' | tr -d "\n")
        lastUsefulBackupPointUuid=$(echo $lastUsefulBackupPointUuid | sed 's:</Uuid>.*::' | tr -d "\n")
        lastbackupname=$(echo $lastbackupname | sed 's:<Comment>::' | tr -d "\n")
        lastbackupname=$(echo $lastbackupname | sed 's:</Comment>.*::' | tr -d "\n")
    fi

    echo "UUID to be restored：${lastUsefulBackupPointUuid}" >>$PLOGFILE
    echo "UUID to be restored：${lastUsefulBackupPointUuid}"
}

#see backup-auto/autobackup.cpp
restoreAuto() { #还原
    local xxx
    local uuid
    if [ $m_isFactory = true ]; then
    	m_default_uuid="{00000000-0000-0000-0000-000000000000}"
    fi
    getLastUsefulBackupPointUuid
	uuid=$lastUsefulBackupPointUuid
	if [ "${uuid}" = "{00000000-0000-0000-0000-000000000000}" ]; then
		m_isFactory=true
	fi

    #写日志文件
    local m_time=$(date "+%y-%m-%d %H:%M:%S" | tr -d "\n")
    writeLogFile "${m_time},${uuid},4,grub系统还原,,,${lastbackupname}" #grub时只有一键还原，没有增量还原

    restoreDir="${m_backupPath}/snapshots/${uuid}"
    restoreDataPath="${restoreDir}/data"

    if [ $m_Position = true ]; then
	    local prePath=""
	    # if [[ $m_PrefixDestPath =~ ^/data || $m_PrefixDestPath =~ ^/autobackup ]]; then
	    if [[ "x${m_PrefixDestPath}y" != "xy" && "x${m_PrefixDestPath}y" != "x/y" ]]; then
		    prePath="${rootpath}${m_PrefixDestPath}"
	    fi

	    local imgFile="${prePath}/backup/snapshots/${uuid}/data/dst.img"
        local bootDir="${prePath}/backup/snapshots/${uuid}/data/boot"
        echo "imgFile = ${imgFile}"
	    echo "imgFile = ${imgFile}" >> $PLOGFILE
        echo "bootDir = ${bootDir}"
	    echo "bootDir = ${bootDir}" >> $PLOGFILE
	    if [ -e $imgFile ]; then
            local imgPath="${m_backupPath}/imgbackup"
		    mkdir -p $imgPath
		    mount -o loop,ro "${imgFile}" "${imgPath}"
		    sleep 1
		    mount | grep imgbackup >> ${PLOGFILE}
		    if [ ! -e "${imgPath}/boot" ]; then
			    echo "${imgFile} does not mounted on ${imgPath}"
			    exit 9
		    fi
		    restoreDataPath=${imgPath}
	        restoreDir="${prePath}/backup/snapshots/${uuid}"
        elif [ -e $bootDir ]; then
            restoreDir="${prePath}/backup/snapshots/${uuid}"
            restoreDataPath="${restoreDir}/data"
            echo "非压缩备份点${restoreDir}" >> $PLOGFILE
	    else
		    echo "$imgFile does not exist!"
		    exit 7 #备份文件不存在，不能还原系统
	    fi
    fi

    if [ ! -e "${restoreDataPath}/boot" ]; then
        echo "full restore directory not exists!"
        exit 7 #备份文件不存在，不能还原系统
    fi

    if [ "$uuid" = "{$auto_uuid}" ]; then
	    m_isRetainUserData=true
    fi

    #0:backup 1:restore
    #generateExcludeFile 1
    echo "Begin to restore efi directory..." >>$PLOGFILE
    echo "Begin to restore efi directory..."
    #额外排除目录或文件
    local excludes=
    if [ -d ${restoreDataPath}/efi ]; then
        rsync -avAXHr --no-inc-recursive --ignore-missing-args --delete ${restoreDataPath}/efi ${rootpath}/boot >/dev/null 2>>$PLOGFILE
        if [ $? -ne 0 -a $? -ne 24 -a $? -ne 23 ]; then
            echo "System restoring failed, please reboot your system!"
            echo "System restoring failed, please reboot your system!" >>$PLOGFILE
            exit 9
        fi
    elif [ -d ${restoreDataPath}/boot/efi ]; then
        rsync -avAXHr --no-inc-recursive --ignore-missing-args --delete ${restoreDataPath}/boot/efi ${rootpath}/boot >/dev/null 2>>$PLOGFILE
        if [ $? -ne 0 -a $? -ne 24 -a $? -ne 23 ]; then
            echo "System restoring failed, please reboot your system!"
            echo "System restoring failed, please reboot your system!" >>$PLOGFILE
            exit 9
        fi
    fi

    echo "Begin to restore other directories..."
    echo "Begin to restore other directories..." >>$PLOGFILE
    #保留用户数据还原
    if [[ x${m_isRetainUserData} = x"true" ]]; then
        echo "保留用户数据还原" >> ${rootpath}/var/log/backup.log
        # 用户数据目录或文件
        if [ -e "${rootpath}/var/lib/biometric-auth" ]; then
            excludes="${excludes} --exclude=/var/lib/biometric-auth"
        fi
        if [ -e "${rootpath}/data/sec_storage_data" ]; then
            excludes="${excludes} --exclude=/data/sec_storage_data"
        fi
        if [ -e "${rootpath}/etc/passwd" ]; then
            excludes="${excludes} --exclude=/etc/passwd"
        fi
        if [ -e "${rootpath}/etc/shadow" ]; then
            excludes="${excludes} --exclude=/etc/shadow"
        fi
        if [ -e "${rootpath}/etc/group" ]; then
            excludes="${excludes} --exclude=/etc/group"
        fi
        if [ -e "${rootpath}/etc/gshadow" ]; then
            excludes="${excludes} --exclude=/etc/gshadow"
        fi
        if [ -e "${rootpath}/etc/sudoers" ]; then
            excludes="${excludes} --exclude=/etc/sudoers"
        fi
        excludes="${excludes} --exclude=/home --exclude=/root --exclude=/var/lib/AccountsService"
        #下面是域用户相关信息，保留用户数据还原后不退域
        excludes="${excludes} --exclude=/etc/sssd --exclude=/var/lib/sss --exclude=/usr/share/sssd --exclude=/etc/ipa --exclude=/etc/krb5.keytab"
        excludes="${excludes} --exclude=/etc/krb5.conf --exclude=/var/lib/ipa-client --exclude=/etc/nsswitch.conf --exclude=/etc/pam.d --exclude=/etc/hosts"
        excludes="${excludes} --exclude=/etc/hostname --exclude=/etc/hedron --exclude=/etc/kcm --exclude=/usr/hedron/hedronagent --exclude=/etc/.kyinfo --exclude=/etc/LICENSE"
        excludes="${excludes} --exclude=/etc/ssl/certs --exclude=/usr/share/ca-certificates --exclude=/etc/NetworkManager --exclude=/var/lib/pam"
        excludes="${excludes} --exclude=/etc/kim --exclude=/var/lib/kim --exclude=/etc/systemd/system/multi-user.target.wants/kimbackend.service"
        excludes="${excludes} --exclude=/var/lib/polkit-1/localauthority/40-domain.d --exclude=/lib/*/libsss_sudo.so"
        excludes="${excludes} --exclude=/usr/share/applications/kylin-os-installer.desktop"
        excludes="${excludes} --exclude=*/.local/share/applications/kylin-os-installer.desktop"
        excludes="${excludes} --exclude=/etc/xdg/autostart/kylin-os-installer.desktop"
        

        #如果是990，排除/data；否则，排除/data/usershare
        #if [ x${is_990_9a0} == x"true" ]; then
            excludes="${excludes} --exclude=/data"
        #else
        #    excludes="${excludes} --exclude=/data/usershare"
        #fi
        #如果是出厂备份的还原，还需要保留语言和时区配置
        # if [[ ${uuid} = "{${factory_uuid}}" && x${is_990_9a0} != x"true" ]]; then
        if [  "${m_isFactory}" = "true" ]; then
            if [ -e "${rootpath}/etc/localtime" ]; then
                excludes="${excludes} --exclude=/etc/localtime"
            fi
            if [ -e "${rootpath}/usr/share/zoneinfo" ]; then
                excludes="${excludes} --exclude=/usr/share/zoneinfo"
            fi
            if [ -e "${rootpath}/etc/default/locale" ]; then
                excludes="${excludes} --exclude=/etc/default/locale"
            fi
            if [ -e "${rootpath}/usr/share/i18n" ]; then
                excludes="${excludes} --exclude=/usr/share/i18n"
            fi

            if [ -e "${rootpath}/etc/lightdm/lightdm.conf" ]; then
                excludes="${exclude} --exclude=/etc/lightdm/lightdm.conf"
            fi
        fi

        # 系统更新的还原不还原下面目录
        if [ "${uuid}" = "{01234567-0123-0123-0123-0123456789ab}" ]; then
            #  excludes="${excludes} --exclude=/var/lib/kylin-system-updater"
            #  excludes="${excludes} --exclude=/var/cache/kylin-system-updater"
            excludes="${excludes} --exclude=/var/lib/kylin-system-updater/remain"
        fi
    fi
    # 兼容以前的老备份数据，后面可以尝试去掉此条件的逻辑
    # 出厂还原时，还原文件/etc/uid_list，其它场景不还原:等保四级标准操作系统使用周期内有uid唯一性的要求
    if [ "${m_isFactory}" != "true" ]; then
    	if [ ! -e "${restoreDir}/data/etc/uid_list" ]; then
    		excludes="${excludes} --exclude=/etc/uid_list"
    	fi
    fi
    if [ ! -e "${restoreDir}/data/boot/efi" ]; then
        excludes="${excludes} --exclude=/boot/efi"
    fi
    excludes="${excludes} --exclude=/var/log"
    excludes="${excludes} --exclude=*/backup/snapshots"
    excludes="${excludes} --exclude=/data/security-dir"
    # 还原后仍然保持激活状态
    excludes="${excludes} --exclude=/etc/LICENSE --exclude=/etc/.kyinfo --exclude=/etc/.kyactivation --exclude=/etc/.kyhwid"
    # 还原保留kylin-backup-server包安装的程序
    excludes="${excludes} --exclude=/usr/bin/kylin-backup-daemon --exclude=/usr/bin/backup_mount_fstab --exclude=/usr/bin/restore-system"
    excludes="${excludes} --exclude=/usr/share/initramfs-tools/hooks/kylin-backup-hooks"
    excludes="${excludes} --exclude=/usr/share/initramfs-tools/scripts/init-bottom/kylin-backup"
    excludes="${excludes} --exclude=/usr/share/initramfs-tools/conf-hooks.d/kylin-backup"
    excludes="${excludes} --exclude=/etc/extra_backup_or_restore.conf"
    excludes="${excludes} --exclude=/usr/share/plymouth"
    #yi jian huan yuan
    if [ ! -e "${restoreDir}/data/data" ]; then
        #这两行要一致
        echo "rsync -avAXHr --no-inc-recursive --ignore-missing-args --delete --exclude=/data ${excludes}  --exclude-from $restoreDir/$PERSONAL_EXCLUDEFILE ${restoreDataPath}/ $rootpath" >>${rootpath}/var/log/backup.log 2>&1
        rsync -avAXHr --no-inc-recursive --ignore-missing-args --delete --exclude=/data ${excludes} --exclude-from $restoreDir/$PERSONAL_EXCLUDEFILE "${restoreDataPath}/" $rootpath >>${rootpath}/var/log/backup.log 2>&1

    else
        #这两行要一致
        echo "rsync -avAXHr --no-inc-recursive --ignore-missing-args --delete ${excludes} --exclude-from=$restoreDir/$PERSONAL_EXCLUDEFILE ${restoreDataPath}/ $rootpath" >>${rootpath}/var/log/backup.log 2>&1
        rsync -avAXHr --no-inc-recursive --ignore-missing-args --delete ${excludes} --exclude-from=$restoreDir/$PERSONAL_EXCLUDEFILE "${restoreDataPath}/" $rootpath >>${rootpath}/var/log/backup.log 2>&1

    fi

    if [ $? -ne 0 -a $? -ne 24 -a $? -ne 23 ]; then
        echo "System restoring failed, please reboot your system!"
        echo "System restoring failed, please reboot your system!" >>$PLOGFILE
        exit 9
    fi

    echo "restore other directories end"
    echo "restore other directories end" >>$PLOGFILE

    # 还原额外数据  
	local restoreExtraDataPath="${restoreDir}/extra_data" 
    local PERSONAL_EXARTALFILE=".user_extra.txt"
    if [ -e "${restoreExtraDataPath}" ]; then
        if [ -e ${restoreDir}/${PERSONAL_EXARTALFILE} ]; then
            echo "restore extra data"
            echo "restore extra data" >>$PLOGFILE
            
            #这两行要一致
            echo "rsync -avAXHr --no-inc-recursive --ignore-missing-args --delete --files-from=${restoreDir}/${PERSONAL_EXARTALFILE} ${restoreExtraDataPath}/ $rootpath" >>${rootpath}/var/log/backup.log 2>&1
            rsync -avAXHr --no-inc-recursive --ignore-missing-args --delete --files-from=${restoreDir}/${PERSONAL_EXARTALFILE} "${restoreExtraDataPath}/" $rootpath >>${rootpath}/var/log/backup.log 2>&1

        fi
    fi

    sync
    
    # TODO : 解决问题：系统更新后，grub系统还原(桌面系统还原没有问题)，然后自动引导不起来，需要手动引导系统
    #       问题1——grub-install命令的--target参数不同架构不一样；
    #       问题2——命令执行报错：failed to register the EFI boot entry: Operation not permitted.
    #       问题3——未知
    #  由于问题不常见，且桌面系统还原没有问题，暂时不修改grub还原部分
    device_boot=$(mount | grep /boot | grep -v /efi | awk '{print $1}')
    device_efi=$(mount | grep /boot/efi | awk '{print $1}')
    echo "device_boot=${device_boot} device_efi=${device_efi}" >>$PLOGFILE
    device_boot=${device_boot#${rootmnt}}
    device_efi=${device_efi#${rootmnt}}
    echo "After chroot: device_boot=${device_boot} device_efi=${device_efi}" >>$PLOGFILE
    if [ "x${device_efi}" = "x" ]; then
        echo "efi partition not mounted, please check if loss vfat type filesystem module"
        echo "efi分区未挂载，请检查内核或initrd中是否缺少vfat文件系统类型模块"  >>$PLOGFILE
    else
        if [ "x${device_boot}" != "x" ]; then
            echo "grub-install -v --force  --boot-directory=/boot/ --efi-directory=/boot/efi/ ${device_boot}"  >>$PLOGFILE
            chroot ${rootmnt} /bin/bash -c "mount -t proc proc /proc"
            chroot ${rootmnt} /bin/bash -c "mount -t sysfs sys /sys"
            chroot ${rootmnt} /bin/bash -c "mount -t devtmpfs devtmpfs /dev"
            chroot ${rootmnt} /bin/bash -c "mount -t devpts devpts /dev/pts"
            chroot ${rootmnt} /bin/bash -c "grub-install -v --boot-directory=/boot/ --efi-directory=/boot/efi/ ${device_boot}"
	    chroot ${rootmnt} /bin/bash -c "update-grub"
            if [ $? -ne 0 ]; then
                echo "chroot run 'grub-install' error!"
                echo "chroot run 'grub-install' error!" >>$PLOGFILE
            else
                echo "chroot run 'grub-install' ok!"
                echo "chroot run 'grub-install' ok!" >>$PLOGFILE
                sync
            fi
        fi
    fi

    if [[ x${m_isRetainUserData} = x"true" && "${m_isFactory}" = "true" ]]; then
          rm -rf ${rootpath}/lib/systemd/system/kylin-os-installer.service
          rm -rf ${rootpath}/usr/bin/kylin-os-installer
          rm -rf ${rootpath}/usr/bin/kylin-os-installer-dm
          rm -rf ${rootpath}/usr/bin/kylin-os-installer-live
          rm -rf ${rootpath}/usr/bin/kylin-os-installer-oem
          rm -rf ${rootpath}/usr/bin/kylin-os-installer-oem-automatic
          rm -rf ${rootpath}/usr/bin/kylin-os-installer-wrapper
          rm -rf ${rootpath}/usr/share/doc/kylin-os-installer*
          rm -rf ${rootpath}/usr/share/kylin-os-installer*
          rm -rf ${rootpath}/usr/share/lightdm/lightdm.conf.d/96-kylin-os-installer.conf
          rm -rf ${rootpath}/home/*/.config/customAudio.xml
        # #这两行要一致
        #   local restoreExtraDataFile="${rootpath}/home/*/.config/customAudio.xml";
        #   local restoreFactoryExtraDataPath="${rootpath}/home/*/config";
        #   echo "rsync -avAXHr --no-inc-recursive --ignore-missing-args --delete=$restoreExtraDataFile $restoreFactoryExtraDataPath/  $rootpath" >>${rootpath}/var/log/backup.log 2>&1
        #   rsync -avAXHr --no-inc-recursive --ignore-missing-args --delete=$restoreExtraDataFile "$restoreFactoryExtraDataPath/" $rootpath >>${rootpath}/var/log/backup.log 2>&1
 
   fi
#        echo 'chroot ${rootpath} /bin/bash -c "apt remove kylin-os-installer -y"'  >>$PLOGFILE
#        chroot ${rootpath} /bin/bash -c "apt remove kylin-os-installer -y"
#    fi

    sync
}

#see backup-auto/autobackup.cpp
restoreFactory() { #还原
    local xxx
    local uuid

    m_default_uuid="{00000000-0000-0000-0000-000000000000}"

    #写日志文件
    local m_time=$(date "+%y-%m-%d %H:%M:%S" | tr -d "\n")
    writeLogFile "${m_time},${uuid},4,grub系统还原,,,${lastbackupname}" #grub时只有一键还原，没有增量还原

    restoreDir="${m_backupPath}/snapshots/${uuid}"
    restoreDataPath="${restoreDir}/data"


    if [ ! -e "${FactoryRepoPath}" ]; then
        echo "full restore directory not exists!"
        exit 7 #出厂备份文件不存在，不能还原系统
    fi
	
	#读取配置文件
    metainfo_file="${m_backupPath}/snapshots/$METAINFO"
    #先判断备份文件状态是否是正常备份完成，这时候的状态是0或者backup failed
	local STATE=`cat ${metainfo_file}|grep STATE |awk -F "=" '{print $2}'`
	echo $STATE
	if [ "$STATE" = "backup finished" ]; then
        echo "备份点状态正常！"
    else
		echo "备份点状态异常！"
        exit 14
    fi
	
	#获取备份点对应的哈希
	local ETC=`cat ${metainfo_file}|grep ETC |awk -F "=" '{print $2}'`
	local BOOT=`cat ${metainfo_file}|grep BOOT |awk -F "=" '{print $2}'`
	local HOME=`cat ${metainfo_file}|grep HOME |awk -F "=" '{print $2}'`
	
	echo $ETC
	echo $BOOT
	echo $HOME
	
	#如果temp不存在，则创建该目录
	local TEMP="/data/.backup/temp_dir"
	mkdir -p ${TEMP}
	if [ $? -ne 0 ]; then
		echo "Could not create /data/.backup/temp_dir in initrd!"
		exit 20
	fi
	
	#导出数据到临时目录
	ostree  checkout  --repo=${FactoryRepoPath} $ETC ${TEMP}/etc
	ostree  checkout  --repo=${FactoryRepoPath} $BOOT ${TEMP}/boot
	ostree  checkout  --repo=${FactoryRepoPath} $HOME ${TEMP}/home

	#执行同步过程
	rsync -avAHXr --info=progress2 --no-inc-recursive --ignore-missing-args --delete ${TEMP}/boot/ /boot
	rsync -avAHXr --info=progress2 --no-inc-recursive --ignore-missing-args --delete ${TEMP}/home/ /home
	rsync -avAHXr --info=progress2 --no-inc-recursive --ignore-missing-args --delete ${TEMP}/etc/ /etc
	
	#删除临时文件
	rm -fr ${TEMP}
	
	writeLogFile "=================================出厂还原结束===========================================" 
	sync
	exit 0



    
    
    # TODO : 解决问题：系统更新后，grub系统还原(桌面系统还原没有问题)，然后自动引导不起来，需要手动引导系统
    #       问题1——grub-install命令的--target参数不同架构不一样；
    #       问题2——命令执行报错：failed to register the EFI boot entry: Operation not permitted.
    #       问题3——未知
    #  由于问题不常见，且桌面系统还原没有问题，暂时不修改grub还原部分
    device_boot=$(mount | grep /boot | grep -v /efi | awk '{print $1}')
    device_efi=$(mount | grep /boot/efi | awk '{print $1}')
    echo "device_boot=${device_boot} device_efi=${device_efi}" >>$PLOGFILE
    device_boot=${device_boot#${rootmnt}}
    device_efi=${device_efi#${rootmnt}}
    echo "After chroot: device_boot=${device_boot} device_efi=${device_efi}" >>$PLOGFILE
    if [ "x${device_efi}" = "x" ]; then
        echo "efi partition not mounted, please check if loss vfat type filesystem module"
        echo "efi分区未挂载，请检查内核或initrd中是否缺少vfat文件系统类型模块"  >>$PLOGFILE
    else
        if [ "x${device_boot}" != "x" ]; then
            echo "grub-install -v --force  --boot-directory=/boot/ --efi-directory=/boot/efi/ ${device_boot}"  >>$PLOGFILE
            chroot ${rootmnt} /bin/bash -c "mount -t proc proc /proc"
            chroot ${rootmnt} /bin/bash -c "mount -t sysfs sys /sys"
            chroot ${rootmnt} /bin/bash -c "mount -t devtmpfs devtmpfs /dev"
            chroot ${rootmnt} /bin/bash -c "mount -t devpts devpts /dev/pts"
            chroot ${rootmnt} /bin/bash -c "grub-install -v --boot-directory=/boot/ --efi-directory=/boot/efi/ ${device_boot}"
            chroot ${rootmnt} /bin/bash -c "update-grub"
            if [ $? -ne 0 ]; then
                echo "chroot run 'grub-install' error!"
                echo "chroot run 'grub-install' error!" >>$PLOGFILE
            else
                echo "chroot run 'grub-install' ok!"
                echo "chroot run 'grub-install' ok!" >>$PLOGFILE
                sync
            fi
        fi
    fi

    sync
}

#-----------------------------------------------------------------
#see backup-auto/autobackup.cpp
updateBackupAutoFinishedState() {
    echo "this is updateBackupAutoFinishedState"
}

restoreBackupSelf() {
    restoreDir="${rootpath}/var/log/yhkylin-backup-tools"
    echo $restoreDir

    if [ ! -e "$restoreDir" ]; then
	echo "restoreBackupSelf error: no files"
	return 1
    fi
    
    # 还原rsync/kybackup/backup-daemon等程序
    rsync -avAXHr ${restoreDir}/backup-daemon ${rootpath}/usr/bin/ >> ${rootpath}/var/log/backup.log 2>&1
    rsync -avAXHr ${restoreDir}/kybackup ${rootpath}/usr/bin/ >> ${rootpath}/var/log/backup.log 2>&1

    rm -rf ${restoreDir}
}

#-----------------------------------------------------------------
#--------主程序从这里开始-----------------------------------------

mkdir -p ${rootpath}/var/log

writeLogFile "==============================================================================================" 
writeLogFile "=================================备份和还原过程开始===========================================" 
writeLogFile "==============================================================================================" 

writeLogFile "参数:" 
writeLogFile $* 
echo "m_isRetainUserData=" $m_isRetainUserData >> ${rootpath}/var/log/backup.log

# 临时关闭安全，因为有些文件安全保护为只读，造成备份还原无法操作
#sm_status=$(cat /sys/kernel/security/kysec/sm)
#file_protect=$(cat /sys/kernel/security/kysec/fpro)
#echo "文件保护状态：${file_protect} sm_status=${sm_status}" >> ${rootpath}/var/log/backup.log
#echo 0 > /sys/kernel/security/kysec/sm
#echo 0 > /sys/kernel/security/kysec/fpro

if [ $backupORrestore = "--autobackup" ]; then
    #backupAuto #备份, grub时只有全盘备份，没有增量备份
    #updateBackupAutoFinishedState
    #umountBackup

    echo "This is autobackup"

elif [ $backupORrestore = "--autorestore" ]; then
    #mountBackup
    #mount >>$PLOGFILE
    #restoreAuto #还原, grub时只有一键还原，没有增量还原
    #umountBackup
    echo "This is autorestore"

elif [ $backupORrestore = "--factorybackup" ]; then
    m_isFactory=true
    backupAuto              #备份, grub时只有全盘备份，没有增量备份
    updateBackupAutoFinishedState
    #umountBackup
elif [ $backupORrestore = "--restoreretainuserdata" ]; then
    #m_isRetainUserData=true
    #mountBackup
    #mount >>$PLOGFILE
    #restoreAuto
elif [ $backupORrestore = "--factoryrestore" ]; then
    m_isFactory=true
    restoreFactory #还原, grub时只有一键还原，没有增量还原
    #umountBackup

    echo "This is factorybackup"
elif [ $backupORrestore = "--restorebackupself" ]; then
    # 出厂备份安全没有打标记，恢复出厂后再进行其它系统还原就会造成标记丢失，无法进入系统，故不在此进行操作
    #restoreBackupSelf
    echo "Do nothing"
else
    echo "Not correct command"
    #exit 18
fi

sync

#echo ${file_protect} > /sys/kernel/security/kysec/fpro
#echo ${sm_status} > /sys/kernel/security/kysec/sm
# 还原后需要安全重新打标记，设置如下
#echo 0 > /etc/kysec/tmp/.status
#echo 0 > ${rootpath}/etc/kysec/tmp/.status

exit 0
