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" "The source file location:" "" > /tmp/source.location
result=$?
if [ $result -eq ] ;then
echo -e "\e[2J\e[1;1H"
exit ;
elif [ $result -eq ] ;then
echo -e "\e[2J\e[1;1H"
exit ;
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" "The destination dir location:" "" > /tmp/destination_dir
result=$?
if [ $result -eq ] ;then
input_sourcefile_location;
elif [ $result -eq ] ;then
echo -e "\e[2J\e[1;1H"
exit ;
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 * * *) " "The minutes set:" "" "The hours set:" "" "The day set:" "" "The month set:" "" "The week set:" "" > /tmp/crontab
result=$?
echo $result > lll
if [ $result -eq ] ;then
input_destination_dir
elif [ $result -eq ] ;then
echo -e "\e[2J\e[1;1H"
exit ;
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.
 
 
 
 
 
 
 
 
 
 
      

shell脚本实现定时备份某文件的更多相关文章

  1. 用shell脚本实现定时备份数据库

    1.备份数据库的方法 可以使用命令查看 ls  /usr/local/mysql/bin 这个mysqldump就是系统内置的用来备份数据库的工具. 2.实现方法 ①先随便进入一个位置创建一个目录 ② ...

  2. 【shell脚本】定时备份日志===logBackup.sh

    定时备份日志 设置执行权限 [root@VM_0_10_centos shellScript]# chmod a+x logBackup,sh 脚本内容 [root@VM_0_10_centos sh ...

  3. 【shell脚本】定时备份数据库===dbbackup.sh

    定时备份数据库是很有必要的 一.脚本内容 [root@localhost dbbackup]# cat dbbackup.sh #!/bin/bash #备份数据库 mysqldump -uroot ...

  4. shell 脚本实现定时备份mysql数据库

    首先要知道直接在脚本中输入mysql的密码是不被允许的,但是我们可以曲线救国 1. 在新建一个文件专门用来存储用户密码 如: vim ./.mysql.conf [mysqldump] user=yo ...

  5. shell脚本----周期压缩备份日志文件

    一.日志文件样式 二.目标 1.备份压缩.log结尾&&时间样式为“date +%Y%m%d”的日志文件(如:20170912.20160311等) 2.可指定压缩范围(N天前至当天) ...

  6. CentOS/Linux内存占用大,用Shell脚本自动定时清除/释放内存

    CentOS/Linux内存占用大,用Shell脚本自动定时清除/释放内存来自:互联网 时间:2020-03-22 阅读:114以下情况可能造成Linux内存占用过高服务配置存在直接分配错误,或隐性分 ...

  7. Shell脚本调用ftp上传文件

    Shell脚本调用ftp上传文件 1.脚本如下 ftp -n<<! open x.x.x.x ###x.x.x.x为ftp地址 user username password ###user ...

  8. Centos上通过shell脚本实现数据库备份和还原

    最近有个这样的需求,通过shell脚本实现数据库备份还原,最后通过网上查询自己测试实现,将脚本分享给大家 1.数据库备份脚本 #!/bin/bash ds=`` list=`date +%Y`/`da ...

  9. 七牛云qshell工具定时备份空间文件到本地

    qshell 是利用七牛文档上公开的 API实现的一个方便开发者测试和使用七牛API服务的命令行工具,使用该工具可以实现很多的功能,今天就分享一下利用qshell定时备份空间文件到本地 1.下载qsh ...

随机推荐

  1. Windows Server 2008环境下Apache2.4+Tomcat8配置

    安装步骤 1. 安装配置JDK2. 安装配置Apache3. 安装配置Tomcat4. 启动服务并测试 一.Apache安装与配置 1.Apache解压在D盘根目录下建立一个文件夹Apache Gro ...

  2. fcn+caffe+voc2012实验记录

    参考博客: http://blog.csdn.net/haoji007/article/details/77148374 http://blog.csdn.net/jacke121/article/d ...

  3. ranch 源码分析(二)

    接上ranch 源码分析(一) 上次讲到了ranch.erl的start_listener函数,下面我们详细分析下这个函数 -module(ranch). %...... 省略若干行 -spec st ...

  4. 关于window 图片系统功能

    直接选择文件悬浮  分辨率 大小 修改时间 文件选择较少的 可以选择反选  找到自己想要的 大小排序 找到最大的压缩  名称排序 同一类的图片  按时间排序找自己刚修改的图片 文档类预览

  5. SAP 多语言文本翻译

    SAP自己的东西都是有语言包的,针对很多语言有是有对应文本的翻译,巴特,比较不是专业的翻译,多以很多时候还是有这样那样的文本描述需要调整. 语言包怎么打就不说了,也不知道,知道也没打过... 标准界面 ...

  6. JAVA的原子性和可见性,线程同步的理解

    1.原子性 (1)原子是构成物质的基本单位(当然电子等暂且不论),所以原子的意思代表着——“不可分”: (2)原子性是拒绝多线程操作的,不论是多核还是单核,具有原子性的量,同一时刻只能有一个线程来对它 ...

  7. Win10系列:C#应用控件基础9

    TextBox控件 在前文中已经或多或少的接触到过TextBox控件.TextBox控件是一种常用的,也是比较容易掌握的文本编辑控件,它不仅可以接收输入的数据,还可以像TextBlock控件(Text ...

  8. php7安装 event扩展

    1 下载地址:http://pecl.php.net/package/event 2 安装支持库libevent,需要编译高版本(这里以最新版本release-2.1.8-stable为例) http ...

  9. Android : 跟我学Binder --- (2) AIDL分析及手动实现

    目录: Android : 跟我学Binder --- (1) 什么是Binder IPC?为何要使用Binder机制? Android : 跟我学Binder --- (2) AIDL分析及手动实现 ...

  10. oracle中数字保留几位小数的问题

    需求:#将数字填充到对应金额单中 select substr(b.payMoney,length(b.payMoney),1) 分, substr(b.payMoney,length(b.payMon ...