#!/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
|