Shell / Bash - Script continues before finishing the line that is running

1

I have a shell / bash script that works perfectly for making backups, the problem is that I have large files that are giving problems in running the script. The script has to compress the file in tar.gz format and it does this, but when it arrives at 6GB + or- the script continues to compress the file but passes the next lines and the backups are faulty, the server must have a set_time_limit;

The script:

MYSQLDUMP="$(which mysqldump)"
$MYSQLDUMP -u $DBUSER -h $DBHOST -p$DBPASS $DBNAME | gzip > $TIMESTAMP.sql.gz

ssh $USER_SSH@$HOST_SSH "tar -zcf - $HOME" > $TIMESTAMP.backup.tar.gz

tar -zcf $TIMESTAMP.tar.gz $TIMESTAMP.backup.tar.gz $TIMESTAMP.sql.gz

SUCCESS=$?

rm $TIMESTAMP.sql.gz
rm $TIMESTAMP.backup.tar.gz

I did not put the variants because I think it is not necessary.

Before ending set_time_limit(0); it removes the 2 files from the end lines ... if the file is less than about 6 GB or 7GB this does not happen

    
asked by anonymous 09.08.2016 / 20:03

2 answers

1

You can ask the SSH client to inform the server that it is every seconds n seconds by entering the following option:

ssh -o ServerAliveInterval=30 $USER_SSH@$HOST_SSH ...

In this example: 30 seconds. The parameter is -o ServerAliveInterval=<tempo-em-segundos> .

More details here .

    
10.08.2016 / 16:16
1

There are a few alternatives.

One of them is the command timeout .

  

Run a command with a time limit, execute the command provided   and ends if it is still running after the time interval   specified.

     

Where the time limit can be:

     
  • s to seconds (default)

  •   
  • m to minutes

  •   
  • h per hour

  •   
  • d for days

  •   

Syntax:

timeout <opções> <duração> <comando> <args..>

Example:

timeout 10 ./script1.py

The above example will execute the file script1.py , if after 10s the file is still running, it is exited.

Another alternative is:

./script1.py& sleep 10; kill $!

The above line will run the file script1.py in the background, wait 10 seconds, and end the process by specifying pid in $! .

Editing

You can also loop to check the return of the function responsible for doing the backup , after the end ask the user if he wants to proceed with the script or not.

#!/bin/bash

function iniciarBackup(){
    echo "Iniciando backup..."

    MYSQLDUMP="$(which mysqldump)"
    $MYSQLDUMP -u $DBUSER -h $DBHOST -p$DBPASS $DBNAME | gzip > $TIMESTAMP.sql.gz

    ssh $USER_SSH@$HOST_SSH "tar -zcf - $HOME" > $TIMESTAMP.backup.tar.gz
    tar -zcf $TIMESTAMP.tar.gz $TIMESTAMP.backup.tar.gz $TIMESTAMP.sql.gz

    return 0
}

function deletarArquivos(){
    rm "$TIMESTAMP.sql.gz"
    rm "$TIMESTAMP.backup.tar.gz"
}


iniciarBackup

while true ; do
   if [ $? -eq 0 ]; then
     echo "Operação de backup concluída!"
     read -p "Você deseja deletar os arquivos? [S/N]" resposta
     case $resposta in
        [Ss]* ) deletarArquivos; break;;
        [Nn]* ) exit;;
        * ) echo "Você deseja deletar os arquivos? [S/N]";;
    esac
   fi
done

echo "Fim do script."
    
09.08.2016 / 20:10