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

Asked

Viewed 1,282 times

1

I have a shell/bash script that works perfectly to make backups, the problem is that I have large files that are giving problems in the execution of the script. The script has to compress the file in the format tar.gz and it does this, but when it arrives at 6GB+or- the script continues to compress the file but passes to the next lines and the backups crashes, the server must have a set_time_limit; same in php, in the php file that calls the shell/bash I use set_time_limit(0); and works very well, the shell/bash has something similar ?

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 didn’t put the variables in because I don’t think it’s necessary.

Before the end of the tar it removes the 2 files from the end lines ... if the file is less than about 6 GB or 7GB this does not happen

  • I did not understand why to limit time. Because in that case could not interrupt the process? Your question would not be about synchronous execution?

  • The GNU tar has a parameter called --checkpoint with which you can create a callback. Thus, the next execution would only be invoked when the current process completes.

  • @Danielomine have any examples of using this --checkpoint ? if you can post an answer

2 answers

1

You can ask the SSH client to inform the server that it is active (Alive) each n seconds by inserting 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.

  • if I put the -o ServerAliveInterval=30 it deletes the files even faster, to try the opposite of what you said, putting -o ServerAliveInterval=3600, let’s see...

  • Didn’t work too :/

  • tar: ServerAliveInterval=3600: Cannot stat: No such file or directory

  • You are specifying the parameter in the ssh command or in the tar command?!

  • I’m putting the remote this way: ssh -o ServerAliveInterval=3600 $USER_SSH@$HOST_SSH "tar -zcf - $HOME" > $TIMESTAMP.backup.tar.gz

  • I tried this way and it worked normally (my SSH server is listening on port 9595): ssh 192.168.1.2 -p9595 -lvictor -o ServerAliveInterval=50 "tar cfzv /tmp/abc.tgz /tmp/abc" > abc.log

Show 1 more comment

1

There are some alternatives.

One of them is the command timeout.

Executes a command with a time limit, executes the given command and ends if it is still running after the time interval specified.

Where the time limit can be:

  • s for seconds (default)

  • m for minutes

  • h for hours

  • d for days

Syntax:

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

Example:

timeout 10 ./script1.py

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

Another alternative is:

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

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

Editing

You can also in a loop check the return of the function responsible for doing the backup, after the termination ask the user if he wants to proceed with the execution of 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."
  • @A.S. Here for me it worked, you can post the code on http://pastebin.com/ and put the link here?

  • @A.S. This is strange, try the following: http://pastebin.com/CaKyQ2xb. See if he asks for your confirmation.

  • @A.S. Worked out now? The script asks for confirmation to remove the files?

  • @A.S. What file system do you use on that server? Something else, run the script and see the logs of the system (here shows some forms).

  • How do I check the file system ? and on the system log I tried the ways the link q sent but I only have access to one, the command Tail, but I did not have permission running it...

  • @A.S. stat -f -c %T / in the terminal. Since you do not have access to logs, do the following: 1) on the tar -zc... replace with tar -czvf $TIMESTAMP.tar.gz $TIMESTAMP.backup.tar.gz $TIMESTAMP.sql.gz 1 2> foobar.log. 2) Execute the script. 3) The tar will create a log, foobar.log, view the contents of this file, like this: cat foobar.log and see if there are any errors.

  • @A.S. So.. got to see the log entries?

  • this is log @stderr: tar: Removing leading /' from member names&#xA;tar: Removing leading /' from member names&#xA;tar: /home/w_admin/backups/v2_1472236567.sql.gz: Cannot stat: No such file or directory&#xA;tar: Exiting with failure status due to previous errors&#xA;rm: cannot remove /home/w_admin/backups/v2_1472236567.sql.gz': No such file or directory

  • @A.S. It’s already something. : ) The first message is a warning (see), now the second, seems to indicate that for some reason one or more files could not be added to the tar, (see). If in the tar has the flag -v remove and rotate the script! Have you tried turning the script with privileges? ex.: sudo script1.sh

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.