在哪里可以找到高级主目录备份脚本

在哪里可以找到高级主目录备份脚本

是否有任何适用于 Linux 的脚本可以将主目录和 mysql 数据库备份到单独的目录中。我的意思是所有脚本中最好的是通用的,任何人都可以使用它。

我所说的备份是指,我只需制作 tar.gz 并复制到单独的目录中

使用各种不常用但很完美的高级命令

答案1

如果您正在寻找一个不错的备份脚本,请查看我的网站,因为我几周前写了一份关于此内容的操作方法。

Bash 备份脚本

编辑:

#!/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 "==============================================================================="`

答案2

一个好的起点可能是维基百科上的备份软件列表。也许列出的某个工具具备您需要的所有功能。

相关内容