【Shell】定时备份工具Crontab_backup

2022-01-16  本文已影响0人  caokai001

1.背景

之前介绍了如果备份的步骤,现在将其整理成tool了,方便大家使用。

👳♂️之前的流程:

需要手动编辑一下sh及cron文件;

##################
## Step1: rsync ##
##################

## 编辑你需要同步的文件
$ touch rsync.sh
#!/bin/bash
rsync -ahzv  /dev/pgm/ ~/sync/ > /dev/null 2>&1  

echo '#!/bin/bash' > rsync.sh
echo 'rsync -ahzv  /dev/pgm/ ~/sync/' >> rsync.sh

# --include '*.sas' --exclude '*.txt'
# ~/sync/$(date +%F)

 

####################
## Step2:crontab ##
####################
$ crontab -e

# DT:Execute rsync-sh script every day at 03: 00
# 每天凌晨三点钟,同步到自己的home目录下;
* 3 * * * sh ~/rsync.sh  > /dev/null 2>&1

👶更新后流程:

### 备份
Crontab_backup.sh -s "~/test1/*" -t "~/test2" -c "* * * * *"

2.代码

#!/bin/bash
#####################################################################
#  Program: Cron_backup.sh
#  Programmer: Kai Cao
#  Date: 11Jan2021
#  Study: all study
#  Version: 1.0 
#  Input :  Source Target Crontab
#
###################################################################
#  MODIFICATIONS:
#  Programmer:
#  Date:
#  Reason:
#
###################################################################/

##############################
# set shell function about uasge
##############################
# uasge help page
usage() {
    echo "Usage:"
    echo -e "    Cron_backup.sh [-s Source] [-t Target] [-c Crontab] [-l] [-r] [-d DeleteRow]"
    echo -e '    Cron_backup.sh -s "~/test1/*" -t "~/test2" -c "* * * * *" -p'
    echo -e "    Cron_backup.sh -d 1,2 "
    echo -e "    Cron_backup.sh -l \n"
    echo "Description:"
    echo -e "    Back up important code regularly\n    By default, the files that were last synchronized are overwritten\n"
    echo "Options:"
    echo -e "    s Source       : The path or type of file you want to back up. " 
    echo '                     such as : "./test1/*.sas" '
    echo -e "    t Target       : The path you want to synchronize. "
    echo '                     such as : "./test2" '
    echo -e "    c Crontab      : Enter crontab syntax timing.                  \n\t\t     such as : '* 3 * * *' means Every morning at three o 'clock"
    echo -e "    l ListCron     : Print all scheduled tasks .\n"
    echo -e "    r RemoveAll    : Remove all scheduled tasks \n"
    echo -e "    d DeleteRow    : Which row you want delect.\n\t\t     such as : 1,2 or  '1,2'\n"
    exit -1
}








##############################
# step1:parse crontab_tool parameter
##############################
# default parameter
PackageDaily="FALSE"
ListCron="FALSE"
RemoveAll="FALSE"



# parse arguement
while getopts s:t:c:d:lr option
do
   case "${option}"  in  
                s) Source=${OPTARG};;
                t) Target=${OPTARG};;
                c) Crontab=${OPTARG};;
                l) ListCron="TRUE";;
                r) RemoveAll="TRUE";;
                d) DeleteRow=${OPTARG};;
                h) usage;;
                ?) usage;;
   esac
done

# print parameter
#echo -e "\n\033[1;33m[Parse parameter]\033[0m"
#echo -e "         Source      : $Source 
#         Target      : $Target 
#         Crontab     : $Crontab 
#         ListCron    : $ListCron
#         RemoveAll   : $RemoveAll
#         DeleteRow   : $DeleteRow\n"


         
         
         
         
##############################
# step2: list the crontab content
##############################
# source && target && crontab variale not missing
if [ -n "$Source" ] && [ -n "$Target" ] && [ -n "$Crontab" ]; then

    # add_cron_backup
    echo -e "\033[1;31m[Backup Information]\033[0m"
    echo -e "\tOverwrites the last backup file "
    echo -e "\t${Crontab} rsync -ahzv ${Source} ${Target}> /dev/null 2>&1"
    echo "${Crontab} rsync -ahzv ${Source} ${Target} > /dev/null 2>&1">> ~/.crontab.cron

    # update crontab file
    crontab ~/.crontab.cron
    
    echo -e "\n\033[1;33mSuccessfully adding!\n\033[0m"
fi


##############################
# step3: list the crontab content
##############################
if [ ${ListCron} = "TRUE" ]; then 
    echo -e "\n\033[1;33m[Crontab content]\033[0m"
    crontab -l
    echo -e "\n"
fi

##############################
# step4: remove all the crontab
##############################
if [ ${RemoveAll} = "TRUE" ]; then 
    echo -e "\n\033[1;31m*** Warning! All crontabs have been cleared ***\033[0m"
    crontab -r
    rm -rf ~/.crontab.cron
    echo -e "\n"
fi

##############################
# step5: delete which row
##############################
if [ ${#DeleteRow} != 0 ]; then 

    # print before cron
    echo -e "\n\033[1;33m[Before delete]\033[0m"
    crontab -l
    
    # remove imfo
    echo -e "\n\033[1;31mRemove rows: ${DeleteRow} \033[0m"
    # print new cron
    echo -e "\n\033[1;33m[After delete]\033[0m\n"
    sed -i ${DeleteRow}d ~/.crontab.cron
    crontab ~/.crontab.cron
    crontab -l
fi


# Cron_backup.sh -s "./" -t "hello" -c "* * * * *" 
# Cron_backup.sh -s "~/test1/*" -t "~/test2" -c "* * * * *" 

3.Usage

3.1 参数解析:

$ Crontab_backup.sh -h

Usage:
    Cron_backup.sh [-s Source] [-t Target] [-c Crontab] [-l] [-r] [-d DeleteRow]
    Cron_backup.sh -s "~/test1/*" -t "~/test2" -c "* * * * *" -p
    Cron_backup.sh -d "1,2"
    Cron_backup.sh -l

Description:
    Back up important code regularly
    By default, the files that were last synchronized are overwritten


Options:
    s Source       : The path or type of file you want to back up.
                     such as : "./test1/*.sas"
    t Target       : The path you want to synchronize.
                     such as : "./test2"
    c Crontab      : Enter crontab syntax timing.
                     such as : '* 3 * * *' means Every morning at three o 'clock
    l ListCron     : Print all scheduled tasks .

    r RemoveAll    : Remove all scheduled tasks

    d DeleteRow    : Which row you want delect.
                     such as : "1,2"

3.2 Crontab format

用户所建立的crontab文件中,每一行都代表一项任务,每行的每个字段代表一项设置,它的格式共分为六个字段,前五段是时间设定段,第六段是要执行的命令段,格式如下:

minute hour day month week command

其中:

image-20220116220216584

在以上各个字段中,还可以使用以下特殊字符:
星号():代表所有可能的值,例如month字段如果是星号,则表示在满足其它字段的制约条件后每月都执行该命令操作。
逗号(,):可以用逗号隔开的值指定一个列表范围,例如,“1,2,5,7,8,9”
中杠(-):可以用整数之间的中杠表示一个整数范围,例如“2-6”表示“2,3,4,5,6”
正斜线(/):可以用正斜线指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。同时正斜线可以和星号一起使用,例如
/10,如果用在minute字段,表示每十分钟执行一次

3.3 crontab format在线工具

创建crontab format web page https://crontab-generator.org/

img

检验crontab含义的web page https://crontab.guru/

img

⚙4.实例


############################# 使用
## help message
Crontab_backup.sh -h
## add backup
mkdir -p ~/test2
Crontab_backup.sh -s "~/test1/*" -t "~/test2" -c "* * * * *"
ll ~/test2
## add backup
mkdir -p ~/test3
Crontab_backup.sh -s "~/test1/*" -t "~/test3" -c "* * * * *"
ll ~/test3
## view all cron
Crontab_backup.sh -l
## reomve one timer
Crontab_backup.sh -d "2"
## remove all timer
Crontab_backup.sh -r

5.Note

image-20220116215306070

Crontab_backup.sh -s "~/test1/*" -t "~/test2" -c "* * * * *"

DeleteRow: "1,2" , '1,2' , 1,2

Demo&QA?

上一篇下一篇

猜你喜欢

热点阅读