bash script for loop

Discussion in 'Programming/Scripts' started by Tom John, Sep 2, 2019.

  1. Tom John

    Tom John Active Member HowtoForge Supporter

    Hi guys,
    i want to backup some files of my backup directory.
    my script list all the files of all directories in /var/backup
    with head -n1 i see only the newest file in the directory but i need:
    - the newest file of .sql.gz
    - and the newest file of tar.gz
    i tried it with find but i don't get the result i want to have.
    How can i find the newest file of tar.gz and the newest file of sql.gz
    i dont know how to put this into the loop

    Thanks a lot for your kind help
    ls of /var/backup:
    Code:
    root@server3:~# ls /var/backup
    server3  web3  web4  web5  web8  web9
    
    
    ls of the directory web3:
    Code:
    root@server3:~# ls /var/backup/web3/
    db_c1wppass_2019-09-01_00-01.sql.gz  web3_2019-09-01_00-01.tar.gz
    db_c1wppass_2019-09-02_00-01.sql.gz  web3_2019-09-02_00-01.tar.gz
    root@server3:~# 
    
    
    the bash script:
    Code:
    #!/bin/bash
    
    #Backup vom ISPConfig backup auf backupserver 1 x Wöchentlich neueste Datei
    
    TIMESTAMP=$( date +%F )
    BACKUP_DIR="/var/backup/*/"
    
    
    
    for d in $BACKUP_DIR 
    do
          ls -d "$d"
          ls -t "$d" | head -n 1
    done
    
    
     
  2. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    Something like this should work:
    Code:
    NEWESTWEB=$(ls -t *.tar.gz | head -n 1)
    NEWESTSQL=$(ls -t *.sql.gz | head -n 1)
    
     
    ahrasis likes this.
  3. Tom John

    Tom John Active Member HowtoForge Supporter

    thanks for your kind answer.
    if i run on command line: ls -t /var/backup/*/ *.tar.gz
    i get:
    Code:
    /var/backup/web3/:
    db_c1wppass_2019-09-19_00-01.sql.gz  web3_2019-09-17_00-01.tar.gz
    web3_2019-09-19_00-01.tar.gz         db_c1wppass_2019-09-16_00-02.sql.gz
    db_c1wppass_2019-09-18_00-01.sql.gz  web3_2019-09-16_00-01.tar.gz
    web3_2019-09-18_00-01.tar.gz         db_c1wppass_2019-09-15_00-01.sql.gz
    db_c1wppass_2019-09-17_00-01.sql.gz  web3_2019-09-15_00-01.tar.gz
    
    /var/backup/web4/:
    db_c1pass_2019-09-19_00-01.sql.gz  db_c1pass_2019-09-17_00-01.sql.gz
    web4_2019-09-19_00-01.tar.gz       web4_2019-09-17_00-01.tar.gz
    db_c1pass_2019-09-18_00-01.sql.gz  db_c1pass_2019-09-16_00-02.sql.gz
    web4_2019-09-18_00-01.tar.gz       web4_2019-09-16_00-01.tar.gz
    
    /var/backup/web5/:
    web5_2019-09-19_00-01.tar.gz  web5_2019-09-17_00-01.tar.gz
    web5_2019-09-18_00-01.tar.gz  web5_2019-09-16_00-01.tar.gz
    
    
    But when i run:
    ls -t /var/backup/*/ .tar.gz | head -n 1 (to get the newest files only)
    i get the following output:
    Code:
    root@server3:~# ls -t /var/backup/*/ *.tar.gz | head -n 1
    ls: cannot access '*.tar.gz': No such file or directory
    /var/backup/server3/:
    
    
    actually i dont understand why there is the output cannot access ....
    how can i add your command $NEWESTWEB into my script ?
    maybe you can help?
    thanks a lot


    i changed the script into:
    Code:
    TIMESTAMP=$( date +%F )
    BACKUP_DIR="/var/backup/*/"
    NEWESTWEB=$( ls -t "$d" *.tar.gz | head -n 1 )
    
    for d in $BACKUP_DIR
    do
           $NEWESTWEB
       
    done
    
    
    but this does not work as well..
     
    Last edited: Sep 19, 2019
  4. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    If the command line you pasted is the actual command you wrote, there is an extra space in the pathname before *.tar.gz.
    Code:
    root@server3:~# ls -t /var/backup/*/ *.tar.gz | head -n 1
    
    Code:
    $ ls -t /tmp/Test/backup/*/ *.tar.gz
    ls: tiedostoa '*.tar.gz' ei voi käsitellä: Tiedostoa tai hakemistoa ei ole
    /tmp/Test/backup/web5/:
    
    $ ls -t /tmp/Test/backup/*/*.tar.gz
    /tmp/Test/backup/web5/web5_2019-09-16_00-01.tar.gz
    /tmp/Test/backup/web5/web5_2019-09-18_00-01.tar.gz
    /tmp/Test/backup/web5/web5_2019-09-17_00-01.tar.gz
    /tmp/Test/backup/web5/web5_2019-09-19_00-01.tar.gz
    /tmp/Test/backup/web4/web4_2019-09-16_00-01.tar.gz
    /tmp/Test/backup/web4/web4_2019-09-18_00-01.tar.gz
    /tmp/Test/backup/web4/web4_2019-09-17_00-01.tar.gz
    /tmp/Test/backup/web4/web4_2019-09-19_00-01.tar.gz
    /tmp/Test/backup/web3/web3_2019-09-15_00-01.tar.gz
    /tmp/Test/backup/web3/web3_2019-09-18_00-01.tar.gz
    /tmp/Test/backup/web3/web3_2019-09-16_00-01.tar.gz
    /tmp/Test/backup/web3/web3_2019-09-19_00-01.tar.gz
    /tmp/Test/backup/web3/web3_2019-09-17_00-01.tar.gz
    
    That $NEWESTWEB is not a command. NEWESTWEB is a variable, and a dollar sing in front makes it a dereference, that is the value of the variable is replaced on that spot.

    I created a test directory tree:
    Code:
    $ tree backup/
    backup/
    ├── web3
    │   ├── db_c1wppass_2019-09-15_00-01.sql.gz
    │   ├── db_c1wppass_2019-09-16_00-02.sql.gz
    │   ├── db_c1wppass_2019-09-17_00-01.sql.gz
    │   ├── db_c1wppass_2019-09-18_00-01.sql.gz
    │   ├── db_c1wppass_2019-09-19_00-01.sql.gz
    │   ├── web3_2019-09-15_00-01.tar.gz
    │   ├── web3_2019-09-16_00-01.tar.gz
    │   ├── web3_2019-09-17_00-01.tar.gz
    │   ├── web3_2019-09-18_00-01.tar.gz
    │   └── web3_2019-09-19_00-01.tar.gz
    ├── web4
    │   ├── db_c1pass_2019-09-16_00-02.sql.gz
    │   ├── db_c1pass_2019-09-17_00-01.sql.gz
    │   ├── db_c1pass_2019-09-18_00-01.sql.gz
    │   ├── db_c1pass_2019-09-19_00-01.sql.gz
    │   ├── web4_2019-09-16_00-01.tar.gz
    │   ├── web4_2019-09-17_00-01.tar.gz
    │   ├── web4_2019-09-18_00-01.tar.gz
    │   └── web4_2019-09-19_00-01.tar.gz
    └── web5
        ├── web5_2019-09-16_00-01.tar.gz
        ├── web5_2019-09-17_00-01.tar.gz
        ├── web5_2019-09-18_00-01.tar.gz
        └── web5_2019-09-19_00-01.tar.gz
    
    3 directories, 22 files
    
    Tested with this code:

    Code:
    #!/bin/bash
    
    #Comment out following line to stop echoing every line executed
    set -x
    
    TIMESTAMP=$( date +%F )
    BACKUP_DIR="/tmp/Test/backup/"
    cd $BACKUP_DIR
    NEWESTWEB=$( ls -t */*.tar.gz | head -n 1 )
    echo $NEWESTWEB
    NEWESTSQL=$( ls -t */*.sql.gz | head -n 1 )
    echo $NEWESTSQL
    
    for d in $BACKUP_DIR/web*
    do
        echo $d
        NEWESTWEB=$(ls -t $d/*.tar.gz | head -n 1)
        NEWESTSQL=$(ls -t $d/*.sql.gz | head -n 1)
        ls -l $NEWESTWEB
        if [ "$NEWESTSQL" != "" ]; then
        ls -l $NEWESTSQL
        else
        echo "NEWESTSQL is empty"
        fi
    done
    With this result when set -x is commented out:
    Code:
    $ ./tomjohn.sh
    web5/web5_2019-09-16_00-01.tar.gz
    web4/db_c1pass_2019-09-16_00-02.sql.gz
    /tmp/Test/backup//web3
    -rw-r--r-- 1 tale tale 0 syys  20 11:39 /tmp/Test/backup//web3/web3_2019-09-15_00-01.tar.gz
    -rw-r--r-- 1 tale tale 0 syys  20 11:39 /tmp/Test/backup//web3/db_c1wppass_2019-09-17_00-01.sql.gz
    /tmp/Test/backup//web4
    -rw-r--r-- 1 tale tale 0 syys  20 11:40 /tmp/Test/backup//web4/web4_2019-09-16_00-01.tar.gz
    -rw-r--r-- 1 tale tale 0 syys  20 11:40 /tmp/Test/backup//web4/db_c1pass_2019-09-16_00-02.sql.gz
    /tmp/Test/backup//web5
    ls: tiedostoa '/tmp/Test/backup//web5/*.sql.gz' ei voi käsitellä: Tiedostoa tai hakemistoa ei ole
    -rw-r--r-- 1 tale tale 0 syys  20 11:41 /tmp/Test/backup//web5/web5_2019-09-16_00-01.tar.gz
    NEWESTSQL is empty
    

    Try Internet Search Engines with
    Code:
    bash programming
     
  5. Tom John

    Tom John Active Member HowtoForge Supporter

    Hi,
    Thanks so much for your big help.
    OMG you explained everything so detailed to me and more over you pushed me forward that i can finish my project. I thank you a lot for your kind help... thanks so much
     

Share This Page