Linux: Backup Shell Script for Joomla

The following is an example of backup Joomla website filesystem and database, keeping always the newest backups, using shell script:

#!/bin/bash

#this script is in: /home/html/myScripts/scripts
#backup is in: /home/admin/backup


sysdate=`date '+%Y%m%d'`
echo "This is the SYSDATE [TODAY()]: "$sysdate


#set the folder name of Joomla to be taken as backup:
prefix="my-joomla-site_v.1"

#backup Joomla file system:
#--------------------------

echo "Trying to make directory: "${prefix}_$(date +%Y%m%d)
mkdir /home/admin/backup/${prefix}_$(date +%Y%m%d)
echo "Copying the file system..."
cp -f -r -R /var/www/html/${prefix} /home/admin/backup/${prefix}_$(date +%Y%m%d)/${prefix}


#backup Joomla database:
#-----------------------

MUSER="root"
MPASS="****"

mysqldump -u $MUSER -p$MPASS --databases ${prefix} >
/home/admin/backup/${prefix}_$(date +%Y%m%d)/${prefix}.sql


#delete old backups (keep always the 10 most recent files)

cd /home/admin/backup

#find the files called like '%prefix%' and sort them by columns $6, $7 (date and time)
#[date starts at character 1 and time at 11]

the_list=`ls -l | grep $prefix | awk '{print $6,$7,$8}' | sort -r -b -n -k 1,11`

#set the delimiter because files have spaces, due to ls -l (IFS is the default delimiter in file loops)
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")


i=1
#loop through ls -l, but remove only folders greater than 10
for file in $the_list
do
if [ "$i" -gt "5" ]
then
rm -rf `expr $file | awk '{print $3}'`
echo "The file "$file" is deleted"
fi
i=`expr $i + 1`
done


#restore the delimiter
IFS=$SAVEIFS

#return to scripts directory:
cd /home/html/myScripts/scripts


Leave a Reply