1:目标
实现在图像化界面输入需要备份的源文件路径、目标路径,定时的时间、然后通过输入的信息,把需要备份的源文件打包放到指定的目标路径下以执行定时任务的时间为子目录
把/shell/l.txt文件每分钟备份打包一次,放到/lile目录下面
1)输入源文件路径(必须为绝对路径)
![](https://images2015.cnblogs.com/blog/746846/201703/746846-20170313151549916-1973567534.png)
2)输入目标目录
![](https://images2015.cnblogs.com/blog/746846/201703/746846-20170313151551526-848416792.png)
3)输入定时的时间
![](https://images2015.cnblogs.com/blog/746846/201703/746846-20170313151551901-1102189881.png)
实现效果:
![](https://images2015.cnblogs.com/blog/746846/201703/746846-20170313151552791-1587140595.png)
脚本文件:
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) The 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[2Je[1;1H" exit 1; elif [ $result -eq 255 ] ;then echo -e "e[2Je[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) The 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[2Je[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: The 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[2Je[1;1H" exit 255; fi crontab_exec } crontab_exec(){ cat /tmp/crontab |tr " " " " > /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 "
" " " 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.