Archiving files keeping the same structure directory

Hello Team,

We would like to backup a lot of files inside of a structure of directories, four, five or more levels in some Ubuntu, Mac and Solaris systems.

For instance:

/home/chuck/sales/virgin/rent-quote.pdf
/home/chuck/sales/marriott/vacation-quote.pdf
/home/chuck/sales/telefonica/mobile-quote.pdf

I would like to create a shell script in order to archive the files in other filesystem with the following conditions:

  1. Conserve the same original directory structure
  2. Classify by year in a new directory (last date of modified)
  3. Move the file to the new archive directory

output:

/backup/home/chuck/sales/2010/virgin/rent-quote.pdf
/backup/home/chuck/sales/2011/marriott/vacation-quote.pdf
/backup/home/chuck/sales/2012/telefonica/mobile-quote.pdf

In the future I would like to run this script periodically to archive files with more than 60 days old

I really appreciate it your help

Regards,

Carlos

Have you tried putting things together?

You will have to use find and stat to look for files and their modification times and then take out the year from the last modification time to check for the existance of the archive directories and the move the files over.

no space in all file and directory name

#! /usr/bin/bash
path="/home/chuck/sales"
if [ ! -d $path ]; then
   echo "don't find the folder $path, exit"
   exit
fi

find $path -type f |while read file
do
  file=${file#./}
  dir=${file%/*}
  year=$(perl -MFile::stat -e "print scalar localtime stat('$file')->mtime" |awk '{print $NF}' )
  dir=${dir/$path//backup$path/$year}
  echo $file,$dir,$year

  if [ ! -d $dir ]; then
      mkdir -p $dir
  fi
  cp $file $dir
done