LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-08-2004, 07:42 PM   #1
TPupAZ
Member
 
Registered: Sep 2003
Posts: 36

Rep: Reputation: 15
Cron Backup


Ok so I'm trying to automate my backup procedures. Very basic, not a lot of fuss to it. But I'm trying to deliberate on a few ideas, I dont mind researching them myself, but wanted to ask if it is possible or have seen something similar to this before.

So I've got my crontab set up like this...


30 20 * * 5 root backup

Meaning that every Friday , @ 8:30 , the backup script will execute.

The backup script is simply

cd /home/directoryname
rm backup.tar.gz
tar cvf backup.tar.gz /home/backuptargetdirectory

So it goes to the home directory, removes the old backup and then creates a new one in its place.

My question is, is there a script I can write or would it be possible to have the script name the file by date..

e.g. -- July092004_backup.tar.gz

Then at the start having it delete the one from 2 weeks ago...so if i were to ls the /backup directory and it said

July092004_backup.tar.gz
July162004_backup.tar.gz
July232003_backup.tar.gz

Now on July 30th, it will go in and delete July092004_backup.tar.gz
leaving me with 3 total backups of the past 3 weeks.

I read an article using rdiff that does something similar to this but not sure if I can incorporate it into what I want to do

Thanks in advance for any advice, questions, or suggestions.

TPupAZ
 
Old 07-08-2004, 09:35 PM   #2
zakaluka
Member
 
Registered: Oct 2003
Posts: 58

Rep: Reputation: 15
rdiff is a little different than what you are saying. For backups, you would probably use rdiff-backup (http://rdiff-backup.stanford.edu/).

Basically, it creates an identical backup of your source directory the first time. Every time after that, it simply backs up the changes made to the source directory since the last backup. It also contains the ability to roll-back changes, which is what the backup is for, anyway. In this case, you would NOT delete any previous backups, but simply let them build upon each other. There are options you can use so that it will remove backups that are older than a certain date, so that you are not wasting too much space.

This method would be more space efficient than what you are currently doing (3 full backups of the directory).

Regards,

zakaluka.
 
Old 07-08-2004, 09:56 PM   #3
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Re: Cron Backup

Quote:
Originally posted by TPupAZ

My question is, is there a script I can write or would it be possible to have the script name the file by date..

e.g. -- July092004_backup.tar.gz

This part is simple:

tar cvzf `date +%B``date +%d``date +%Y`_backup.tgz ~/

make sure you use back ticks instead of single quotes.

check out the man pages for "date" for more extra stuff on how date is used.
 
Old 07-08-2004, 10:10 PM   #4
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
for the delete part, this is a bit klugey, but it works (I think):

(using tcsh):
Code:
set lsArray=( `ls -t1 | grep backup.tgz` )

if ($#lsArray > 3)
  rm -f $lsArray[$#lsArray]
of corse, the uber dangerous part of this method is that if you have anything else in your dir called *backup.tgz, it may get deleted. You can avoid that by putting your backups all in the same dir

what this does is create an array made up of the contents of your directory that contain the string "backup.tgz" sorted by date (oldest last)
if then checks to see if that array is longer than 3. if it is, it deletes the last one.

I'm sure there's a better way, but this way should at least do what you want. be careful with it though.

The above code will not work in bash, but I'm sure it's pretty close.
 
Old 07-12-2004, 05:32 PM   #5
zakaluka
Member
 
Registered: Oct 2003
Posts: 58

Rep: Reputation: 15
The bash version of the deletion script is:

lsArray=`ls -t1 | grep backup.tar.gz`
length=`echo $lsArray | wc -w`
if (( $length > 3 )); then
rm `echo $lsArray | cut -d' ' -f $length`
fi

This only removes the oldest file from the list. If you want to keep only the 3 newest files, do this:

while (( 1 > 0 )); do
lsArray=`ls -t1 | grep backup.tar.gz`
length=`echo $lsArray | wc -w`
if (( $length > 3 )); then
rm `echo $lsArray | cut -d' ' -f $length`
else
break
fi
done

NOTE: neither of these versions allow for spaces within the filename.

Regards,

zakaluka.
 
Old 02-10-2005, 08:58 AM   #6
řivind
LQ Newbie
 
Registered: Feb 2005
Posts: 2

Rep: Reputation: 0
ok, i've tried these scripts, but...

I am trying to make the same kind of script and backup operation as TPupAZ but there are some things i don't fully understand with the scripting and it would be great if someone could give me a little help here.

I have a folder in the /home/ directory named /important/ which contains files i want to make backup of in tar.gz format and i want the archive to end up in /home/backupdir/ i have put together a script by using the tips in this thread but it's not fully operational yet

this is how my (so called ) script looks like for the moment :

cd /
cd /home/important/
tar cvzf `date +%B``date +%d``date +%Y_backup.tgz /home/important/
cd /
cd /home/backupdir/
while (( 1 > 0 )); do
lsArray=`ls -t1 | grep backup.tar.gz`
length=`echo $lsArray | wc -w`
if (( $length > 3)); then
rm `echo $lsArray | cut -d` ` -f $length`
else
break
fi
done


Now this script sort of works 'cause it DOES put all the files in the /important/ dir into a dated .tgz archive but the archive is placed in the same directory as the files in it (/important) and I want it to end up in the /backupdir/

I probably just have made a simple mistake somewhere along the way in the script but I am just a beginner at Linux so I don't have any experience with this kind of scripting and therefore just don't know what to do
 
Old 02-10-2005, 09:50 AM   #7
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
řivind,

Not to bash previous methods but this is how I do it....
Code:
#!/bin/bash

filename=`date '+%m%d%y'`

cd /home/important

/bin/tar -cvzf  /home/backupdir/${filename}.tar.gz . \
>  /home/backupdir/${filename}.log \
2>  /home/backupdir/${filename}err.log

#Delete old files with the following command
find  /home/backupdir -type f -name '*.gz' , -name '*.log' , \
-mtime +3 -exec rm {} \;
 
Old 02-11-2005, 07:49 AM   #8
řivind
LQ Newbie
 
Registered: Feb 2005
Posts: 2

Rep: Reputation: 0
sweeet

thanks man, that one worked without the slightest problem
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a backup be done with cron? sadly so Solaris / OpenSolaris 3 02-03-2005 01:28 AM
Cron Backup question lil_drummaboy Linux - General 2 12-07-2004 12:39 AM
Backup Question / Cron TPupAZ Linux - Software 1 06-11-2004 01:17 AM
Backup MySQL w/ CRON tracer Linux - General 0 03-28-2003 11:40 AM
cron job - backup medamnit Linux - Newbie 4 05-24-2002 03:37 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration