Learn about Centmin Mod LEMP Stack today
Register Now

Sysadmin My Cronjob Doesn't Work

Discussion in 'System Administration' started by huaanhhuy, Jul 5, 2020.

  1. huaanhhuy

    huaanhhuy New Member

    25
    0
    1
    Jun 10, 2020
    Ratings:
    +1
    Local Time:
    10:56 AM
    Hi @eva2000, thanks for creating Centminmod. I make my own backup script and use rclone addon to store my backup files to my Google Drive.

    I created a bash script named backup.sh and located in root folder. When I run my script by command below, my script working OK
    Code (Text):
    /root/backup.sh


    Then I use crontab -e to make a cronjob below for auto backup my data files once a day at 2PM

    Code (Text):
    0 2 * * * /root/backup.sh >/dev/null 2>&1



    But the cronjob doesn't do the job, i try to run /root/backup.sh manually and it still works. Can you help me how to config correct cronjob for this situation. Thank you
     
  2. eva2000

    eva2000 Administrator Staff Member

    55,245
    12,253
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,833
    Local Time:
    1:56 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    what's contents of /root/backup.sh ?

    for posting code or output from commands to keep the formatting, you might want to use CODE tags for code How to use forum BBCODE code tags :)
     
  3. huaanhhuy

    huaanhhuy New Member

    25
    0
    1
    Jun 10, 2020
    Ratings:
    +1
    Local Time:
    10:56 AM
    Hi here is my backup content script

    Code:
    #!/bin/bash
    
    SERVER_NAME=huaanhhuy_backup ## Folder in Google Drive
     
    TIMESTAMP=$(date +"%F")
    BACKUP_DIR="/root/backup/$TIMESTAMP"
    MYSQL_USER="root"
    MYSQL=/usr/bin/mysql
    MYSQL_PASSWORD="VCGaa*******QWOeXZRX7Z5l" ## Root MySQL Password
    MYSQLDUMP=/usr/bin/mysqldump
    SECONDS=0
     
    mkdir -p "$BACKUP_DIR/mysql"
    
    echo "Backup Database In Process";
    databases=`$MYSQL --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema|mysql)"`
     
    for db in $databases; do
        $MYSQLDUMP --force --opt --user=$MYSQL_USER -p$MYSQL_PASSWORD $db | gzip > "$BACKUP_DIR/mysql/$db.gz"
    done
    echo "Backup Database Successful";
    echo '';
     
    echo "Backup Website In Process";
    # Loop through /home directory
    for D in /home/nginx/domains/*; do
        if [ -d "${D}" ]; then #If a directory
            domain=${D##*/} # Domain name
            echo "- "$domain;
        cd /home/nginx/domains/$domain/public
            zip -r $BACKUP_DIR/$domain.zip * -q -x /home/nginx/domains/$domain/public/wp-content/cache/**\* #Exclude cache
        fi
    done
    echo "Backup Website Successful";
    echo '';
    
    
    echo " Backup to Google Drive";
    /usr/sbin/rclone --transfers=1 move $BACKUP_DIR "huaanhhuy:$SERVER_NAME/$TIMESTAMP" >> /var/log/rclone.log 2>&1  #remote: remote config name created in previous step.
    # Clean up
    rm -rf $BACKUP_DIR  #Delete backup directory on VPS
    /usr/sbin/rclone -q --min-age 7d delete "huaanhhuy:$SERVER_NAME" #Remove all backups older than 7 day on Google Drive Backup Directory
    /usr/sbin/rclone -q --min-age 7d rmdirs "huaanhhuy:$SERVER_NAME" #Remove all empty folders older than 7 day Google Drive Backup Directory
    /usr/sbin/rclone cleanup "huaanhhuy:" #Cleanup Trash , Delete all files in trash directory
    echo "Backup Gooogle Drive Successful";
    echo '';
     
    duration=$SECONDS
    echo "Total $size, $(($duration / 60)) minutes and $(($duration % 60)) seconds elapsed."
    
    FYI, huaanhhuy_backup is my backup Folder on Google Drive, huaanhhuy is my rclone config name at the creating config step. And my cronjob is on above post
     
  4. eva2000

    eva2000 Administrator Staff Member

    55,245
    12,253
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,833
    Local Time:
    1:56 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    does /var/log/rclone.log even populate anything on cronjob runs or no new entries ? and is
    BACKUP_DIR directory created on cronjob scheduled time runs or the directory not created ?

    is cronjob listed in output for cronjob listing command
    Code (Text):
    crontab -l

    maybe you didn't save the crontab -e new entry properly via nano linux text editor ?
     
  5. huaanhhuy

    huaanhhuy New Member

    25
    0
    1
    Jun 10, 2020
    Ratings:
    +1
    Local Time:
    10:56 AM
    I checked my cronjob listing and it still have my rclone backup cronjob.

    Code:
    13 23 * * * /usr/local/src/centminmod/tools/autoprotect.sh >/dev/null 2>&1
    0 */4 * * * /usr/bin/cminfo_updater 2>/dev/null
    22 */12 * * * /usr/local/src/centminmod/tools/csfcf.sh auto >/dev/null 2>&1
    0 0 * * 5 /usr/local/src/centminmod/tools/cf-authenticated-origin-cert-update.sh update >/dev/null 2>&1
    19 02 * * * /root/tools/phpmyadmin_update.sh >/dev/null 2>&1
    32 0 * * * "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null
    */15 * * * * sleep 246s ; wget -O - -q -t 1 http://tie***.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1
    0 */8 * * * sleep 181s ;/root/tools/wp_updater_tie***.com.sh >/dev/null 2>&1
    */15 * * * * sleep 298s ; wget -O - -q -t 1 http://ahs***.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1
    0 */8 * * * sleep 145s ;/root/tools/wp_updater_ahs***.com.sh >/dev/null 2>&1
    */15 * * * * sleep 161s ; wget -O - -q -t 1 http://wi***.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1
    0 */8 * * * sleep 285s ;/root/tools/wp_updater_wi***.com.sh >/dev/null 2>&1
    */15 * * * * sleep 269s ; wget -O - -q -t 1 http://di***.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1
    0 */8 * * * sleep 270s ;/root/tools/wp_updater_di***.com.sh >/dev/null 2>&1
    0 2 * * * /root/backup.sh > /dev/null 2>&1
    */15 * * * * sleep 760s ; wget -O - -q -t 1 http://xuong*****.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1
    0 */8 * * * sleep 166s ;/root/tools/wp_updater_xuong*****.com.sh >/dev/null 2>&1
    #*/15 * * * * sleep 616s ; wget -4 -O - -q -t 1 http://xu****y.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1
    0 */8 * * * sleep 215s ;/root/tools/wp_updater_xu****y.com.sh >/dev/null 2>&1
    

    My /var/log/rclone.log has nothing in it. When I run the backup script manually via /root/backup.sh command, the backup process is OK. The problem is that cronjob I set not running at the time I set up.
     
  6. tininho

    tininho Active Member

    182
    44
    28
    May 22, 2019
    eu
    Ratings:
    +135
    Local Time:
    6:56 AM
  7. huaanhhuy

    huaanhhuy New Member

    25
    0
    1
    Jun 10, 2020
    Ratings:
    +1
    Local Time:
    10:56 AM
    Thanks, but i think it belongs to system cron. I need to run
    0 2 * * * /root/backup.sh > /dev/null 2>&1
    for my backup script
     
  8. eva2000

    eva2000 Administrator Staff Member

    55,245
    12,253
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,833
    Local Time:
    1:56 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
  9. huaanhhuy

    huaanhhuy New Member

    25
    0
    1
    Jun 10, 2020
    Ratings:
    +1
    Local Time:
    10:56 AM
    @eva2000

     
  10. huaanhhuy

    huaanhhuy New Member

    25
    0
    1
    Jun 10, 2020
    Ratings:
    +1
    Local Time:
    10:56 AM
    Hi @eva2000 I have granted executive permission for backup.sh

    Code:
    chmod +x /root/backup.sh

    and the permission is

    Code:
    getfacl: Removing leading '/' from absolute path names
    # file: root/backup.sh
    # owner: root
    # group: root
    user::rwx
    group::r-x
    other::r-x