- 论坛徽章:
- 0
|
个人稍加修改,就成自己最顺手的脚本了。
注意配合crontab使用。多台机器可以结合脚本ftp来下载或者上传。
自动备份脚本
[root@ad12 william.w]# cat backup-1.sh
#!/bin/bash
# df -k
#Filesystem 1K-blocks Used Available Use% Mounted on
#/dev/sda2 113852040 10113912 97954732 10% /
#/dev/sda1 790588 24680 725748 4% /boot
#none 1028008 0 1028008 0% /dev/shm
#/dev/sda5 19362784 10617104 7762104 58% /export/home
#
# to avoid the error: TERM environment variable not set.
#
TERM=linux
export TERM
clear
cd /
echo
echo
echo =====================================
echo this tool is used to backup
echo =====================================
# to get the date
DATE=`date +%Y-%m-%d-%H`-tar
echo today is ${DATE%-*}
#to get the hostname
RNAME=`hostname | awk -F. '{printf $1}'`
echo the server name is $RNAME
# to get the IP
IP1=`ifconfig | grep "inet addr:" | grep -v "inet addr:127.0.0.1" | awk -F: '{printf $2}'`
IP2=${IP1%% *}
echo the server IP is $IP2
# to make a file folder to store the backp files
#if it was already exsit, just move original one to 99 folder
PLACE=/export/home
if [ -d $PLACE/$DATE ]
then
echo $PLACE/$DATE has already existed
DATETMP=`date +%Y-%m-%d-99`-tar
echo just waiting...
rm -fr $PLACE/$DATETMP
mv $PLACE/$DATE $PLACE/$DATETMP
echo and it has been move to $PLACE/$DATETMP
fi
mkdir $PLACE/$DATE
echo fresh folder $PLACE/$DATE has been created
echo and backup file will be backuped in $PLACE/$DATE/
# all backup files using tar file have been located in $PLACE/, and below command just sum the backup files and show them in the list.
NUM=`ls $PLACE | grep tar | wc -l`
echo now we have $NUM tar folder, they are all under $PLACE/
echo =================
ls -tr $PLACE | grep tar
echo =================
# to caculate the used disk space for $PLACE/
USEDISK=`df -k |grep $PLACE | awk '{print $5}' |sed 's/\%//'`
if [ $USEDISK -gt 90 ]
then
echo USEDISK is $USEDISK%
echo WARNNIG: have no enough space to backup!
exit;
fi
echo USEDISK is $USEDISK%
if [ $USEDISK -gt 65 ]
then
echo "***************************"
echo 'USEDISK is lager than 65!!!'
if [ $NUM -gt 2 ]
then
echo "-----------------------------------------------"
# if the used disk space is beyond 65% and the number of tar-format-backup-file-folder is bigger than 2, we would delete these folders to keep the number to be less or equate 2
DIFF=`expr $NUM - 2`
# the number of file folders that would be deleted is $DIFF, and they would be deleted in below circle.
for dir in `ls -tr $PLACE | grep tar | head -n $DIFF`
do
DISKSPACE=`df -k |grep $PLACE | awk '{print $5}' |sed 's/\%//'`
if [ $DISKSPACE -le 65 ]
then
echo "***************************"
echo there are $NUM tar folders:
ls -tr $PLACE | grep tar
echo --------------------------------
echo now USEDISK is $DISKSPACE%, and it is OK
break;
fi
echo "`ls -tr $PLACE | grep tar | head -n 1` will be deleted soon..."
echo deleting...
rm -fr $PLACE/$dir;
echo now USEDISK is $(df -k |grep $PLACE | awk '{print $5}' |sed 's/\%//')%
echo "$dir has been deleted successfully"
echo "-----------------------------------------------"
done
echo "********************************"
fi
fi
echo
echo "==========Begin to tar==============="
echo now below file folders would be tared:
# just show the list of file folder that would be backuped, and folder name has been add with "/" becuase this RHEL4.0 X86-64 does not show the root path.
echo ------------------
UPPLACE=`echo $PLACE | awk -F/ '{printf $2}'`
ls / | grep -v -e proc -e mnt -e media -e lost+found -e tmp -e $UPPLACE| sed 's/\([ ]\{1,\}\)/\1\//g;s/^./\/&/g'
echo ------------------
echo tar is working now...
echo please waiting.......
# prepare to show the file name, and the backup file name is $DATE/$RNAME-$IP3-$DAY.tar.gz locating at $PLACE/$DATE/
# to backup all root file folders, but donot include /proc, /mnt, /media/, and $PLACE/
# all the display infomation would not show by >& /dev/null
# all error information would not show by 2> /dev/null
IP3=`echo $IP2 | awk -F. '{printf $3"."$4}'`
DAY=`echo $DATE| awk -F- '{printf $2$3$4}'`
RNAME=`hostname | awk -F. '{printf $1}'`
tar -zcvPf $PLACE/$DATE/$RNAME-$IP3-$DAY.tar.gz 2> /dev/null $(ls / | grep -v -e proc -e mnt -e media -e lost+found -e tmp -e $UPPLACE) >& $PLACE/$DATE/$RNAME-$IP3-$DAY.backup.log
echo now backup is successful.
echo the file is $PLACE/$DATE/$RNAME-$IP3-$DAY.tar.gz
echo "==================================End to tar========================="
echo
# to list the backup folders
echo Summary of backup files $PLACE/:
echo -----------------------
echo the img backup:
ls -t $PLACE | grep img
echo ------------------
echo the tar backup:
ls -t $PLACE | grep tar
echo ------------------
echo other files:
ls -t $PLACE | grep -v tar |grep -v img
echo =================================
USEDISK1=`df -k |grep $PLACE | awk '{print $5}' |sed 's/\%//'`
echo before backup the USEDISK is $USEDISK%
echo at last the USEDISK is $USEDISK1%
echo =================================
echo the relevant log files are
echo tar-log: $PLACE/$DATE/$RNAME-$IP3-$DAY.backup.log
echo crontab-log: /script-location/backup.log
echo =================================
cp /usr/local/william.w/backup.log /usr/local/william.w/backup.`date +%Y-%m%d-%H%M`.log
[root@ad12 william.w]#
手动恢复脚本
[root@ad12 william.w]# cat restore-1.sh
#!/bin/bash
cd /
clear
echo
echo
echo
echo =====================================
echo this tool is used to restore
echo =====================================
echo
echo this restore command must be like "#/command-patch/restore.sh backup-file-folder"
echo -------------------------------------------------------------------------------
echo for example:
echo "#/command-patch/restore.sh 2008-04-01-00-tar"
echo
# to get the date
DATE=`date +%Y-%m-%d-%H`-tar
echo today is ${DATE%-*}
#to get the hostname
RNAME=`hostname | awk -F. '{printf $1}'`
echo the server name is $RNAME
# to get the IP
IP1=`ifconfig | grep "inet addr:" | grep -v "inet addr:127.0.0.1" | awk -F: '{printf $2}'`
IP2=${IP1%% *}
IP3=`echo $IP2 | awk -F. '{printf $3"."$4}'`
echo the server IP is $IP2
PLACE=/export/home
# to get the path of the backup file, and it is $PLACE/$1/
# to get the name of the backup file, and it is $RNAME-$IP3-$DAY.tar.gz
echo the restored folder and file are:
DAY=`echo $1| awk -F- '{printf $2$3$4}'`
echo $PLACE/$1/$RNAME-$IP3-$DAY.tar.gz
echo ---------------------------------------------------------
echo please wait......
# to restore the backup files under root path
cd /
tar -zxvPf $PLACE/$1/$RNAME-$IP3-*.tar.gz 2> /dev/null -C / >& $PLACE/$1/$RNAME-$IP3-$DAY.restore.log
echo backup is successful
echo --------------------
echo the relevant log files is
echo tar-log: $PLACE/$1/$RNAME-$IP3-$DAY.restore.log
echo crontab-log: /script-location/restore.log
echo =================================
[root@ad12 william.w]#
自动备份命令
[root@ad12 william.w]# crontab -l
SHELL=/bin/bash
30 18 * * * /usr/local/william.w/backup-1.sh >/usr/local/william.w/backup.log 2>&1
[root@ad12 william.w]#
[ 本帖最后由 wangjian_com 于 2008-4-5 15:51 编辑 ] |
-
5.jpg
(12.12 KB, 下载次数: 37)
|