Compress Dirs
The purpose of this document is to show how to compress a set of dirs and then delete the compressed dirs after a week.
/etc/crontab 7 # .---------------- Min (0 - 59) 8 # | .------------- Hr (0 - 23) 9 # | | .---------- DOM (1 - 31) 10 # | | | .------- Mon (1 - 12) 11 # | | | | .---- DOW (0 - 6) 12 # | | | | | 13 # Min Hr DOM Mon DOW User Command 14 #*/10 * * * * root truncate --size=10M /var/log/maillog 15 45 23 * * * root /opt/jgs/scripts/trim.sh >> /var/log/trim.log 2>&1 16 1 0 * * * root find /home/prod/postfix_backup/ -mindepth 1 -maxdepth 1 -name '*.tgz' -type f -mtime +7 -delete 17
/opt/jgs/scripts/trim/trim.sh
#!/bin/bash src_dirs="/var/proj/src" tmp_dirs="/var/proj/tmp" tgz_dirs="/var/proj/dst" mkdir -p "${tmp_dirs}" mkdir -p "${tgz_dirs}" iso_date=$( date +'%Y-%m-%d' ) dirs=( "forward" "noreply" ) for dir in "${dirs[@]}" do sleep 1 this_dst_dir="${tmp_dirs}/${dir}" this_src_dir="${src_dirs}/${dir}" if [ -d "${this_dst_dir}" ] then echo "$( date --rfc-3339=seconds --utc) - Deleting $this_dst_dir ..." rm -rf "${this_dst_dir}" fi mkdir -p "${this_dst_dir}" echo "$( date --rfc-3339=seconds --utc) - Now going from $this_src_dir to $this_dst_dir ..." find "${this_src_dir}" -type f -mtime +2 -print0 | xargs -0 -I "{}" mv "{}" "${this_dst_dir}" echo "$( date --rfc-3339=seconds --utc) - Compressing to ${tgz_dirs}/${dir}_${iso_date}.tgz ..." tar -cf - "${this_dst_dir}" | pigz --to-stdout --verbose > "${tgz_dirs}/${dir}_${iso_date}.tgz" echo "$( date --rfc-3339=seconds --utc) - Deleting $this_dst_dir ..." rm -rf "${this_dst_dir}" sleep 1 done