Logrotate files in multiple sub directories to backup location in same folder structure

2.2k Views Asked by At

Im trying to use logrotate with very little experience, Currently working i have the files rotating, compressing and renaming into the same folder. Now i need instead of dropping the files in the same place, i need to have them dropped in another location. They also need to have the same folder structure and if it isn't there than it needs to create the new folder. All the compressed files need to be added and not override the existing files

I'm thinking that the olddir will drop them into a destination folder but not sure on how to have it drop it in the corresponding folder or create it if its not already there.

Example source

var/log/device1/*.log

var/log/device2/*.log

var/log/device3/*.log

Example Destination to drop .gz files into

opt/archive/device1/

opt/archvie/device2/

(needs to create opt/archive/device3 and put rotated file in here)

1

There are 1 best solutions below

0
On

Didn't end up finding a way to move with logrotate but came up with script to do the same sort of thing. pretty simplistic and wont work for more than 1 level deep of subfolders.

#!/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