备份软件配置的脚本

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
numbchild
帖子: 107
注册时间: 2010-03-16 12:34
送出感谢: 0
接收感谢: 0

备份软件配置的脚本

#1

帖子 numbchild » 2011-01-24 2:11

通常我都有好几个文件需要备份配置,还不会用svn,github,啥的,暂时用脚本啦,况且没有联网的情况下,使用本地也是相当重要的。
下面是我的脚本的所有代码,有点乱,只是记录下了一些思路,其中有不同的方法来实现判断识别不同的配置文件位置以及文件名,

代码: 全选

#!/bin/bash
# Author: chris M. Sprite
#Mail: (numbchild@gmail.com)
#Notice: you can not use my file without my permit !
#Description:  backup all config file in your home

used to exercise shell script
post on ubuntu forums

# go home first
cd ~

# backup some scripts and bin
script_dir=~/scripts/
bin_dir=~/bin/
[ -d $script_dir ] && cp -r ~/scripts ~/backup_config/
[ -d $bin_dir ] && cp -r ~/bin ~/backup_config/

# make sure destination direction
backup_dir=~/backup_config/
if [ -d $backup_dir ]; then
    echo "backup_dir exist"
else
    mkdir ~/backup_config/
fi

# backup function
backup() {
    if [ $name!=$execption ] ; then
        # divide into two backup method
        backup_1
    else
        backup_2
    fi
}

backup_1() {
    rm -rf $backup_dir/$name
    cp -r $1 $backup_dir
    echo "backuping $1 into $backup_dir  ..."
}

backup_2() {
    case $1 in 
        # here can $1 get the first argument from function backup_2
        xchat)
            b_xchat ;;
        *)
            echo "Oops" ;;
    esac
}

# exception list s backup independent backup method.
b_xchat() {
    version=`xchat -v | cut -d . -f 1 | cut -d ' ' -f 2`
    rm -rf $backup_dir/$name
    cp -r ${name}$version $backup_dir
    echo "backuping $name into $backup_dir ..."
}

# namelist, easy to add softwares
namelist[*]=(vim jumanji chromium irssi mutt conky bash wget moc mplayer\
 elinks fluxbox awesome fvwm liferea lynx ssh thunderbird stardict\ vidalia vimperator w3m xchat2 fcitx uzbl zathura autostart)
# exception list
exception[*]=(xchat2)


# config locale, you need to change dir depend on your own environment.
#include rc file
vim_dir=~/.$name ~/.${name}rc
mutt_dir=~/.$name ~/.${name}rc
conky_dir=~/.$name ~/.${name}rc
bash_dir=~/.${name}rc
wget_dir=~/.${name}rc
# no rc file
jumanji_dir=~/.config/$name
chromium_dir=~/.config/${name}
fcitx_dir=~/.config/$name
uzbl_dir=~/.config/$name
zathura_dir=~/.config/$name
autostart_dir=~/.config/$name
irssi_dir=~/.$name
moc_dir=~/.$name
mplayer_dir=~/.$name
elinks_dir=~/.$name
fluxbox_dir=~/.$name
awesome_dir=
fvwm_dir=~/.$name
liferea_dir=
ssh_dir=~/.$name
thunderbird_dir=~/.$name
stardict_dir=~/.$name
vidalia_dir=~/.$name
vimperator_dir=~/.$name
w3m_dir=~/.$name
xchat2_dir=~/.$name # xchat2 s command "name" is xchat not xchat2
# all upper list can change into bellowing sentence
if [ -d ~/.$name ] ; then
    if [ -e ~/.${name}rc ] ; then
        backup ${name}rc $backup_dir
        echo "backuping ${name}rc to $backup_dir ..."
    else
        echo "Oops: no ${name}rc file found.."
        backup $name $backup_dir
        echo "backuping ${name} to $backup_dir ..."
    else
        echo "Oops"
    fi
else
    if [ -d ~/.config/$name ]; then
        if [ -e ~/.${name}rc ] ; then
            backup ${name}rc $backup_dir
            echo "backuping ${name}rc to $backup_dir ..."
        else
            echo "Oops: no ${name}rc file found.."
            backup $name $backup_dir
            echo "backuping ${name} to $backup_dir ..."
        fi
    fi
else
    echo "Oops...."
fi
# or use elif
if [ -d ~/.$name ] ; then
    if [ -e ~/.${name}rc ] ; then
        backup ${name}rc $backup_dir
        echo "backuping ${name}rc to $backup_dir ..."
    else
        echo "Oops: no ${name}rc file found.."
        backup $name $backup_dir
        echo "backuping ${name} to $backup_dir ..."
    else
        echo "Oops"
    fi
elif [ -d ~/.config/$name ]; then
    if [ -e ~/.${name}rc ] ; then
        backup ${name}rc $backup_dir
            echo "backuping ${name}rc to $backup_dir ..."
        else
            echo "Oops: no ${name}rc file found.."
            backup $name $backup_dir
            echo "backuping ${name} to $backup_dir ..."
        fi
    fi
else
    # here else is error.
    echo "Oops...."
fi

    


# loop
for name in ${namelist[*]} ; do
    # test whether you have installed this sprite.
    if [ -n `which $name` ]; then
        backup $name
        how to let array ++ ?
        let name++
    else
    echo "Oops:'_' sprite $name does not exist ."
fi
done


# recover config file
recover() {
    rm -rf $local_dir
    cp $backup_dir/$name $local_dir
    echo "recovering $name to $local_dir ..."
}

for name in ${namelist[*]} ; do
    # test whether you have installed this sprite.
    if [ -n `which $name` ]; then
        recover $name
        how to let array ++ ?
        let name++
    else
    echo "Oops:'_' sprite $name does not exist ."
fi
done
头像
millenniumdark
论坛版主
帖子: 4159
注册时间: 2005-07-02 14:41
系统: Ubuntu 14.04 (Kylin)
送出感谢: 42 次
接收感谢: 9 次
联系:

Re: 备份软件配置的脚本

#2

帖子 millenniumdark » 2011-02-15 18:24

还不会用svn,github,啥的,暂时用脚本啦,况且没有联网的情况下,使用本地也是相当重要的。
svn的确笨重。git/hg/bzr/darcs都支持本地仓库,不需要服务器的呀。

代码: 全选

used to exercise shell script
post on ubuntu forums
忘了加#了?

代码: 全选

# make sure destination direction
backup_dir=~/backup_config/
if [ -d $backup_dir ]; then
    echo "backup_dir exist"
else
    mkdir ~/backup_config/
fi
貌似一行就够了

代码: 全选

mkdir -p ~/backup_config/
下面的没看。
头像
numbchild
帖子: 107
注册时间: 2010-03-16 12:34
送出感谢: 0
接收感谢: 0

Re: 备份软件配置的脚本

#3

帖子 numbchild » 2011-02-15 18:36

millenniumdark 写了:
还不会用svn,github,啥的,暂时用脚本啦,况且没有联网的情况下,使用本地也是相当重要的。
svn的确笨重。git/hg/bzr/darcs都支持本地仓库,不需要服务器的呀。

代码: 全选

used to exercise shell script
post on ubuntu forums
忘了加#了?

代码: 全选

# make sure destination direction
backup_dir=~/backup_config/
if [ -d $backup_dir ]; then
    echo "backup_dir exist"
else
    mkdir ~/backup_config/
fi
貌似一行就够了

代码: 全选

mkdir -p ~/backup_config/
下面的没看。
谢谢你还看到我的这么搓的脚本,额,回头看实在是悲剧的。。。。。现在努力学习钟。。。。
回复

回到 “Shell脚本”