需求

线上环境有一些定时脚本(用crontab -l可查看当前用户的),有时我们可能会改这些定时任务的脚本内容。为避免改错无后悔药,需用shell实现一个程序,定时备份crontab中的.sh脚本文件

分析与思考

所有用户的crontab放在/var/spool/cron/目录,各个用户放在各自目录下。只要把这些用户的crontab读取出来,提取出其中的.sh文件,然后按照用户备份到相应目录就行。最后配一个crontab,定时去执行这个备份程序。

备份程序的实现

#!/bin/bash
# this shell script from  https://www.cnblogs.com/itwild/

# backup dir
# root user will in ${bak_dir}/root, itwild user will in ${bak_dir}/itwild
bak_dir=/var/itwild

# new file will end with 2020-02-24_00:28:56
bak_suffix=$(date '+%Y-%m-%d_%H:%M:%S')

if [[ ! $bak_dir == */ ]]; then
    bak_dir="${bak_dir}/"
fi

create_dir_if_not_exist() {
    u="$1"
    user_bak_dir="${bak_dir}$u"

    if [ ! -d "$user_bak_dir" ]; then
        mkdir -p $user_bak_dir
        chown -R ${u}:${u} $user_bak_dir
    fi
}

backup_files() {
    u="$1"
    files=$2

    for f in ${files[*]}; do
        if [[ $f == *.sh ]]; then
            # only backup file which end with .sh
            copy_file $u $f
        fi
    done
}

copy_file() {
    u="$1"
    filepath=$2

    if [ ! -f "$filepath" ];then
        return
    fi

    user_bak_dir="${bak_dir}$u"
    filename=${filepath##*/}
    cur_ms=$[$(date +%s%N)/1000000]
    # avoid same filename here add cur_ms to distinct
    newfilepath="${user_bak_dir}/${filename}.${bak_suffix}.${cur_ms}"

    # switch to user and then copy file to right position
    su - $u -c "cp $filepath $newfilepath"
}

# start from here
cd /var/spool/cron/

for u in `ls`
do
    create_dir_if_not_exist $u
    files=$(cat $u | awk '{for(i=6;i<=NF;++i) printf $i" "}')
    backup_files $u "${files[*]}"
done

说明

  • 此脚本需要用root用户去执行,因为它会把所有用户的crontab都备份到相应目录
  • 然后把这个程序配成crontab就能完成定时备份的任务
  • 此程序仅仅自用,方便以后查询shell的一些用法

使用shell程序备份crontab中的.sh脚本文件的更多相关文章

  1. shell 之 用linux定时任务crontab和watchdog.sh脚本做软件看门狗

    1.简介 看门狗的作用是定期检测服务正常运行,如果发现服务不在了,会重新拉起服务:linux中可以利用系统的定时任务功能crontab定期的去执行watchdog.sh脚本,而watchdog.sh脚 ...

  2. Linux中运行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory。

    在Linux中运行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory. 分析:这是不同系统编码格式引起的:在windows系统中 ...

  3. 在Linux中执行.sh脚本,异常

    在Linux中执行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory. 分析:这是不同系统编码格式引起的:在windows系统中 ...

  4. 在Linux中执行.sh脚本,异常/bin/sh^M

    在Linux中执行.sh脚本,异常/bin/sh^M 在linux中执行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory. 分 ...

  5. sh脚本文件的运行

    sh脚本文件的运行mac终端下运行shell脚本 1.写好自己的 脚本,比如test-bash.sh 2.打开终端 执行,方法一: 输入命令 ./test-bash.sh , 方法二:直接把 aa.s ...

  6. linux执行sh脚本文件命令

    linux执行sh脚本文件命令 很多时候需要多个命令来完成一项工作,而这个工作又常常是重复的,这个时候我们自然会想到将这些命令写成sh脚本,下次执行下这个脚本一切就都搞定了,下面就是发布代码的一个脚本 ...

  7. crontab中部署Python脚本注意事项

    有时候手工执行Python脚本跑的好好的,但是部署到Linux的crontab中后,就会遇到一些问题,最近终于有空整理一下这方面的内容,其实也是自己也踩了一些别人踩过的坑!这里仅仅列举个人遇到的一些小 ...

  8. shell如何传递变量到另一个脚本文件中

    http://www.jbxue.com/article/shell/20707.html本文介绍了shell脚本传递变量到另一个脚本文件中的方法,在脚本中调用另一脚本,即创建了一个子进程,感兴趣的朋 ...

  9. ubuntu中写sh脚本

    批量执行命令 https://jingyan.baidu.com/article/3052f5a121c8ac97f21f8661.html 批量执行脚本也是可行的! 如,main.sh内写   sh ...

随机推荐

  1. qt5实现简单布局

    layout.h #ifndef LAYOUT_H #define LAYOUT_H #include <QtWidgets/QDialog> #include <QLabel> ...

  2. Rust学习笔记一 数据类型

    写在前面 我也不是什么特别厉害的大牛,学历也很低,只是对一些新语言比较感兴趣,接触过的语言不算多也不算少,大部分也都浅尝辄止,所以理解上可能会有一些偏差. 自学了Java.Kotlin.Python. ...

  3. php mySql常用的函数

    1.mysql_connect()-建立数据库连接 格式: resource mysql_connect([string hostname [:port] [:/path/to/socket] [, ...

  4. css常用元素通用样式表

    @charset "utf-8";html,body,a,h1,h2,h3,h4,h5,h6,p,a,b,i,em,s,u,dl,dt,dd,ul,ol,li,strong,spa ...

  5. jq根据table的tr行数动态删除相应的行

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. FastDFS 配置文件 storage.conf

    FastDFS 版本5.05 配置文件分为三部分   控制器:tracker.conf存储器:storage.conf 客户端:client.conf 文件位置:/etc/fdfsstorage.co ...

  7. Linux 配置 DNS

    这里不讨论如何在linux上搭建一台DNS服务器: 这里讨论的是 配置 linux系统,让其能够解析域名,使用户可以流畅使用Internet 先了解几个文件,位于/etc目录下的有:hosts,hos ...

  8. Nginx是什么 ? 能干嘛 ?

    学习博客:https://blog.csdn.net/forezp/article/details/87887507 学习博客:https://blog.csdn.net/qq_29677867/ar ...

  9. typescript 点滴

    1 extend的用法 const x = extend({ a: 'hello' }, { b: 42 }); 2只有在d.ts,你才可以使用  export as 这样子的语法.而且必须有name ...

  10. linux中的挂载命令

    一.查询与自动挂载 查询系统中已经挂载的设备,-l会显示卷标名称 mount [-l] oot@izm5e2q95pbpe1hh0kkwoiz tmp]# mount sysfs on /sys ty ...