Skip to content

Instantly share code, notes, and snippets.

@kubaceg
Last active May 1, 2017 11:32
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kubaceg/8be927d5a0deddd5c96d to your computer and use it in GitHub Desktop.
Save kubaceg/8be927d5a0deddd5c96d to your computer and use it in GitHub Desktop.
n98Magerun magento database backup script, creates daily, weekly and monthly dumps
#!/bin/bash
#CONFIG
backupDir='/mnt/backup'
magentoDir='/var/www/magento/htdocs'
magerunPath='/usr/bin/n98-magerun.phar'
#NUMBER OF DAYS TO KEEP BACKUPS
dailyKeepDays=7
weeklyKeepDays=30
monthlyKeepDays=300
#DATE FORMAT FOR BACKUP FILES
today=`date +"%d-%m-%y"`
monthDay=`date +"%d"`
weekDay=`date +"%u"`
function checkBackupDir
{
if [ ! -d $actualBackupDir ]
then
mkdir -p $actualBackupDir
fi
}
function makeDump
{
cd $magentoDir
$magerunPath db:dump --compression="gzip" --strip="@stripped" -q $destination
}
function generateDestination
{
actualBackupDir=$backupDir/$1
destination=$actualBackupDir/$today.sql
}
if [ "$monthDay" -eq 1 ] ; then
generateDestination backup.monthly
else
if [ "$weekDay" -eq 7 ] ; then
generateDestination backup.weekly
else
generateDestination backup.daily
fi
fi
checkBackupDir
makeDump
#Clean old daily backups
if [ -d $backupDir/backup.daily ] ; then
find $backupDir/backup.daily/ -maxdepth 1 -mtime +$dailyKeepDays -type f -exec rm -rv {} \;
fi
#Clean old weekly backups
if [ -d $backupDir/backup.weekly ] ; then
find $backupDir/backup.weekly/ -maxdepth 1 -mtime +$weeklyKeepDays -type f -exec rm -rv {} \;
fi
#Clean old monthly backups
if [ -d $backupDir/backup.monthly ] ; then
find $backupDir/backup.monthly/ -maxdepth 1 -mtime +$monthlyKeepDays -type f -exec rm -rv {} \;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment