shell脚本实现定时备份某文件
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.
作者:李先生
-------------------------------------------
个性签名:在平凡中坚持前行,总有一天会遇见不一样的自己!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!
微信公众号 微信打赏 支付宝打赏
posted on 2017-03-13 15:16 Captain_Li 阅读(7364) 评论(0) 编辑 收藏 举报