cesaregb
cesaregb

Reputation: 765

rsync to backup one file generated in dynamic folders

I'm trying to backup just one file that is generated by other application in dynamic named folders. for example:

parent_folder/ 
   back_01 -> file_blabla.zip (timestam 2013.05.12)
   back_02 -> file_blabla01.zip (timestam 2013.05.14)
   back_03 -> file_blabla02.zip (timestam 2013.05.22)

and I need to get the latest generated zip, just that one it doesnt matter the name of the file as long as is the latest, is a zip and is inside "parent_folder" get that one.

as well when I do the rsync the folder structure + file name is generated and I want to omit that I want to backup that file in a folder and with a name so I know where is the latest and it will be always named the same.

now im doing this with a perl that get the latest generated folder with

"ls -tAF | grep '/$' | head -1" 

and perform the rsync but it does brings the last zip but with the folder structure that I dont want because it doesnt override my latest zip file.

rsync -rvtW --prune-empty-dirs --delay-updates --no-implied-dirs --modify-window=1 --include='*.zip' --exclude='*.*' --progress /source/ /myBackup/

as well it would be great if I could do the rsync without needing to use perl or any other script.

thanks

Upvotes: 0

Views: 806

Answers (1)

V H
V H

Reputation: 8587

The file names will differ each time ?

This would be hard for any type of syncing to work.

What you could do is :

  • create a new folder outside of where it is found, then :
  • Before you start remove the last sym linked file in that folder
  • When the file is found i.e. ls -tAF | grep '/$' | head -1 .... symlink it this folder
  • then rsync,ssh,unison file across to new node.
  • If the symlink name is file-latest.zip then it will always be this one file sent across.

But why do all that when you can just scp and you can take a look at here:

https://github.com/vahidhedayati/definedscp for a more long winded approach, and not for this situation but it uses the real file date/time stamp then converts to seconds... It might be useful if you wish to do the stat in a different way

Using stat to work out file, work out latest file then simply scp it across, here is something to get you started:

One liner:

scp  $(find /path/to/parent_folder -name \*.zip  -exec stat -t {} \;|awk '{print $1" "$13}'|sort -k2nr|head -n1|awk '{print $1}') remote_server:/path/to/name.zip

More long winded way, maybe of use to understand what above is doing:

#!/bin/bash
FOUND_ARRAY=()
cd parent_folder;
for file in $(find . -name \*.zip); do
  ptime=$(stat -t $file|awk '{print $13}');   
  FOUND_ARRAY+=($file" "$ptime)
done


IFS=$'\n'
FOUND_FILE=$(echo "${FOUND_ARRAY[*]}" | sort -k2nr | head -n1|awk '{print $1}');
scp $FOUND_FILE remote_host:/backup/new_name.zip

Upvotes: 1

Related Questions