Linux命令行與shell腳本(20)--實例:備份文件

  • 創建一個配置文件,該文件包含了要備份的每個目錄或文件
$ cat files_backup_config 
/Users/chenhong/Desktop/shell_workspace/mysql.sh
/Users/chenhong/Desktop/shell_workspace/disk_used_view.sh
/Users/chenhong/Desktop/shell_workspace/file.sh
  • 編寫腳本
#!/bin/bash

date=`date +%y%m%d`
file=filebackup$date.tar.gz
config_file=`pwd`/files_backup_config;
destination=`pwd`/$file;

if [ -f $config_file ]
then
    echo "load $config_file";
else
    echo "Sorry,can not find config file $config_file";
    exit 1;
fi

file_number=0;
exec < $config_file;
read file_name;
while [ $? -eq 0 ]  # read命令執行結果爲0
do
    if [ -f $file_name -o -d $file_name ] #-o 表示or
    then
        file_list="$file_list "$file_name;
    else
        echo "$file_name does not exist";
        echo "the number is $file_number";
    fi
    file_number=$[ $file_number + 1 ];
    read file_name;
done

tar -czf $destination $file_list 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章