程式碼:
#!/usr/local/bin/bash
BasePath="/PATH/TO/BACKUP/DIRECTORY"
PATH=$PATH
BackupConfig(){
if [ -z $1 ]
# Checks if any params.
then
echo "No parameters passed to function."
return 0
else
ProgName=$1
fi
if [ $2 ]
then
BackupFolder=$2
fi
case $ProgName in
postfix) CMD="postconf -n"
;;
sysctl) CMD="sysctl -a"
;;
pkglist) CMD="ls /var/db/pkg"
;;
*) echo "Do not support this program $ProgName"
;;
esac
mkdir -p $BasePath/$BackupFolder
rm -f $BasePath/$BackupFolder/${ProgName}_old.txt
echo "Check if had OLD data : "
if [ -f $BasePath/$BackupFolder/${ProgName}_now.txt ]; then
echo " Had OLD data! Rename it to ${ProgName}_old.txt"
mv $BasePath/$BackupFolder/${ProgName}_now.txt $BasePath/$BackupFolder/${ProgName}_old.txt
else
echo " Do not have OLD data."
fi
echo "Backup configuration of ${ProgName} now."
$CMD > $BasePath/$BackupFolder/${ProgName}_now.txt
echo "Generate diff log : "
if [ -f $BasePath/$BackupFolder/${ProgName}_old.txt ]; then
echo " Generate diff log between ${ProgName}_new.txt and ${ProgName}_old.txt."
diff $BasePath/$BackupFolder/${ProgName}_now.txt $BasePath/$BackupFolder/${ProgName}_old.txt > $BasePath/$BackupFolder/diff-`date +"%Y%m%d"`.txt
find $BasePath/$BackupFolder/ -type f -size 0 | xargs rm -f
else
echo " Do not have OLD data."
fi
}
### Backup postfix setting
BackupConfig postfix postfix_config_backup
### Backup sysctl setting
BackupConfig sysctl sysctl_config_backup
### Backup /var/db/pkg
BackupConfig pkglist installed-pkglist
No comments:
Post a Comment