文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>小脚本搞定文件备份问题

小脚本搞定文件备份问题

时间:2007-10-18  来源:zyangxue

前段时间写的程序在一不小心的时候,全都掉了,由于那时的cvs还没有用起来,真是心痛死了。

因此为了保险,而且是双保险,就采用了下面两个方法:
    1、每天写的东西,含系统/etc文件下的修改等,确定OK之后,即上传到cvs中保存一份
    2、写一个脚本,放到cron工作中,定时全备份主目录一次。
       (1). cron每周定时备份
       (2). 备份文档按日期方式命名. username-yyyy-mm-dd.tar.gz
       (3). 只保留5份,之前的备份文档自动清理      

#!/bin/sh
#
# Copyright (C) 2007 Edwin Y.X. Zeng <[email protected]>
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA.
#
# See http://www.gnu.org/licenses/gpl.html for the complete text of license.
#

# $Id: backhome.sh,v 1.2 2007-10-17 16:43:20 zyangxue Exp $
# FileName: backhome.sh
# Version: $Revision: 1.2 $
# Discription:
# 1. Auto backup the home directory by tar
# 2. Backup files named username-YYYY-MM-DD.tar.gz
# 3. delete the oldest backup files.
#

# The user's name
user_name=edwin
user_home=$(finger ${user_name} | awk '/^Directory/{print $2}')

# Generate the tar.gz file name for this times
# curdate=$(date | awk '{print $6"-"$2"-"$3}')
curdate=$(date +%F)
curfl=${user_name}"-"${curdate}

# Where is the tar.gz stored into
bkdir=/media/sda5/cvs

# How many tar.gz reserved
bknum=5

cd ${user_home}
rm -rf .macromedia .viminfo .bash_history .dmrc .mplayer .gqview \
       .thumbnails .xsession-errors .xscreansaver .hxplayerrc \
       .sudo_as_admin_successful .maple .lesshst .recently-use* \
       Signature/enig*.txt

cd ${bkdir}
if [ ! -e ${curfl}.tar.gz ]; then
    echo "Press CTRL-C to interrupt......"
    trap 'rm -f ${curfl}.tar.gz' INT
    echo "Backing files......"
    tar zcvf ${curfl}.tar.gz ${user_home}/.[a-zA-z0-9]* ${user_home}/*
else
    echo "Because of conflict with exist files, skipping ...."
    exit 0
fi

gznum=$(ls -lt ${user_name}-20[0-2][0-9]-[01][0-9]-[0-3][0-9].tar.gz | wc -l)
if [ ${gznum} -gt ${bknum} ]; then
    bkarches=$(ls -lt ${user_name}-20[0-2][0-9]-[01][0-9]-[0-3][0-9].tar.gz | \
                awk -v mrnum=${bknum} '{if (NR > mrnum) {print $8} }')
    for todel in ${bkarches}; do
        echo deleting ${todel}...
        rm -f ${todel}
    done
fi


意外:
    1、bash 命令行的正则表达式与grep与vim等的还是有重大区别,关键的时候还得注意一下。如
       在grep中表示一个字符重复几次的表达方式[0-9]\{4\} 是错误的,不被识别, [0-9]{4}也不行。以前还没有注意到此细节
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载