Small WordPress backup script that sends email on failed backups and deletes old backups

This is a simple backup script in bash, for WordPress on Linux. It is based on the script from Small WordPress backup script post.
Like the script there it will dump MySQL database used by WordPress site and create a tar.gz file that will have WordPress site files and database dump.
This script will also send out an email if the backup process fails on any stage and delete old backups files.


bkpAge=90 #How many days of backup to keep

dbUser=$(grep DB_USER $webroot/wp-config.php | awk -F\' '{print$4}')
dbName=$(grep DB_NAME $webroot/wp-config.php | awk -F\' '{print$4}')
dbPassword=$(grep DB_PASSWORD $webroot/wp-config.php | awk -F\' '{print$4}')
dbDump="$bkpDir""$dbName"_$(date +'%F_%H_%M').sql


email="[email protected]"

rm -f "$errorFile"

mysqldump -u $dbUser -p$dbPassword $dbName > $dbDump || echo "$(date +'%F %T') MySQL backup of $dbName failed." >> "$errorFile"

tar -czf "$bkpDir"srvfail_$(date +'%F_%H_%M').tar.gz $webroot $dbDump || echo "$(date +'%F %T') Tar process failed." >> "$errorFile"

rsync -az $bkpDir -e "ssh -p $rSSHport" $rUser@$rHost:$rDir --delete || echo "$(date +'%F %T') rsync to $rHost failed." >> "$errorFile"

rm -f "$bkpDir"*$(date -d "$bkpAge days ago" +'%F')*

if test -f "$errorFile"; then
    cat "$errorFile" | mail -s "Backup process on $(hostname) failed" "$email"

It will create two backup files, one for database dump in format that looks like “dbname__2021-12-01-06-00.sql” and one tar.gz file that will look like “wpbackup_2021-12-01_06-00.tar.gz”.

Script can be saved as and put in crontab to run at a certain time like 6:00AM.

0 6 * * * /path/to/script/ >/dev/null 2>&1

or if you want to have output of it in case of errors

0 6 * * * /path/to/script/ > /var/log/wpbackup.log 2>&1

Find which directory is using most inodes

Oneliners to find which directory on the server is using most inodes:

To find how many inodes are used per each directory on certain path run:

find ./ -maxdepth 1 -type d -print0 | while IFS= read -r -d $'\0' dir; do echo "$(find "$dir" | wc -l) "$dir""; done| sort -n | tail

Last line will show the path being searched and second to last line will be directory with most inodes, then cd to that directory and run again to see which subfolder is using most inodes.

To get directory with most inodes immediately, without the need to cd in each directory you can run following:

find ./ -type d -print0 | while IFS= read -r -d $'\0' dir; do echo "$(stat -t "$dir" | awk '{print$2}') "$dir""; done | sort -n | tail

This will not show the number of inodes used, only which directory has most inodes in it.
Oneliner from top can be used to get exact number of inodes in it.

Small WordPress backup script

Simple backup script written in bash, for WordPress on Linux, that will dump MySQL database used by WordPress site and create tar.gz file consisting of WordPress site files and database dump



DBUSER=$(grep DB_USER $WEBROOT/wp-config.php | awk -F\' '{print$4}')
DBNAME=$(grep DB_NAME $WEBROOT/wp-config.php | awk -F\' '{print$4}')
DBPASSWORD=$(grep DB_PASSWORD $WEBROOT/wp-config.php | awk -F\' '{print$4}')
DBDUMP="$BKPDIR""$DBNAME"_$(date +"%Y-%m-%d-%H-%M").sql

#In case you want to rsync backups to remote server


tar -czvf "$BKPDIR"wpbackup_$(date +"%Y-%m-%d_%H-%M").tar.gz $WEBROOT $DBDUMP

rsync -az $BKPDIR -e "ssh -p $RSSHPORT" $RUSER@$RHOST:$RDIR

It will create two backup files, one for database dump in format that looks like “dbname_2018-01-04-06-00.sql” and one tar.gz file that will look like “wpbackup_2018-01-04_06-00.tar.gz”.

Script can be saved as and put in crontab to run at a certain time like 6:00AM.

0 6 * * * /path/to/script/ 2>/dev/null

It can also be run manually with bash

How to provide SSH password inside a script or oneliner

If you ever need to provide a password for SSH login inside a bash script or a shell command, to avoid being asked a password when SSH keys are not used, it can be done with usage of expect command, or sshpass utility.

Using expect

Expect is a program that “talks” to other interactive programs according to a script.

Lets say you want to SSH to a server and run a remote command, ls for instance, with a command like this:

ssh user@server "ls -lh file"

If you are not using SSH keys, you will be prompted a password, and will need to enter it manually.

If you want to avoid entering a password, and have it inputted to login prompt automatically you can use expect command.

Provide SSH password inside a script.

Expect reads cmdfile for a list of commands to execute. Expect may also be invoked implicitly on systems which support the #! notation by marking the script executable, and making the first line in your script:
#!/usr/local/bin/expect -f

Example of a script which runs a remote command over SSH, with password being provided inside a script.

#!/usr/bin/expect -f
spawn ssh [email protected] "ls /file"
expect "assword:"
send "mypassword\r"


Running a SSH command with provided password inside a one-liner, can be done using expect -c and then putting the commands inside single quotes.

The -c flag prefaces a command to be executed before any in the script. The command should be quoted to prevent being broken up by the shell. This option may be used multiple times. Multiple commands may be executed with a single -c by separating them with semicolons.

Example of a one-line command:

expect -c 'spawn ssh user@server "ls -lh file"; expect "assword:"; send "mypassword\r"; interact'

Using sshpass

SSH password prompt can also be bypassed by connecting with sshpass command, which is available in EPEL repo in CentOS

To install sshpass, first make sure you have EPEL repo on your server.

You can install EPEL with

yum -y install epel-release

Install sshpass with

yum -y install sshpass

You can then connect to remote server with sshpass using a command similar to this:

sshpass -p 'password' ssh user@server

If you have never connected to the server before, you will probably not get connected to remote server due to host key checking.
To bypass host key checking use -o StrictHostKeyChecking=no option:

sshpass -p 'password' ssh -o StrictHostKeyChecking=no  user@server

Find jobs for SysAdmins with Jooble