Pages

Affichage des articles dont le libellé est backup. Afficher tous les articles
Affichage des articles dont le libellé est backup. Afficher tous les articles

mardi 13 mars 2012

copy old files to a new dir

1)  The idea of the script:
We would like to find the files modified some days ago, and mv them to another server.
---- to find the files and directories modified between $3 days ago  and $4 days ago
-----copy to a new server
-----remove those found files from the old server


2) Attentions
   a) to find the files/directories and remove them
       to remove directories
      find . -type d -mtime $i -exec rm -rf {} \; 
      to remove files
      find . -type f -mtime $i -exec rm -f {} \;  
      There must be \ and ;

   b) in csh if we define a variable by:
       set foundfiles = `find . -type d -mtime $i`
       This variable $foundfiles could contain the array.
   c) In csh, it does not work if we define a new variable by :
       set b = $foundfiles 
      will not produce the same string as $foundfiles !!!
    d) The following does not work in csh
       if ( $foundfiles != "") then
          somthing
       endif
     It will produce "if:Expression Syntax" error, because it has difficulty to deal with a variable with array. (see http://www.grymoire.com/Unix/Csh.html also for some insights).
     e ) What works is:
      if ( `find . -type d -mtime $i` != "") then
        something
      endif


3 ) Here is the complete code that I use to copy old files from one server to another.
# Input:
# ---1) the directory where we want to find the folders  in the old server
# ---2) the directory where we want to move the files in the new server
# ---3) the initial date xx when the files are modified xx days ago
# ---4) the final date xx when the files are modified xx days ago
#

set oldserverpath = $1
set newserverpath = $2
set firstdaylimit = $3
set lastdaylimit = $4
#------------------------------------------------
#set newserverpath = /home/user/work/gin/eqna
if ( ! -e $oldserverpath) then
echo "The old directory is not found. Exit."
exit 1
endif
#------------------------------------------------
cd $oldserverpath
# copy folders
@ i = $firstdaylimit
while ( $i  <= $lastdaylimit )
set foundfiles = `find . -type d -mtime $i`
if ( `find . -type d -mtime $i` == "" ) then
echo "Not found files $i days ago. Continue."
else if ( `find . -type d -mtime $i` != "" ) then 
echo "copy the found files $i days ago to gsat"
scp -r $foundfiles "user@gsat:"$newserverpath
find . -type d -mtime $i -exec rm -rf {} \;
endif
@ i += 1
end
# copy files 
@ i = $firstdaylimit
while ( $i  <= $lastdaylimit )
set foundfiles = `find . -type f -mtime $i`
if ( `find . -type f -mtime $i` == "" ) then
echo "Not found files. Continue."
else if ( `find . -type f -mtime $i` != "" ) then 
echo "copy the found files to gsat"
scp  $foundfiles "user@gsat:"$newserverpath
find . -type f -mtime $i -exec rm -f {} \;
endif
@ i += 1
end