Programming:Rob backup.sh

From Robupixipedia
Revision as of 05:54, 20 May 2014 by Thunderrabbit (talk | contribs) (post backup script)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Thanks Flominator

#!/bin/bash                                                                                                     

# before running, copy this file to the wiki root directory, in this case ~/wiki.robnugen.com/w/                

FNAME=wiki.rn.com.bak.`date +%Y-%m-%d_%H%M%S`
BACKUP_DIR=~/rn.com.back
BACKUP_DIR_ID_DOUBLECHECK=00_THIS_IS_THE_BACKUP_DIRECTORY.txt

mysqldump -h DBHOST -u DBUSER -pDBPASSWORD DBNAME > ${FNAME}.sql
zip -r ${BACKUP_DIR}/${FNAME}.zip images/ ${FNAME}.sql LocalSettings.php extensions/

# for quickly testing zip portion:                                                                              
# zip -r ${BACKUP_DIR}/${FNAME}.zip  LocalSettings.php                                                          
rm ${FNAME}.sql

cd ${BACKUP_DIR}

if [[ ! -f ${BACKUP_DIR_ID_DOUBLECHECK} ]]; then
   echo "NOT DELETING FILES because I'm not sure I'm in the backup directory."
   echo "I failed to find the file " ${BACKUP_DIR}/${BACKUP_DIR_ID_DOUBLECHECK}
   exit 1
fi

pwd
ls -al

#Count files in directory (hidden files (filename starts with a dot) are ignored)                               
file_count=`ls | wc -l`

#Do while there are more than or equal 6 files present                                                          
while [ $file_count -ge 6 ]
do
    rm `ls -tr | head -n 1`
    file_count=`ls | wc -l`
done

# make sure the file date is the very latest and therefore will not be wiped                                    
touch ${BACKUP_DIR}/${BACKUP_DIR_ID_DOUBLECHECK}