Register
It is currently Sat Oct 25, 2014 1:00 pm

Backup directory and gzip and rotate backups to keep 7 days


All times are UTC - 6 hours


Post new topic Reply to topic  [ 1 post ] 
Author Message
 PostPosted: Tue Aug 29, 2006 2:23 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
Wrote this today because i was bored and someone asked for something similar on opensuse.us

Code:
#!/bin/bash
# Written by Crouse 08-29-2006
# Backup directory and gzip and rotate backups to keep 7 days worth.
# This script keeps 7 days worth of backups of whatever directory you wish.
# Notes: To UNZIP the backups use  tar -zxvf filname.tgz

# The next two lines can be modified to fit your backup needs.
DIR1="/home/$USER/Documents/HayfieldOrders/"; #This is the directory to backup
BAKUPS="/home/$USER/BACKUPS"; #This is the directory to backup too.

# These lines set date and create the backup dir if not there already.
CURRENT_DATE="$(date +%m.%d.%Y)";
mkdir -p $BAKUPS  #make dir if not already there for backup storage
tar -c $DIR1 | gzip > $CURRENT_DATE.tgz  # Tars and Gzips the directory
mv $CURRENT_DATE.tgz $BAKUPS/  # moves the backups and removes the temp dir and files.

# The following REMOVES all but the last 6 backups
cd $BAKUPS
for i in `ls -t * | tail +2`; do
ls -t * | tail +8 | xargs rm -f
done

exit 0


You can change the tail +8 to something else if you want to keep more than 7 days worth of backups. This assumes you are running this script daily as a cron job.


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP