回精華區
作者vyvian.bbs@bbs.ee.ncnu.edu.tw (好山好水留子孫) 看板: plan
標題自動更新原始檔
時間暨大電機漂浮電子 (2004/03/16 Tue 20:42:14)
我寫了一隻script可以來自動檢查及更新~/src底下的檔案。

詳細的用法請參考本程式開頭的說明,適合少量自己patch的站。

分享給各位辛苦的站長。

下載點:

http://studentweb.ncnu.edu.tw/92323540/bash/fullupgrade.sh


#!/bin/bash
#       本程式提供BBS原始檔做全面性自動化更新的工作
#
# 本程式開發環境:Linux + bash 2.05b
#
# **程式原理如下:
#
#   1. 只檢查那些手動patch過的檔案和最新版本的檔案是否有不同,如果不同的
#      話則提出警告。
#   2. 並且讓管理者選擇修正的方式,可以用新版的或者用舊版的檔案。
#   3. 如果新版的檔案並沒有動過你自己patch過的那些檔案,則把所有舊的原始
#      碼換成最新版。
#   4. 在把你手動patch過的檔案覆蓋回去,重新compiler。
#
# **使用方法如下:
#
#   1. 如果是第一次使用,把所有曾經手動patch過的原始碼copy到 ~/manage/patch
#      這個資料夾
#   2. 檔案在做任何patch之前,先將原始檔先copy到~manage/src裡面
#       (同一個檔案只有在做第一次patch時才需要做這個動作。)
#
#   3. 將patch之後的檔案copy到~/manage/patch裡面
#       (上面這兩個動作非常重要,否則無法判斷那些檔案被你自己修改過。)
#       (請不要移除下載回來的壓縮檔,以做為版本判斷之用。)
#   4. 建立一個新目錄,Ex: /home/bbs/manage
#   5. 將本程式放在所建立的目錄內
#   6. 執行 /home/bbs/manage/fullupgrade.sh
#   7. 請確認您的系統有裝bash, sed, awk, lynx
#
# 對本程式有任何問題,歡迎mail給我   s2323540@ncnu.edu.tw
#
basedir=/home/bbs/manage    #設定工作目錄,也是本程式擺放的位置。
#
prog='bash tar gzip sed awk lynx'
for exim in $prog
do
    which $exim > /dev/null 2>&1
    if [ "$?" != "0" ]; then
        echo "您的系統沒有安裝$exim,請將$exim安裝到系統上。"
        exit
    fi
done
if [ ! -d "$basedir/src" ]; then
    mkdir -p $basedir/src
fi
if [ ! -d "$basedir/patch" ]; then
    mkdir -p $basedir/patch
fi
oldfile=`ls $basedir|grep MapleBBS`
file=`lynx --dump http://processor.tfcis.org | grep "PACK"|awk '{print $2}'`
filename=${file##*/}
if [ -f "$oldfile" ]; then
    tmp=${oldfile%-*}
    oldfiledate=${tmp##*-}
    tmp1=${filename%-*}
    newfiledate=${tmp1##*-}
    if [ "$oldfiledate" == "$newfiledate" ]; then
        echo "目前的原始檔是最新版本。"
        echo "It's already up to date."
    else
        rm $basedir/$oldfile
        rm -rf $basedir/bbs
        rm -rf $basedir/filechange
        lynx --dump "$file" > "$basedir"/"$filename"
        tar -zxf "$basedir"/"$filename"
        if [ $? -ne 0 ]; then
            echo "Cann't unzip source file"
            exit
        fi
    fi
else
    if [ -d "$basedir/bbs" ]; then
        rm -rf $basedir/bbs
    fi
    if [ -f "$basedir/filechange" ]; then
        rm -rf $basedir/filechange
    fi
    lynx --dump "$file" > "$basedir"/"$filename"
    tar -zxf "$basedir"/"$filename"
    if [ $? -ne 0 ]; then
        echo "Cann't unzip source file"
        exit
    fi
fi
files=`ls -l $basedir/src|grep "^-"|awk '{print $9}'`
for file in $files
do
    path=`find "$basedir/bbs/src" -type f -name "$file"`
    chksum1=`md5sum "$basedir/src/$file"|awk '{print $1}'`
    chksum2=`md5sum "$path"|awk '{print $1}'`
    if [ "$chksum1" == "" ]||[ "$chksum2" == "" ]; then
        echo "$file does not exit !!"
        echo "$file 在新版本中已經不存在了,您可以從系統中移除"
        echo -n "是否要移除$file \(yes/no\)?"
        read answer
        while [ ! "$answer" == "yes" ]&&[ ! "$answer" == "no" ]
        do
            echo -n "請回答yes 或 no:"
            read answer
        done
        if [ "$answer" == "yes" ]; then
            rm -f "$basedir/src/$file"
            rm -f "$basedir/patch/$file"
        else
            echo "$file尚未移除"
            echo " "
            flag=1
        fi
    else
        if [ ! "$chksum1" == "$chksum2" ]; then
            echo "$file is different !!"
            echo "$file 這個檔案您曾經patch過,而新的版本也修改過這個檔案。"
            echo "請參閱filechange,裡面會顯示原本的檔案和新版檔案的不同處"
            echo "請根據filechange裡面的資訊做適當的patch"
            echo "若您尚未看過filechange,下面的問題請都回答q)略過"
            echo -n '請選擇 1)用新的檔案 2)用自己patch的檔案 q)略過 [q]?'
            read answer1
        case $answer1 in
        1 )
            cp -f $path $basedir/src/$file
            if [ -f "$basedir/patch/$file" ]; then
                mv $basedir/patch/$file $basedir/patch/"$file".bak
            fi
            echo "Use new file" ;;
        2 )
            cp -f $path $basedir/src/$file
            echo "Use patched file" ;;
        * )
            flag=1
            echo "Ignore !!" ;;
        esac
            diff -c "$basedir/src/$file" "$path" >> $basedir/filechange
            echo " "    >> $basedir/filechange
            echo "====================================" >> $basedir/filechange
            echo " "    >> $basedir/filechange
            echo " "
        fi
    fi
done
if [ "$flag" == "1" ]; then
    echo "patch過的原始檔有做過修改"
    echo "您可以選擇用新版的程式,再手動加上patch。"
    echo "或者用原本patch過的檔案再手動加上新版的功能。"
    echo "請參考filechange的內容,修正之後再重新跑一次本程式"
else
    echo "所有的檢查都已經完成,您已經可以進行升級的動作....."
    echo "以下會把原始檔備份到/home/bbs/bak裡面,並替換成新的檔案"
    echo -n '您是否要進行升級的動作(yes/no)?'
    read check
    while [ ! "$check" == "yes" ]&&[ ! "$check" == "no" ]
    do
        echo -n "請回答yes 或 no:"
        read check
    done
    if [ "$check" == "no" ]; then
        echo "Stop Full upgrade."
        exit
    fi
    cd ~
    tar cf - ./src |gzip > ~/bak/src.tar.gz
    if [ $? -ne 0 ]; then
        echo "Backup source file Failure!!"
        exit
    fi
    cd $basedir
    rm -rf ~/src/*
    cp -r $basedir/bbs/src/* ~/src
    if [ $? -ne 0 ]; then
        echo "Error !! Can't not copy newfile to ~/src"
        exit
    fi

    patch=`ls -l $basedir/patch|grep "^-"|awk '{print $9}'`
    for file in $patch
    do
        judge=${file##*.}
        if [ ! "$judge" == "bak" ]; then
            path=`find ~/src -type f -name "$file"`
            cp -f $basedir/patch/$file $path
            if [ $? -ne 0 ]; then
                echo "Error !! Can't not copy patch file $file to $path"
                exit
            fi
        fi
    done
    echo "~/src裡面的檔案已更新,如果您沒有要進行任何patch,可立刻重新compiler"
    echo -n '您是否要馬上重新compiler?(yes/no)'
    read answer3
    while [ ! "$answer3" == "yes" ]&&[ ! "$answer3" == "no" ]
    do
        echo -n "請回答yes 或 no:"
        read answer3
    done
    if [ "$answer3" == "yes" ]; then
        cd ~/src
        make clean linux install
        echo " "
        echo " "
        echo "Full upgrated Successful. Plesae reboot your BBS."
        echo "升級完成,請將主機重新開機。"
    else
        echo "patch完之後可重新執行本程式來重新compiler"
    fi
fi

: ───────────────────────────────────── :

md5sum是為了檢查檔案是否被更動過,

如果你的站完全沒有自己手動修改過code的話,想直接完全用新版的code

可以用下面這支程式,這是精華區那隻的簡單版(不做手動patch的檢查)

http://bbs.ee.ncnu.edu.tw/download/update.sh

不過你的config.h應該要自己備份起來,然後再放回原本的目錄去編譯。

#!/bin/bash
#       本程式提供BBS原始檔做全面性自動化更新的工作
#本程式開發環境:Linux + bash 2.05b
#本程式最近更新日期 2004/9/24
#
# **使用方法如下:
#
#   1. 建立一個新目錄,Ex: /home/bbs/manage
#   2. 將本程式放在所建立的目錄內
#   3. 執行 /home/bbs/manage/update.sh
#   4. 請確認您的系統有裝bash, sed, awk, lynx
#   5. 萬一更新完之後copmile出問題,想換回原本的程式,請執行
#      /home/bbs/manage/update.sh rescue
basedir=/home/bbs/manage    #設定工作目錄,也是本程式擺放的位置。
#---------------------------------
#對本程式有任何問題,歡迎mail給我   s2323540@ncnu.edu.tw
funcrese () {
    rm -rf ~/src
    tar zxvf ~/bak/src.tar.gz -C ~
    if [ "$?" != "0" ]; then
        echo "已經換回原本的程式了"
        echo "如果您的~/bin裡面的檔案被改掉的話,請重新compile"
    fi
}
if [ "$1" == "rescue" ]; then
    echo -n '您是否要換回原本的程式(yes/no)?'
    read ans
    while [ ! "$ans" == "yes" ]&&[ ! "$ans" == "no" ]
    do
        echo -n "請回答yes 或 no:"
        read ans
    done
    if [ "$ans" == "yes" ]; then
        funcrese
    fi
    exit
fi
prog='bash tar gzip sed awk lynx'
for exim in $prog
do
    which $exim > /dev/null 2>&1
    if [ "$?" != "0" ]; then
        echo "您的系統沒有安裝$exim,請將$exim安裝到系統上。"
        exit
    fi
done
if [ ! -d "$basedir/src" ]; then
    mkdir -p $basedir/src
fi
if [ ! -d "$basedir/patch" ]; then
    mkdir -p $basedir/patch
fi
oldfile=`ls $basedir|grep MapleBBS`
file=`lynx --dump http://processor.tfcis.org | grep "PACK"|awk '{print $2}'|ta
il -1`
filename=${file##*/}
if [ -f "$oldfile" ]; then
    tmp=${oldfile%-*}
    oldfiledate=${tmp##*-}
    tmp1=${filename%-*}
    newfiledate=${tmp1##*-}
    if [ "$oldfiledate" == "$newfiledate" ]; then
        echo "目前的原始檔是最新版本。"
        echo "It's already up to date."
        rm -rf $basedir/filechange
    else
        mv $basedir/$oldfile ~/tmp/$oldfile
        rm -rf $basedir/bbs
        rm -rf $basedir/filechange
        echo "Downloading.........."
        lynx --dump "$file" > "$basedir"/"$filename"
        tar -zxf "$basedir"/"$filename"
        if [ $? -ne 0 ]; then
            echo "Cann't unzip source file"
            exit
        fi
    fi
else
    if [ -d "$basedir/bbs" ]; then
        rm -rf $basedir/bbs
    fi
    if [ -f "$basedir/filechange" ]; then
        rm -rf $basedir/filechange
    fi
    echo "Downloading........."
    lynx --dump "$file" > "$basedir"/"$filename"
    tar -zxf "$basedir"/"$filename"
    if [ $? -ne 0 ]; then
        echo "Cann't unzip source file"
        exit
    fi
fi
    echo "所有的檢查都已經完成,您已經可以進行升級的動作....."
    echo "以下會把原始檔備份到/home/bbs/bak裡面,並替換成新的檔案"
    echo -n '您是否要進行升級的動作(yes/no)?'
    read check
    while [ ! "$check" == "yes" ]&&[ ! "$check" == "no" ]
    do
        echo -n "請回答yes 或 no:"
        read check
    done
    if [ "$check" == "no" ]; then
        echo "Stop Full upgrade."
        exit
    fi
    cd ~
    tar cf - ./src |gzip > ~/bak/src.tar.gz
    if [ $? -ne 0 ]; then
        echo "Backup source file Failure!!"
        exit
    fi
    cd $basedir
    rm -rf ~/src/*
    cp -r $basedir/bbs/src/* ~/src
    if [ $? -ne 0 ]; then
        echo "Error !! Can't not copy newfile to ~/src"
        exit
    fi
    echo "~/src裡面的檔案都已經更新,如果您沒有要進行任何patch,可立刻重新compile
r"
    echo -n '您是否要馬上重新compiler?(yes/no)'
    read answer3
    while [ ! "$answer3" == "yes" ]&&[ ! "$answer3" == "no" ]
    do
        echo -n "請回答yes 或 no:"
        read answer3
    done
    if [ "$answer3" == "yes" ]; then
        cd ~/src
        make clean linux install
        if [ "$?" == "0" ]; then
            echo " "
            echo " "
            echo "Full upgrated Successful. Plesae reboot your BBS."
            echo "升級完成,請將主機重新開機。"
        else
            echo "程式編譯失敗,若要改回舊程式,請執行下列這一行"
            echo "/home/bbs/manage/fullupgrade.sh rescue"
        fi
    else
        echo "patch完之後可重新執行本程式來重新compiler"
    fi


--
  Origin:  暨大電機˙漂浮電子  bbs.ee.ncnu.edu.tw 
  Author: vyvian ip169.puli24.ncnu.edu.tw 發表
精華選讀←離開[主題上]主題下(k)上篇(j)下篇S/a搜尋 G串列 TAB精華 ↑↓捲 Pg/Space翻