G
G
glem13372019-09-09 13:50:51
linux
glem1337, 2019-09-09 13:50:51

Does it work through the console, does it work with cron?

crontab -e:
There 30 13 * * * /home/backup/delete.sh bitmirpack /home/backup >> /tmp/debug_cron 2>&1
script delete.sh

#!/bin/bash
SAVE_LIMIT=2

kernel_name=$1
backup_dir=$2
# clean old backup
clean_old_backup_files(){
    LIMIT_DAYS=$(date -d "$SAVE_LIMIT day ago" +%d.%m.%Y)
    TAR_OLD=$(find $backup_dir -name "www_backup_${kernel_name}_${LIMIT_DAYS}.tar.gz" | wc -l)
    if ; then
        rm -f "www_backup_${kernel_name}_${LIMIT_DAYS}.tar.gz"
    fi
        SQL_OLD=$(find $backup_dir -name "mysql_dump_${kernel_name}_${LIMIT_DAYS}.sql" | wc -l)
    if ; then
        rm -f "mysql_dump_${kernel_name}_${LIMIT_DAYS}.sql"
    fi
    SQL_AFTER_CONNECT_OLD=$(find $backup_dir -name "mysql_dump_${kernel_name}_${LIMIT_DAYS}_after_connect.sql" | wc -l)
    if ; then
        rm -f "mysql_dump_${kernel_name}_${LIMIT_DAYS}_after_connect.sql"
    fi
}

clean_old_backup_files

Folder contents (permissions, file owners):
-rw-r--r-- 1 root root 44 Sep 9 11:18 mysql_dump_bitmirpack_09/07/2019_after_connect.sql
-rw-r--r-- 1 root root 435986705 Sep 9 11: 18 mysql_dump_bitmirpack_09 /07/2019.sql
-rwxr--r-- 1 root root 873 Sep 9 13:21 delete.sh
-rw-r--r-- 1 root root 2180485120 Sep 9 11:21 www_backup_bitmirpack_09/07/2019.tar.gz
Today is 09/09/2019, which means that the files on 09/07/2019 should be deleted as a result, but as described in the topic of the task, deletion is performed only if run through the console. /tmp/debug_cron is empty.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
SunTechnik, 2019-09-09
@glem1337

1. By the modification date of the /tmp/debug_cron file, understand whether the job from cron starts at all or not.
2. If the task starts, after #!/bin/bash enter the line
set -x
In the log we will see which commands were executed with which arguments.
If a part of a large script is given, then it must be taken into account that the environment (env) in cron is different from the environment in the terminal. Maybe some variables are missing.... Although the script seems to be simple...
3. Relative names are used in the script. The current directory for a cron job may be different.
In the script, add the line at the beginning: cd <desired directory>

I
iddqda, 2019-09-09
@iddqda

Most likely a problem with environment variables
where should the script look for bitmirpack?
A recent article on Habré is just about this

P
poige, 2019-09-09
@poige

Does it work through the console, does it work with cron?

From the console, I suppose, it works in the right directory, but cron does not launch it in it.

V
vreitech, 2019-09-09
@fzfx

30 13 * * * /bin/bash /home/backup/delete.sh bitmirpack /home/backup >> /tmp/debug_cron 2>&1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question