Register
It is currently Wed Apr 16, 2014 7:29 am

backup with tar, keeping 7 backups deleting the last


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Tue May 31, 2011 8:58 pm   

Joined: Mon May 30, 2011 11:31 pm
Posts: 4
Hello all! What I would like this script to do:

    1) check how many backups have been made
    2) delete the oldest to make room for the latest

I'm somewhat new to scripting and have some of the script pieced together via snippets of other scripts and addins written by myself. (lots of trial and error!)

This is what I have so far.

SCRIPT

Code:
#!/bin/sh

# ----------------------------------------------------------- #
# 1 - START LOG FILE                #
# 2 - Create list of installed software          #
# 3 - Sources                      #
# 4 - Destination                      #
# 5 - Archive naming                   #
# 6 - Create actual backup                #
# 7 - Copy backup to NAS                #
# 8 - Delete local backup                #
# 9 - END LOG FILE                   #
# ------------------------------------------------------------ #

# 1
echo "-------------------------------------------------------------" >> /media/nas/backups/supertux/incremental/backup_LOG.txt
echo "START  - `date`" >> /media/nas/backups/supertux/incremental/backup_LOG.txt
# 2
dpkg --get-selections > /home/supertux/Documents/scripts/sync/installed-software.txt
# 3
backup_files="/bin/backup.sh /etc/fstab /home/supertux/Documents /home/supertux/Pictures"
# 4
dest="/"
# 5
day=$(date +%Y%m%d)
archive_file="bu_$day.tar.bz2"
# 6
tar cjf $dest/$archive_file $backup_files
# 7
cp /$archive_file /media/nas/backups/supertux/incremental
# 8
rm /$archive_file
# 9
echo "END    - `date`" >> /media/nas/backups/supertux/incremental/backup_LOG.txt
echo "-------------------------------------------------------------" >> /media/nas/backups/supertux/incremental/backup_LOG.txt

# NOTES
# Under line 5 paste  *hostname=$(hostname -s)* to prepend current hostname to filename (without * )
#
#
#
#
#
#
# TODO
# Have script check how many backups are there. Keep only 5..maybe 6 or 7 then
# remove the oldest to make room for newest.
# sudo make breakfast
#
#


LOG (which will be fancier as time goes on and I learn more)

Code:
-------------------------------------------------------------
START  - Fri May 27 22:23:47 EDT 2011
END    - Fri May 27 22:23:48 EDT 2011
-------------------------------------------------------------
-------------------------------------------------------------
START  - Sat May 28 00:00:01 EDT 2011
END    - Sat May 28 00:00:14 EDT 2011
-------------------------------------------------------------
-------------------------------------------------------------
START  - Sun May 29 00:00:01 EDT 2011
END    - Sun May 29 00:00:14 EDT 2011
-------------------------------------------------------------
-------------------------------------------------------------
START  - Mon May 30 00:00:01 EDT 2011
END    - Mon May 30 00:00:13 EDT 2011
-------------------------------------------------------------
-------------------------------------------------------------
START  - Tue May 31 00:00:01 EDT 2011
END    - Tue May 31 00:00:14 EDT 2011
-------------------------------------------------------------


I'm trying my best to be as neat as possible with my code writing. I've seen some of the codes we use at work and......well I just don't see how anyone else can
interpret them aside from the author. Which was probably purposely done. Any help is greatly appreciated so thanks in advance and hope everyone had a great weekend!!

-Snickasaurus


Top
 Profile  
 PostPosted: Mon Jun 06, 2011 6:38 pm   

Joined: Mon May 30, 2011 11:31 pm
Posts: 4
bump


Top
 Profile  
 PostPosted: Tue Jun 07, 2011 3:58 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 223
Location: London - UK
let's suppose you want to remove the one from 7 days ago...
Code:
lastweek=$(date -d '7 days ago' +%Y%m%d)
rm /media/nas/backups/supertux/incremental/bu_${lastweek}.tar.bz2

Hope that helps


Top
 Profile  
 PostPosted: Tue Jun 07, 2011 5:51 pm   

Joined: Mon May 30, 2011 11:31 pm
Posts: 4
Going to give that a try when I get home DarthWavy. Thanks for the reply, will let you know how it goes.


Edit - Didn't seem to work. Is the "7 days ago" just a comment of some sort? I'm puzzled as to how it would compare the dates of those files. Thanks again for the help.


Top
 Profile  
 PostPosted: Wed Jun 08, 2011 4:20 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 223
Location: London - UK
"Didn't seem to work" sadly tells me nothing

This is a section from the date manpage. Almost all commands have a man page to give you the details of how to use them and can be accessed by typing "man command", eg "man date";
Code:
       -d, --date=STRING
              display time described by STRING, not `now'

and further down;
Code:
DATE STRING
       The --date=STRING is a mostly free format human readable date  string
       such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or
       even "next Thursday".  A date string  may  contain  items  indicating
       calendar  date,  time  of day, time zone, day of week, relative time,
       relative date, and numbers.

To actually help with your problem more help on your part is needed other than "didn't seem to work", I don't have time to guess <):)


Top
 Profile  
 PostPosted: Wed Jun 08, 2011 5:03 pm   

Joined: Mon May 30, 2011 11:31 pm
Posts: 4
My apologies. I manually removed the last file created, which was that current days backup, and that left 10 days worth of backups. I opened Scite with sudo to edit the file. Once I pasted in your code I then saved it and ran it from the terminal ( $sudo sh /bin/backup.sh ) and the backup was created successfully but the old files were not removed. There was no error message so I started to read up on that command set to see what I could possibly tweak to get it to work. After trying to hit it with a one two combo based off of the same stuff you quoted ( --date=string ) at the end of your reply I gave up.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 9 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