免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6350 | 回复: 7
打印 上一主题 下一主题

[脚本共享] tar 自动备份和手动恢复脚本 (RHEL4)(有详细注释) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-04-01 11:56 |只看该作者 |倒序浏览
个人稍加修改,就成自己最顺手的脚本了。
注意配合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)

5.jpg

论坛徽章:
0
2 [报告]
发表于 2008-04-03 17:54 |只看该作者

回复 #1 wangjian_com 的帖子

经过几天的在线运行测试,都工作正常。
请放心使用。

nemopang 该用户已被删除
3 [报告]
发表于 2008-04-03 22:46 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
4 [报告]
发表于 2008-04-04 14:31 |只看该作者
顶一下................

论坛徽章:
0
5 [报告]
发表于 2008-04-14 09:23 |只看该作者

回复 #1 wangjian_com 的帖子

经过一段时间运行,算是正常:

[root@ad12 ~]#
[root@ad12 ~]# ls -alt /export/home/
total 32
drwxr-xr-x  6 root root 4096 Apr 13 18:30 .
drwxr-xr-x  2 root root 4096 Apr 13 18:30 2008-04-13-18-tar
drwxr-xr-x  2 root root 4096 Apr 12 18:30 2008-04-12-18-tar
drwxr-xr-x  2 root root 4096 Apr 11 18:30 2008-04-11-18-tar
-rw-r--r--  1 root root  198 Apr  9 16:55 ad12-239.233-.restore.log
drwxr-xr-x  3 root root 4096 Apr  4 12:25 ..
-rw-r--r--  1 root root    0 Mar 31 16:47 test.test
drwxr-x---  2 root root 4096 Mar 29 06:43 2008-03-26-17-img
[root@ad12 ~]#
[root@ad12 ~]# df -k
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda2            113852040  13954896  94113748  13% /
/dev/sda1               790588     24680    725748   4% /boot
none                   1028008        20   1027988   1% /dev/shm
/dev/sda5             19362784  14097104   4282104  77% /export/home
[root@ad12 ~]#

论坛徽章:
0
6 [报告]
发表于 2008-04-14 15:18 |只看该作者
饿的神啊 跟天书一样

论坛徽章:
0
7 [报告]
发表于 2008-05-12 22:13 |只看该作者
powerful.............

论坛徽章:
0
8 [报告]
发表于 2011-10-31 13:59 |只看该作者
这个脚本复制能用么
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP