shell 多进程打包备份脚本

#!/bin/sh
##############################################
#                                                                                                                    #
#                                                                                                                    #
#     tar the all backup data and saved                                                  #
#       in backuptar directory                                                                      #
#                                                                                                                    #
##############################################

DATE=`date +%Y%m%d`
DATE_START=`date +%Y%m%d-%H:%M:%S`
Mail="[email protected]"
LogName=`echo $0 | cut -d. -f1`
pid_file="/tmp/$LogName.pid"
BACKUP_DIR="file1 file2 file3"


tmp_fifofile="/tmp/$$.fifo"

function pid_check(){
###check the rsync scrpits have finished
count=0
        for i in 1 2 3
        do
                if [ -f "/tmp/rsync_backup.pid" ];then
                        sleep 60
                        ((count++))
                fi
        done
        if [ $count -eq 3 ];then
                echo -e "Rsync the backup files maybe have problesm,the cn_backup.pid exits." | mail -s "Tarball failure,can not continue `hostnae -i`" $Mail
                exit 43
        fi
}

function pid_lock(){
        if [ -f "$pid_file" ];then
                backup_tar_pid=`cat $pid_file`
                echo -e "Tarball failure for Backup Server: \n
                        PID:     $backup_tar_pid
                        DATE:    $DATE
                        Script:  $0 " | mail -s "Tarball failure for Backup Server `hostname -i`" $Mail
                echo "Tarball failure" >> /home/opsuser/backup_log/${LogName}_$DATE.log
                exit 44
        else
                echo $$ > $pid_file
        fi
}

function pid_free(){
        rm $pid_file
        if [ -f "$pid_file" ];then
                echo "$pid_file exist, please check the pid file" >> /home/opsuser/backup_log/${LogName}_$DATE.log
        fi
}


function Tarball(){
        mkfifo $tmp_fifofile
        exec 6<>$tmp_fifofile
        rm $tmp_fifofile
        Num=`echo $BACKUP_DIR | tr ' ' '\n' | wc -l`

        thread=8
        for ((i=0;i < $thread;i++))
        do
                echo
        done >& 6

        for i in $BACKUP_DIR
        do
                read -u6
        {
                sleep 3
                Name=`echo ${i##*/}`
                cd $i
                if [ $i = "/data/config_bak/httpd_201" ]
                then
                        tar zcf ${Name}_${DATE}.tgz backup --exclude downloads
                else
                        tar zcf ${Name}_${DATE}.tgz backup
                fi
                #ls -l ${Name}_${DATE}.tgz              #It is using for test

                echo >& 6
        } &
        done

        wait

        exec 6>&-

        DATE_END=`date +%Y%m%d-%H:%M:%S`

        echo "Make tarball SUCCEED on $DATE" >> /home/opsuser/backup_log/${LogName}_$DATE.log
        echo -e "Tarball succeed for Backup Server: \n
                 PID:   $$
                 DATE:  $DATE \n
                 START: $DATE_START 
                 END:   $DATE_END \n" > /tmp/backuptar.mail

}

function SendMail(){
        cat /tmp/backuptar.mail | mail -s "Tarball SUCCEED for Backup Server Data `hostname -i`" $Mail
        rm /tmp/backuptar.mail
}

pid_check
pid_lock
Tarball
SendMail
pid_free


你可能感兴趣的:(shell,备份,多进程)