#!/bin/sh

# Author: tianyukai 

#set -x

# Types: local_dev, ssh_server, samba_server, nfs_server

if [ "$#" -ne "1" ]

then

. /usr/share/drbl/sbin/drbl-conf-functions

. /etc/drbl/drbl-ocs.conf

. /usr/sare/drbl/sbin/ocs-functions

. /etc/ocs/ocs-live.conf

# Load language file

ask_and_load_lang_set en_US.UTF-8

fi

#Paramer setting 

Systemname=`hostname`

Hdisk=`cat /proc/cmdline|sed 's/hdisk=/:/'|gawk -F: '{print $3}'|sed 's/\"//g'`

Passwd=`cat /proc/cmdline|sed 's/passwd=/:/'|gawk -F: '{print $3}'|cut -d" " -f1`

Uuid=`cat /proc/cmdline|sed 's/uuid=/:/'|gawk -F: '{print $3}'|cut -d" " -f1`

Hwsn=$(dmidecode -s system-serial-number 2>/dev/null | awk '/^[^#]/ { print $1 }')

Ipaddr=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"|sed -n 1p`

Result=""

Mountpoint="/home/partimag"

Cdata=`date  +%Y%m%d%H%M`

Mountip="**.**.**.**"

Mountpasswd="Huawei12#$"

Mountuser="root"

MountSize=""

Vgname=VolGroup00

Title=""

Restore_version=""

IsBackup=""

Status=""

BackupName=""

IsRestory=""

RestoryName=""

Filename=""

Message=""

Delfilename=""

function error_info(){

[ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE

echo $* >> /tmp/install.log

[ -z "$Title" ] && Title="Default" 

[ -z "$IsBackup" ] && IsBackup="Default"

[ -z "$BackupName" ] && BackupName="Default"

[ -z "$IsRestory" ] && IsRestory="Default"

[ -z "$RestoryName" ] && RestoryName="Default"

curl -H "Content-Type: application/json" -X POST -d "{\"Sn\":\"$Hwsn\",\"IpAddr\":\"$Ipaddr\",\"HostName\":\"$Systemname\",\"Title\":\"$Title\",\"Status\":\"finish\",\"IsBackup\":\"$IsBackup\",\"BackupName\":\"$BackupName\",\"IsRestory\":\"$IsRestory\",\"RestoryName\":\"$RestoryName\",\"Passwd\":\"$Passwd\",\"Reason\":\"$*\",\"Result\":\"Failed\",\"Uuid\":\"$Uuid\"}" http://xxx.xxx.xxx.xxx:8083/api/osinstall/v1/device/backupRestory

umount $Mountpoint

[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL


}

function result_info(){

echo $* >> /tmp/install.log

        [ -z "$Title" ] && Title="Default"

[ -z "$Status" ] && Status="running"

        [ -z "$IsBackup" ] && IsBackup="Default"

        [ -z "$BackupName" ] && BackupName="Default"

        [ -z "$IsRestory" ] && IsRestory="Default"

        [ -z "$RestoryName" ] && RestoryName="Default"

        [ -z "$Result" ] && Result="Default" 

        curl -H "Content-Type: application/json" -X POST -d "{\"Sn\":\"$Hwsn\",\"IpAddr\":\"$Ipaddr\",\"HostName\":\"$Systemname\",\"Title\":\"$Title\",\"Status\":\"$Status\",\"IsBackup\":\"$IsBackup\",\"BackupName\":\"$BackupName\",\"IsRestory\":\"$IsRestory\",\"RestoryName\":\"$RestoryName\",\"Passwd\":\"$Passwd\",\"Reason\":\"$*\",\"Result\":\"$Result\",\"Uuid\":\"$Uuid\"}" http://xxx.xxx.xxx.xxx:8083/api/osinstall/v1/device/backupRestory

}

function env_check(){


if [ ! `cat /proc/cmdline |grep -o "backup\|restore"` ] 

then

Message="Paramer set Failed"

error_info $Message

exit 1

fi

if [ ! $Passwd ]

then

Message="Passwd set Failed"

error_info $Message

exit 1

fi 


}

#env_check

# 1. Mount the clonezilla image home.

# Types: local_dev, ssh_server, samba_server, nfs_server

if [ "$#" -eq "1" ]

then

Delfilename=$1

cmd="rm -fr "$Mountpoint/$Delfilename

sshpass -p $Mountpasswd ssh $Mountuser@$Mountip -o StrictHostKeyChecking=no $cmd

if [ "$?" -ne "0" ]

then

        exit 1

else

exit 0

fi


else

sshpass -p $Mountpasswd ssh $Mountuser@$Mountip -o StrictHostKeyChecking=no 'exit'

if [ "$?" -ne "0" ]

then

Message="Remote connection Failed"

        error_info $Message

        exit 1

fi

fi

#MountSize=`sshpass -p $Mountpasswd  ssh $Mountuser@$Mountip -o StrictHostKeyChecking=no "df -h |grep home|awk '{print \$4}' && exit"`

sshfs -o ssh_command='sshpass -p aaPA18ww ssh' -o cache=yes,allow_other $Mountuser@$Mountip:/home/partimag $Mountpoint

if [ "$?" -ne "0" ]

then

Message="The hardpoint could not be connected"

error_info $Message

exit 1

fi

env_check

cat /proc/cmdline|grep -o restore

if [ $? -eq "0" ]

then

sed -i '6703i ocs-lvm2-stop' /usr/share/drbl/sbin/ocs-functions

Title="Hard disk $Hdisk restore"

Restore_version=`cat /proc/cmdline|sed 's/restore_version=/:/'|gawk -F: '{print $3}'|cut -d" " -f1`

IsRestory="Yes"

RestoryName=$Restore_version

Message="restore is running"

result_info $Message

#echo "$Passwd"|/usr/sbin/ocs-sr -g auto -e1 auto -e2 -t -r -j2 -k -p cmd restoreparts $restore_version sda1 sda2 sda3 sda4

echo "$Passwd"|/usr/sbin/ocs-sr -g auto -e1 auto -e2 -t -r -j2 -k -p cmd restoreparts $Restore_version $Hdisk

if [ "$?" -eq "0" ]

then

str=`cat /var/log/clonezilla.log |grep "Failed to use partclone program to save or restore an image"`

str2=`cat /var/log/clonezilla.log |grep -i  "warning"`

str3=`cat /var/log/clonezilla.log |grep -i  "Failed"`

if [ "$str" ]

then

Message=`cat /var/log/clonezilla.log |grep -B1 "Failed to use partclone program to save or restore an image"|sed -n 1p`

                error_info $Message

                exit 1

fi

if [ "$str2" ]

        then

                Message=`cat /var/log/clonezilla.log |grep -i "warning"|sed -n 1p`

                error_info $Message

                exit 1


        fi

if [ "$str3" ]

                then

                        Message=`cat /var/log/clonezilla.log |grep -i "Failed"|sed -n 1p`

                        error_info $Message

                        exit 1


                fi

Status="finish"

Result="normal"

Message="Success Restore"

result_info $Message


else

# Message="Failed to Restore"

Message=`cat /var/log/clonezilla.log |grep -i "Failed\|warning"|sed -n 1p`

error_info $Message

exit 1

fi

fi


cat /proc/cmdline|grep -o backup

if [ $? -eq "0" ]

then

#if [ `echo $Hdisk|grep [1-9]|awk '{print $1}'` ]

        #then

        #        Title="Hard disk partition backup"

        #else

        #        Title="Hard disk all backup"

        #fi

Title="Hard disk $Hdisk backup"

IsBackup="Yes"

    Filename=`cat /proc/cmdline|sed 's/function=/:/'|gawk -F: '{print $3}'|cut -d" " -f1`

BackupName=$Filename

Message="backup is running"

result_info $Message

if [ -z $Filename ]

then

        Message="Filename set Failed"

error_info $Message

exit 1

fi

#echo "$Passwd

#$Passwd"|/usr/sbin/ocs-sr -q2 -j2 -z1p -i 4096 -sfsck  -enc -p cmd saveparts  $FILENAME sda1 sda2 sda3 sda4

echo "$Passwd

$Passwd"|/usr/sbin/ocs-sr -q2 -j2 -z1p -i 4096 -sfsck  -enc -p cmd saveparts  $Filename $Hdisk


    if [ "$?" -eq "0" ]

    then

str=`cat /var/log/clonezilla.log |grep  "Failed to use partclone program to save or restore an image"` 

str2=`cat /var/log/clonezilla.log |grep -i  "warning"`

str3=`cat /var/log/clonezilla.log |grep -i  "Failed"`

if [ "$str" ]

then

Message=`cat /var/log/clonezilla.log |grep -B1 "Failed to use partclone program to save or restore an image"|sed -n 1p`

error_info $Message

exit 1

fi

if [ "$str2" ]

        then

                Message=`cat /var/log/clonezilla.log |grep -i "warning"|sed -n 1p`

                error_info $Message

                exit 1


        fi

if [ "$str3" ]

        then

                Message=`cat /var/log/clonezilla.log |grep -i "Failed"|sed -n 1p`

                error_info $Message

                exit 1


        fi

Status="finish"

Result="normal"

Message="Success Backup"

result_info $Message


    else

    Message=`cat /var/log/clonezilla.log |grep -i "Failed\|warning"|sed -n 1p`

    error_info $Message

    exit 1

    fi

fi

vmware:

echo "mkisofs -relaxed-filenames -J -R -o ${file_isoks} -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -eltorito-alt-boot -b efiboot.img -no-emul-boot ${dir_isotmp}"

mkisofs -relaxed-filenames -J -R -o ${file_isoks} -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -eltorito-alt-boot -b efiboot.img -no-emul-boot ${dir_isotmp}

系统镜像功能_镜像备份linux:


系统镜像功能_镜像备份