1:目標
實現在圖像化界面輸入需要備份的源文件路徑、目標路徑,定時的時間、然后通過輸入的信息,把需要備份的源文件打包放到指定的目標路徑下以執行定時任務的時間為子目錄
把/shell/l.txt文件每分鍾備份打包一次,放到/lile目錄下面
1)輸入源文件路徑(
必須為絕對路徑)

2)輸入目標目錄

3)輸入定時的時間

實現效果:

腳本文件:
1)實現圖形化界面以及把獲得的輸入信息放到指定的位置
#!/bin/bash input_sourcefile_location(){ dialog --title "Input you base information" --form "Please enter you information about mysql backup information: (Please input Absolutely location) \nThe source location is you need backups file" 10 70 3 "The source file location:" 1 1 "" 1 30 50 0 2> /tmp/source.location result=$? if [ $result -eq 1 ] ;then echo -e "\e[2J\e[1;1H" exit 1; elif [ $result -eq 255 ] ;then echo -e "\e[2J\e[1;1H" exit 255; fi input_destination_dir } input_destination_dir(){ dialog --title "Input you base information" --form "Please enter you information about mysql backup information: (Please input Absolutely location) \nThe distination dir (you only need input the dir,everydays back file will output the dir where use date is dir) is you need backups file" 15 70 3 "The destination dir location:" 1 1 "" 1 30 50 0 2> /tmp/destination_dir result=$? if [ $result -eq 1 ] ;then input_sourcefile_location; elif [ $result -eq 255 ] ;then echo -e "\e[2J\e[1;1H" exit 255; fi set_crontab_time } set_crontab_time(){ dialog --title "Input you base information" --form "Please enter you information about mysql backup information: \nThe crontab seting ,minutes,hours,day,mouth,week (eg:* */2 * * *) " 15 70 5 "The minutes set:" 1 1 "" 1 20 20 0 "The hours set:" 2 1 "" 2 20 20 0 "The day set:" 3 1 "" 3 20 20 0 "The month set:" 4 1 "" 4 20 20 0 "The week set:" 5 1 "" 5 20 20 0 2> /tmp/crontab result=$? echo $result > lll if [ $result -eq 1 ] ;then input_destination_dir elif [ $result -eq 255 ] ;then echo -e "\e[2J\e[1;1H" exit 255; fi crontab_exec } crontab_exec(){ cat /tmp/crontab |tr "\n" " " > /tmp/day sed -i 's/$/export DISPLAY=:0.0 ;gnome-terminal -x \/bin\/bash -c "\/shell\/2.sh 2>>\/tmp\/log" /g' /tmp/day cat /tmp/day >> /var/spool/cron/root echo >> /var/spool/cron/root } input_sourcefile_location
2)對輸入的信息進行處理
#!/bin/bash dirname=`date +%Y%m%d%H%M` destination_dir=`cat /tmp/destination_dir` source_file=`cat /tmp/source.location` source_dir=`echo ${source_file%/*}` file_name=`awk -F / '{print $NF}' /tmp/source.location` tar_file=$file_name.tar.gz if [ ! -d $destination_dir ] ;then mkdir -p $destination_dir fi mkdir $destination_dir/$dirname cd $source_dir tar -zcf $tar_file $file_name cp $tar_file $destination_dir/$dirname rm -rf $tar_file
遇到的坑:
1:腳本文件里寫的函數千萬要注意,不要用命令去做函數名,在第一個腳本的時候把crontab做為函數名了,怎么都是不對的,弄了一上午
2:crontab要注意的是,新建的crotab定時任務不會馬上執行,至少要兩分鍾
3:注意使用crontab file,如果你用crontab file,會把原來的定時任務全部給覆蓋掉
4:把一個文件的換行符替換成空格符:tr "\n" " " filename
5:echo ${source_file%/*}表示刪除/*以后的,%表示非貪婪匹配
6:crontab -e定時的任務是放在/var/spool/cron/root里
7:執行crontab的時候,報錯,在后面加上一行空行即可
"cronfile1":1: premature EOF
errors in crontab file, can"t install.