Archive different folders based on their names

This is my first post so ... be gentle:)
Hello I have several folders that are backed up daily in following format:

/back_YY.MM.DD/backup1/*
........................./backup2/*

I looking a script to archive and rename all backup folders bazed on root folder date:

/back_YY.MM.DD/backup1_YYMMDD.tar.gz
........................../backup2_YYMMDD.tar.gz

Thanks in advance.

for F in /back_YY.MM.DD/backup1 /back_YY.MM.DD/backup2
do echo $F/${F#/*/}_${F:6:2}${F:9:2}${F:12:2}.tar.gz
done

output:

/back_YY.MM.DD/backup1/backup1_YYMMDD.tar.gz
/back_YY.MM.DD/backup2/backup2_YYMMDD.tar.gz

do you mean you need tar and compress the folders under /back_YY.MM.DD?

for example, if there is a folder :

/back_YY.MM.DD/backup1/* 

you need gtar the folder backup1, and saved as

/back_YY.MM.DD/backup1_YY.MM.DD.tar.gz
1 Like

I think you're right.

for F in /back_YY.MM.DD/backup1 /back_YY.MM.DD/backup2
do echo ${F%/*}/${F#/*/}_${F:6:2}${F:9:2}${F:12:2}.tar.gz
done

result

/back_YY.MM.DD/backup1_YYMMDD.tar.gz
/back_YY.MM.DD/backup2_YYMMDD.tar.gz
1 Like

Thanks for your reply. I will try your script and keep you posted.

I guess you have many back_YY.MM.DD folders with different date.

rootpath="/"          # you can update here, if the backup folder are not under / 

cd $rootpath

ls -l back_* |awk '/^d/ {print $NF}'  |while read dir
do
  cd ${dir}
  ls -l |awk '/^d/ {print $NF}' |while read subdir
  do
     tar cvf - ${subdir} |gzip > ${subdir}_${dir#*_}.tar.gz   # or replace by below command, if you have gtar
#   gtar zcvf ${subdir}_${dir#*_}.tar.gz ${subdir}
  done
  cd $rootpath
done

Yes. I have many backups with different dates and in each day is adding more.
I tested your script and something strange are happening:

  • al folders beginning with back are processed ("back" "back.somename")

  • on every pass i get a message about line 7:
    "line 7: cd: back1: No such file or directory"

    Probably because of that:

    • all archives are made in root folder and not in it's own folder
    • all files from subfolders are not deleted.
    • archives contain all subfolders not just own folders: backup1_YYMMDD.tar.gz contain all subfolders (ex. backup1/, backup2/, etc) instead just backup1/* subfolder.

Even so, I can use your script as it is. Is a great time saving. Thanks again for your reply.

I made little modification on rcdcwayx code, and is working fine now:

rootpath="/somefolder"   # you can update here, if the backup folder are not under /
cd $rootpath 
ls -l|grep back_ |awk '/^d/ {print $NF}' |while read dir
 do
   cd ${rootpath}/${dir}
  ls -l |awk '/^d/ {print $NF}' |while read subdir
 do
 tar cvf - ${subdir} |gzip > ${subdir}_${dir#*_}.tar.gz # or replace by below command, if you have gtar 
# gtar zcvf ${subdir}_${dir#*_}.tar.gz ${subdir}
 done
 cd $rootpath
 done 

Just "back_" folders are processed, all tars are created now in own "back" folders and contain only right subfolder. I can�t understand why cd command doesn�t work without $rootpath variable ("line 7: cd: back1: No such file or directory" are displayed and all script go crazy).

Still I don�t know how to delete archived files and subfolders and how to avoid archival of already archived folders on next script run. Maybe changing the name of main folder from �back_� to �back.� after archiving process end, may by a solution.

Thanks again for help.

The final version:

cd $rootpath

ls -l|grep back_ |awk '/^d/ {print $NF}'  |while read dir
do
  cd ${rootpath}/${dir}
  ls -l |awk '/^d/ {print $NF}' |while read subdir
  do
     tar cvf - ${subdir} |gzip > ${subdir}_${dir#*_}.tar.gz   # or replace by below command, if you have gtar
#   gtar zcvf ${subdir}_${dir#*_}.tar.gz ${subdir}
     rm -rf $subdir
  done
  cd $rootpath
done
rename back_ back. back_*

All subfolders are deleted and main folders are renamed to not be procesed on next run.