#Perform incremental BACKUP Function Command Add_Backup() { if [ $WEEK -ne "7" ];then cd $TARGET_DIR/$YEAR/$MONTH/$DAY ;tar -g $TARGET_DIR/snapshot -czvf $A_NAME$FILES ${SOURCE_DIR[@]} [ "$CODE" == "0" ]&&echo -e "-----------------------------------------\n\033[32mThese Add_Backup System Files $TARGET_DIR/$YEAR/$MONTH/$DAY/${YEAR}_$A_NAME$FILES Backup Successfully !\033[0m" fi } sleep 3 Full_Backup;Add_Backup
时间: 2023-09-16 20:09:07 浏览: 41
这段代码是一个 Bash 脚本中的函数 Add_Backup,用于执行增量备份操作。如果今天不是星期天(WEEK 不等于7),则进入目标目录(TARGET_DIR)下的年份、月份和日期子目录,使用 tar 命令对指定的源目录(SOURCE_DIR)进行增量备份,备份文件名为 A_NAME 与 FILES 的组合,备份快照保存在 TARGET_DIR/snapshot 文件中。如果备份成功(CODE 等于 0),则输出提示信息。最后,调用 Full_Backup 函数和 Add_Backup 函数。其中,Full_Backup 函数可能是进行全量备份的操作。
相关问题
#!/bin/bash #Auto Backup Linux System Files #by author rivers on 2021-09-28 SOURCE_DIR=( $* ) TARGET_DIR=/data/backup/ YEAR=`date +%Y` MONTH=`date +%m` DAY=`date +%d` WEEK=`date +%u` A_NAME=`date +%H%M` FILES=system_backup.tgz CODE=$? if [ -z "$*" ];then echo -e "\033[32mUsage:\nPlease Enter Your Backup Files or Directories\n--------------------------------------------\n\nUsage: { $0 /boot /etc}\033[0m" exit fi #Determine Whether the Target Directory Exists if [ ! -d $TARGET_DIR/$YEAR/$MONTH/$DAY ];then mkdir -p $TARGET_DIR/$YEAR/$MONTH/$DAY echo -e "\033[32mThe $TARGET_DIR Created Successfully !\033[0m" fi #EXEC Full_Backup Function Command Full_Backup() { if [ "$WEEK" -eq "7" ];then rm -rf $TARGET_DIR/snapshot cd $TARGET_DIR/$YEAR/$MONTH/$DAY ;tar -g $TARGET_DIR/snapshot -czvf $FILES ${SOURCE_DIR[@]} [ "$CODE" == "0" ]&&echo -e "--------------------------------------------\n\033[32mThese Full_Backup System Files Backup Successfully !\033[0m" fi } #Perform incremental BACKUP Function Command Add_Backup() { if [ $WEEK -ne "7" ];then cd $TARGET_DIR/$YEAR/$MONTH/$DAY ;tar -g $TARGET_DIR/snapshot -czvf $A_NAME$FILES ${SOURCE_DIR[@]} [ "$CODE" == "0" ]&&echo -e "-----------------------------------------\n\033[32mThese Add_Backup System Files $TARGET_DIR/$YEAR/$MONTH/$DAY/${YEAR}_$A_NAME$FILES Backup Successfully !\033[0m" fi } sleep 3 Full_Backup;Add_Backup
这是一个Linux系统文件自动备份脚本,可以备份指定的文件或目录。备份文件被打包成system_backup.tgz,并且按照年/月/日的目录结构存储在指定的目录下。该脚本还包括了全量备份和增量备份两种备份方式。全量备份在每周日进行,增量备份在其他时间进行。其中增量备份使用了tar的-g选项,可以根据快照文件只备份新增或修改的文件,从而减少备份的时间和存储空间。
add_if_exists = $(foreach d,$(1),$(if $(wildcard $(srctree)/$(d)),$(d) ,)) the usage of wildcard and foreach
`wildcard` is a function in GNU Make that allows you to perform wildcard pattern matching on file names. It takes a pattern argument and returns a space-separated list of file names that match the pattern. For example, `$(wildcard *.c)` would return a list of all the `.c` files in the current directory.
`foreach` is another function in GNU Make that allows you to iterate over a list of values and perform some action for each value. It takes two arguments: a variable name and a list of values. For each value in the list, the variable is set to that value and the specified action is performed.
In the `add_if_exists` example you provided, `wildcard` is used to check if certain directories exist in the source tree. `foreach` is then used to iterate over a list of directories and call `wildcard` on each directory. If the directory exists, it is added to the return list. If it does not exist, it is skipped.
Here is a breakdown of the `add_if_exists` code:
```
add_if_exists = $(foreach d,$(1),$(if $(wildcard $(srctree)/$(d)),$(d),))
```
- `add_if_exists` is a variable that contains the function definition.
- `$(1)` is a reference to the first argument passed to the function.
- `foreach d,$(1),` sets up a loop that iterates over the values in the first argument and sets the variable `d` to each value in turn.
- `$(wildcard $(srctree)/$(d))` performs wildcard pattern matching on the directory `$(srctree)/$(d)` to see if it exists.
- `$(if $(wildcard $(srctree)/$(d)),$(d),)` is a conditional statement that returns `$(d)` if the directory exists, or an empty string if it doesn't.
- The overall effect is to return a space-separated list of directories that exist in the source tree.