Linux
我在哪裡可以找到高級主目錄備份腳本
是否有任何用於 linux 的腳本可以將主目錄和 mysql 數據庫備份在不同的目錄中。我的意思是最好的通用腳本,任何人都可以使用該腳本。
通過備份我的意思是,我只是製作 tar.gz 並複製到單獨的目錄
使用各種不常用但完美的高級命令的東西
如果您正在尋找的只是一個不錯的備份腳本,請查看我的網站,因為我在幾週前寫了一個操作方法。
編輯:
#!/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 "==============================================================================="`