-1

Is there any script for linux which can backup the home directories and mysql databases in separate directories . i mean best of all scripts which is general and any one can use that one.

By backup i mean , i just to makr tar.gz and copy to separate directories

Something which uses all sort of advacned command which are not commonly used but perfect

Mirage
  • 561
  • 4
  • 10
  • 25
  • 1
    You have to give us a lot more background because there are so many variables with backup: How much data? Does it change a lot? Can MySQL go offline for backup? How big is the database? Again, how much does it change? How often do you want to do a backup? What is the target media? – Sven Apr 30 '11 at 16:24
  • I have 20 websites around 15GB, 40 databases , no offline mysql , it chnages not much often but yes , i want daily backup , target media is another folder in directory – Mirage May 01 '11 at 02:03

2 Answers2

4

If all you are looking for is a decent backup script, check out my site as I wrote up a how-to on this a few weeks back.

Bash Backup Script

Edit:

#!/bin/bash                                                                                                 

##########################                                                                                  
##### Config Section #####                                                                                  
##########################                                                                                  
#Directory to Backup
backupSourceDir="/var/www"

#Directory to backup to
backupDestDir="/var/backups/www/content"

#Time stamp
suffix=$(date +%Y%m%d-%H%M) 

#Prefix Name of backup
backupFileName="content_backup"

#Where to log
logDir="/var/log/backups/"

#If available disk space dips below X number of Gigabytes, Do Not Backup.
diskSizeToMaintain="5" 

#How many days to keep backups. If older than X amount of days, DELETE.
daysToKeepBackups="14"

#Do not keep less than X number of backups.
numBackupsToKeep="10" 


###########################                                                                                 
######## Functions ###########                                                                                 
##########################
function e {
while [ "$1" != "" ]; do
    echo -n "$1"
    echo -n " "
    echo -n "$1" >> $logDir"/"$backupFileName.$suffix.log
    echo -n " "  >> $logDir"/"$backupFileName.$suffix.log
    shift
done
echo
echo >> $logDir"/"$backupFileName.$suffix.log
}

function backupPrep {
    e "Preparing backup..."
    if [ ! -d $logDir ] #Does Log Directory Exist?    
    then
    e "Log directory" $logDir "doesn't exist."
    exit 1
    else
    e "Log Directory Exists..."
    fi

    if [ ! -d $backupSourceDir ] #Does Backup Source Directory Exist?
    then
    e "Source directory" $backupSourceDir "doesn't exist."
    exit 1
    else
    e "Source Directory Exists..."
    fi

    if [ ! -d $backupSourceDir ] #Does Backup Directory Exist?
    then
    e "Destination directory" $backupDestDir "doesn't exist."
    exit 1
    else
    e "Destination directory" $backupDestDir "exists..."
    fi
}

function backupCheck {
        e "Checking disk space..."
    sourceDirSize=$(du -h $backupSourceDir/ | grep $backupSourceDir | tail -1 | awk '{print $'1'}') #Get Source Directory Size
    diskAvail=$(df -h $backupSourceDir | tail -1 | awk '{print $'4'}') #Get Disk Space Available
    a=$(echo $sourceDirSize | sed s/.$//) #Strip G off backup size
    b=$(echo $diskAvail | sed s/.$//) #Strip G off disk size
    diskAfterBackup=$(echo $b - $a | bc) #Is Sourcce Directory Size Larger than Disk Size to Maintain? See config. 
    # See if there is enough disk space available
    comp=$(echo "a=$diskAfterBackup;b=$diskSizeToMaintain;r=0;if(b<=a)r=1;r"|bc)
    e "Comparing disk space to diskSizeToMaintain..."
        if [ $comp -eq 0 ]
    then
    e "Not enough space left on disk or you need to change config section for diskSizeToMaintain"
    e "Disk Size To Maintain:" $diskSizeToMaintain 
    e "Directory Size:" $sourceDirSize 
    e "Disk Size Available:" $diskAvail
    exit 1
     fi    
}
function backupPerform {
    e "Performing backup..."
    cd $backupDestDir #Go to backup Dirctory
    tar hcfP - $backupSourceDir | gzip -c > $backupFileName.$suffix.tar.gz #Perform Backup

    if [ $? != 0 ];
    then
        e "Backup failed! Check the log file by running 'less" $logDir"/"$backupFileName.$suffix.log"'"
        exit 1
    else
            e "Backup has completed..."
    fi
}
function backupVerify {
    e "Verifying backup..."
    if [ -f $backupDestDir/$backupFileName.$suffix.tar.gz ] #Does backup file Exist? If so grab info to display in report.
    then
    destDirSize=$(ls -ltrah $backupDestDir/$backupFileName.$suffix.tar.gz | awk '{print $'5'}' | tail -1)
    e "Backup successful! Backup file" $backupDestDir/$backupFileName.$suffix.tar.gz "was generated and is" $destDirSize 
    diskAfterBackup=$(df -h $backupSourceDir | tail -1 | awk '{print $'4'}')
    else
    e "Backup failed! Backup file" $backupDestDir/$backupFileName.$suffix.tar.gz "didn't get created."
    exit 1
    fi
}
#Delete backups older than daysToKeepBackups
function backupClean {
    e "Checking daysToKeepBackups..."
    if [ $daysToKeepBackups != 0 ]
    then
    e "==============================================================================="
    e "========================== DELETE OLD BACKUPS ================================="
    e "==============================================================================="
    e "Deleting older backups..."
    countTotalBackups=$(find $backupDestDir/ -name $backupFileName\* | wc -l) #Count all backups
    countBackupsToDelete=$(find $backupDestDir/ -name $backupFileName\* -mtime "+"$daysToKeepBackups | wc -l) #Count backups to be deleted based daysToKeepBackups
    countBackups=$(echo $countTotalBackups - $countBackupsToDelete | bc) #countTotalBackups - countBackupsToDelete = countBackups

    if [ $countBackups -ge $numBackupsToKeep  ] 
        then
    e "Backups that will be deleted..."
        find $backupDestDir/ -name $backupFileName\* -mtime "+"$daysToKeepBackups #List backups to be deleted.
        find $backupDestDir/ -name $backupFileName\* -mtime "+"$daysToKeepBackups >> $logDir"/"$backupFileName.$suffix.log
    e "Performing delete..."
        find $backupDestDir/ -name $backupFileName\* -mtime "+"$daysToKeepBackups -exec rm {} \; #Delete backups.                                                                                
    else
        e "Can't delete backups." $numBackupsToKeep "numBackupsToKeep is less than" $countBackups "..."
    fi
    else
    e "0 was specified for daysToKeepBackups in config section. No files deleted."
    fi
}
###########################                                                                                 
######## Script ###########                                                                                 
##########################
e "==============================================================================="
e "=========================== RUNNING BACKUP ===================================="
e "==============================================================================="
startDate=$(date +%Y%m%d-%H:%M:%S) # Time Started                                                           

backupPrep
backupCheck
backupPerform
backupVerify
backupClean

finishDate=$(date +%Y%m%d-%H:%M:%S) # Time Finished                                                         

e "==============================================================================="
e "============================== DETAILS ========================================"
e "==============================================================================="
e "Backup Started:" $startDate
e "Backup Finished:" $finishDate
e "Source Directory:" $backupSourceDir
e "Source Directory Size:" $sourceDirSize
e "Destination Directory:" $backupDestDir
e "Destination File Archive Size:" $destDirSize
e "Backup File Created:" $backupDestDir"/"$backupFileName"."$suffix".tar.gz"
e "Disk Size To Maintain:" $diskSizeToMaintain
e "Disk Size Available:" $diskAfterBackup
e "# Backups to Keep:" $numBackupsToKeep
e "Days to Keep Backups:" $daysToKeepBackups
e "==============================================================================="
e "==============================================================================="
e "==============================================================================="`
jdcarg
  • 76
  • 4
0

A good starting point may be the list of backup software on Wikipedia. Maybe one of the tools listed there has all the features you require.

Axel Knauf
  • 1,600
  • 1
  • 10
  • 12