2

i want to run a cron job that back ups my mysql database and my wordpress files once a day from the hostgator cpanel.

i found a sample script and edited the parameters with my information but it doesn't seem to be working properly. I don't have much experience with cron jobs so i'm not sure what all my issues are.

i have the .sh file named, backups.sh, saved in the home directory and have a folder named backups with subfolders database and wordpress.

this is the .sh file. i replaced {my info} with my credentials and filled out everything for my database info:

#!/bin/bash
# Script Function:
# This bash script backups up the db everyday dependent on 
# when you set the cron job to run with a file name time stamp 
# and tar.gz zips the file.
# The db will be saved in /backups/database/
# Db backups older than 5 days will be deleted.\
#[Changes Directory]
cd /home/{my info}/backups/

#[Old DB Deletion and Files Script]
find /home/{my info}/backups/database -name "*.tar.gz" -mtime +5 -exec rm -f {};
find /home/{my info}/backups/wordpress -name "*.tar.gz" -mtime +5 -exec rm -f {};

#[Stamps the file name with a date]
TIMESTAMP=`date +%m-%d-%y-%H%M`

#[DB Backup Scripts]
# DB Name
HOST=localhost
DBNAME=""
USER=""
PASSWORD=""
DUMP_PATH=/home/{my info}/backups/database/
BACK_PATH=/home/{my info}/backups/wordpress/
mysqldump --opt -c -e -Q -h$HOST -u$USER -p$PASSWORD $DBNAME > $DBNAME.sql
tar czpf $DUMP_PATH/$DBNAME.$TIMESTAMP.tar.gz $DBNAME.sql

rm -f $DBNAME.sql
#Backing up Wordpress files @ root
tar czf $BACK_PATH/wordpress.$TIMESTAMP.tar.gz /home/{my info}/public_html/mydomain

Here is what I have for the command line:

/bin/sh ~/backups.sh

i get an email after the cron job executes and it notifies me saying, "No such file or directory", along with a bunch of command not found.

justWired
  • 99
  • 2
  • 11

2 Answers2

1

cron does not always have the same PATH variable as your login shell, and therefore cannot always locate the executable for things like mysqldump or even tar.

I recommend using the full path to each command, ie: /usr/bin/mysqldump instead of just mysqldump. You can easily determine what the path is by using the which command.

$ which mysqldump
/usr/bin/mysqldump
Sammitch
  • 30,782
  • 7
  • 50
  • 77
  • @ Sammitch, i revised it and it seems to be working. i do have a few questions if you or anyone don't mind. Firstly, i moved the shell script and the backups folder to the public_html root directory. I also added what you mentioned using the full path but left out the $ which mysqldump (wanted to try this first). ran the cron job and both the db and wordpress files got backed up. i got an email saying this however: find: missing argument to `-exec' find: missing argument to `-exec' tar: Removing leading `/' from member names. should i be worried about this? – justWired Oct 25 '12 at 06:21
  • Do not worry about `tar: Removing leading '/' from member names.`, that is tar protecting you from shooting yourself in the foot later. The errors from `find` are strange, and may mean that the backups older than 5 days are not being removed by the script. – Sammitch Oct 25 '12 at 16:03
  • good to know. there are no backup files older than 5 days yet so maybe that could be why i'm getting that error message. dunno. – justWired Oct 26 '12 at 06:30
0

You may use below bash script which backups wordpress website and database. Full details here -> https://www.easyaslinux.com/tutorials/how-to-periodically-backup-your-website-and-mysql-database-to-local-server-as-well-as-s3-bucket-using-bash-script/

#!/bin/bash

###################################
backup_dir="give_directory_here"
webapp_path="website_patj"
database_name="testdb"
database_user="dbuser"
database_pwd="password"
database_host="db_host_name.net"
retention_days=10
##################################

date=`date +%d-%m-%y`
path="$backup_dir$date"
echo $date
mkdir -p $path > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "-Successfully  created directory $path"

    mysqldump -u $database_user -p$database_pwd -h $database_host $database_name > $path/db_backup.sql

    if [ $? -eq 0 ]; then
        echo "-Successfully created database dump"

        tar -czvf  $path/backup_with_db.tar.gz $webapp_path $path/db_backup.sql > /dev/null 2>&1

        if [ $? -eq 0 ]; then
                echo "-Successfully completed file + db  backup process"
        rm -rf $path/db_backup.sql

                old_date=`date --date="$retention_days day ago" +%d-%m-%y`
                old_path="$backup_dir$old_date"

                ls $old_path > /dev/null 2>&1
                if [ $? -eq 0 ]; then
                        rm -rf $old_path > /dev/null 2>&1
            if [ $? -eq 0 ]; then
                            echo "-Sucessfully removed old backup on $old_date"
                    else
                            echo "-Failed old backup removal $old_path" && exit 1
                    fi
        fi

        else
                echo "-Failed file +db backup process" && exit 1
        fi

    else
        echo "-Failed creating database dump, backup process failed" && exit 1
    fi

else
    echo "-Failed creating directory $path, backup process failed" && exit 1

fi
Nijil
  • 356
  • 3
  • 9