Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
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? –  SvW 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 –  Mirror51 May 1 '11 at 2:03

2 Answers 2

up vote 4 down vote accepted

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 "==============================================================================="`
share|improve this answer
    
thanks buddy that was good –  Mirror51 May 1 '11 at 5:08
    
your blog may not always be there - can you edit your answer and add the script please? –  Iain May 1 '11 at 9:53
    
+1 - very comprehensive. nice work. –  Cypher May 1 '11 at 21:02
    
Cypher, thanks for the kudos! –  jdcarg May 1 '11 at 21:09
    
Thanks for posting the script –  Iain May 1 '11 at 21:17

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.

share|improve this answer
    
i need shell script which does cp and then tar , i don't need any software –  Mirror51 May 1 '11 at 2:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.