Lftp mirror pattern

hi
I have lftp server directory like this:

13991010
13991011
13991012
13991013
Fabric
Test
Test2

I want to mirror only content of latest directory with this pattern 13991013 (8digit)
Content of each directory like this : 13991013 > customers1 customers2 customers3
e.g.
something like this:

mirror -include �^[0-9]{8}$'/*

Any recommendation?
Thanks,

Hi
try this

: [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]; echo mirror -include "$_"/*
mirror -include 13991013/customers1 13991013/customers2 13991013/customers3

if everything works correctly, remove the 'echo'

--- Post updated 03-12-20 at 00:00 ---

or

 : [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]; echo mirror -include "$_/*"
mirror -include 13991013/*

--- Post updated at 00:24 ---

Or maybe this will be enough?

: 1*; echo mirror -include "$_/*"
mirror -include 13991013/*

What about any other 8 digit? It change and dynamic each day with same patter?

What about any other 8 digit? It change and dynamic each day with same patter?

Sorry, I can't understand the question. In the above examples with pattern '[0-9]' ,
the largest eight-digit integer or the pattern itself (if no matches are found) will be displayed in the "$_" variable
try this

: [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]; echo $_

--- Post updated at 15:36 ---

possible you need to find the last directory by modification time.
Then it's better to use 'find' or 'ls' with the option '-t'

--- Post updated at 15:50 ---

try this

: $(ls -dptr [0-9]*); echo $_

--- Post updated at 15:56 ---

else

mirror --include "$(ls -pt | grep -Em1 '[0-9]{8}/$')*"

check first separately

ls -pt | grep -Em1 '[0-9]{8}/$'

--- Post updated at 16:09 ---

maybe this?

ls -pt | grep -m1 '[0-9]*/$'

Thank you for your reply but it give directory name not content of it.

Currently Output: 13991013

Expected output:
customers1
customers2
customers3
...

FYI: as I mention before content of each directory is different

Hello
Please do not quote everything meaninglessly. This makes it difficult to work with your topic.
The main task was to find the last directory either by number or by creation time.
I still haven't received an answer, we have dealt with the main task or not and which option is right for you.
Just in case, I bring a few and I hope for your detailed answers

mirror --include "$(ls -pt | grep -Em1 '[0-9]{8}/$')"*
mirror --include "$(ls -pt | grep -m1 '[0-9]*/$')"*

Regards
Nez

Sorry first time I reply and didn't received any answer I thought I should quote till notify you :))

Main task is Both, last directory by number AND by creation time. If both condition were true then copy content of directory.

and?
did we solve your problem?
If not, then specifically on the code above, write what does not work.

I tried to divide the problem into its component parts. In the first, you need to find the desired directory.
Forget about its contents for now. Write which of the given examples finds the correct directory.

ls -pt | grep -Em1 '[0-9]{8}/$'
ls -pt | grep -m1 '[0-9]*/$'

Here is my script, and I try every command you mention but it always copy all directories not content of latest with 8 digit number pattern!

#!/bin/bash
DATE=`date +%Y%m%d -d "1 days ago"`

USER='user'
PASS='pass'

HOST='192.168.1.1'
LOCAL_BACKUP_DIR='/app/'${DATE}''
REMOTE_DIR='/app/log'

mkdir -p '/app/'${DATE}''

lftp -u $USER,$PASS $HOST <<EOF
set ftp:ssl-protect-data true
set ftp:ssl-force true
set ssl:verify-certificate no
cd $REMOTE_DIR
lcd $LOCAL_BACKUP_DIR

mirror --include "$(ls -pt | grep -Em1 '[0-9]{8}/$')"*

quit
EOF
 

output:

13991010 
13991011 
13991012 
13991013 
Fabric 
Test 
Test2
 

expected output:

customers1 
customers2 
customers3
...

I understood what the problem was. We cannot define a locally remote directory

ok little confused, would you please write complete script you mean? like what I wrote in previous reply?

Thanks,

I will try to install 'lftp'

1 Like

I don't know how to say thank you for time that you spend on this post. :slight_smile:

All I can advise

#!/bin/bash
DATE=`date +%Y%m%d -d "1 days ago"`

USER='user'
PASS='pass'

HOST='192.168.1.1'
LOCAL_BACKUP_DIR='/app/'${DATE}''
REMOTE_DIR='/app/log'
tmp=($(lftp -e "cd $REMOTE_DIR && glob -d echo [0-9]*; quit" -u $USER,$PASS $HOST))
REMOTE_DIR="$REMOTE_DIR/${tmp[@]: -1}"

mkdir -p '/app/'${DATE}''

lftp -u $USER,$PASS $HOST <<EOF
set ftp:ssl-protect-data true
set ftp:ssl-force true
set ssl:verify-certificate no
cd $REMOTE_DIR
lcd $LOCAL_BACKUP_DIR

mirror

quit
EOF
1 Like

unfortunately on remote location there are some directories that use 6 digit number!
when I ran script it copy them instead of 8 digit directories.

e.g: remote directory content like this:

13991010 
13991011 
13991012 
13991013 
Fabric 
Test 
Test2
991018
991019

output of script: (copy content of this directory 991019 instead of this 13991013)

 991019

expected output: (content of this 13991013 (exact 8digit))

 13991013
tmp=($(lftp -e "cd $REMOTE_DIR && glob -d echo [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]; quit" -u $USER,$PASS $HOST))

Or maybe you have access via the SSH protocol? For example, by public key

REMOTE_DIR="$REMOTE_DIR/$(ssh user@host "cd $REMOTE_DIR; ls -tp | grep -Em1 '[0-9]{8}/$'")"

--- Post updated at 16:00 ---

Another option. If there is any sequence in the numbering of remote directories, for example,
the number in the name increases by one every day. You can simply organize a daily increase number in the local script

2 Likes

Work like charm! This is exactly what I want.

Thank you so much brother :slight_smile: