将多个子目录中的文件记录到相同文件夹结构中的备份位置

时间:2017-09-18 02:58:00

标签: logrotate

我试图使用logrotate,只有很少的经验,目前工作我有文件旋转,压缩和重命名到同一个文件夹。现在我需要而不是将文件放在同一个地方,我需要将它们放在另一个位置。它们还需要具有相同的文件夹结构,如果不是,则需要创建新文件夹。需要添加所有压缩文件,而不是覆盖现有文件

我认为olddir会将它们放入目标文件夹但不确定如何将其放入相应的文件夹中,或者如果它已经存在则创建它。

示例来源

无功/日志/ DEVICE1 / *。登录

无功/日志/设备2 / *。登录

无功/日志/设备3 / *。登录

将.gz文件放入

的目的地示例

选择/存档/ DEVICE1 /

选择/ archvie /设备2 /

(需要创建opt / archive / device3并在此处放置旋转文件)

1 个答案:

答案 0 :(得分:0)

最终找不到使用logrotate移动的方法,但想出了一些脚本来做同样的事情。相当简单,不适用于超过1级深的子文件夹。

#!/bin/bash

source="/opt/log/host"
destination="/opt/archive/"

for i in $(find $source -maxdepth 2 -type f -name "*.gz")
do

#removing /opt/log/host from string
dd="$( echo "$i" | sed -e 's#^/opt/log/host/##' )"

#removing everything after the first /
ff=$( echo "$dd" | cut -f1 -d"/" )

#setting the correct destination string
ee=$destination$dd

#create new folders if they do not exist
mkdir -p -- "$destination$ff"

#move files
mv $i $ee

done