bash 如何备份 crontab 中列出的所有文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20348452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:50:19  来源:igfitidea点击:

How do i backup all files listed in crontab

linuxbashshellcrontab

提问by Akki

I am trying to backup the crontab entries structure and files running under crontab.

我正在尝试备份在 crontab 下运行的 crontab 条目结构和文件。

But I am stuck with how do i search files in crontab -land copy the scripts running under it.

但是我对如何在其中搜索文件crontab -l并复制在其下运行的脚本感到困惑。

My possible ways

我可能的方法

I have listed

我已经列出

crontab -l > test.txt 

cat test.txt 

But how can I search the files after listing.

但是如何在列出后搜索文件。

My entries are like:

我的条目是这样的:

# daily pingback report
30 1 * * * php /var/opx/cron-script/daily-email-reports/pingback_report.php | curl --data-binary @- https://pxc.com/email/send

# --------------------------------------------------------------------------------------------------
# daily jobs 02+ hours
# --------------------------------------------------------------------------------------------------

# daily Failed Device Search
0 2 * * *  /var/opx/cron-script/failed_device_search.sh

# daily Device Not Detected (we are not getting any of these, so commenting out)
# 5 2 * * *  /var/opx/cron-script/device_not_detected.sh

    # daily Failed App Search (we are not getting any of these, so commenting out)
    # 10 2 * * *  /var/opx/cron-script/failed_app_search.sh

    # daily oss event report (uncomment when dev db issue is fixed)
    10 3 * * * /var/opx/cron-script/event-analysis/daily-oss-event-report

    # Copy tomcat logs from s1 and s2 , compress them and move to S3 storage
    #30     21      *       *       *       /var/opx/cron-script/log_backup.sh

I want to copy scripts running in these entries.

我想复制在这些条目中运行的脚本。

采纳答案by John Zwinck

Try this:

尝试这个:

for part in $(sed 's/#.*//' test.txt | while read t1 t2 t3 t4 t5 main; do echo $main; done); do
  echo $part
done | grep / | grep -v ://

For your example, this prints:

对于您的示例,这会打印:

/var/opx/cron-script/daily-email-reports/pingback_report.php
/var/opx/cron-script/failed_device_search.sh
/var/opx/cron-script/event-analysis/daily-oss-event-report

I'm not proud of it, but it is fairly portable shell, and seems to work for the cases you've given. You might need a smarter parser for more complex cases, or to handle more obscure words that may exist in your crontabs.

我并不为此感到自豪,但它是相当便携的外壳,并且似乎适用于您提供的案例。您可能需要更智能的解析器来处理更复杂的情况,或者处理 crontab 中可能存在的更晦涩的单词。

回答by Akki

This is my script useful if any one want to backup cron's

如果有人想备份 cron,这是我的脚本很有用

#!/bin/bash

    #This Script use for backup of crontab files and entries
    #Modify as per your need

    mkdir -p /mnt/cron_backups
    for part in $(crontab -l | grep -v "clientmqueue" | sed 's/#.*//' | while read t1 t2 t3 t4 t5 main; do echo $main; done); do
      echo $part
    done | grep / | grep -v :// | while read -r row
                                      do
                                        scp $row /mnt/cron_backups/
                                        crontab -l > /mnt/cron_backups/backup_cron.txt 
                                        tar -cvzf  /mnt/cron_backups.tar.gz /mnt/cron_backups/

                                      done
    # grep -v is used for ignoring specific script with specific words or character 

Thanks to John....

感谢约翰....