LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 05-27-2007, 11:34 PM   #1
jamtech
LQ Newbie
 
Registered: May 2007
Distribution: Suse 10 and 11
Posts: 25

Rep: Reputation: 15
Need help with a bash script that does a backup


Hello all,

I have created a simple Bash script that backs up one of my directories Called Code.

The problem that i am having with this script is that it creates the backup directory and informs me that it is created. But after that it produces an error message see below (Script and error message).

The error message states that the files were backed up but the Tar command stated other wise.

Could you please take a look at the code and error message and tell me what I am missing.

################### Code ##################

#!/bin/bash
## This script uses the tar, mkdir and the cp commands
## to create an archive of files and place the files
## in an archive directory

# Scan System for an archive directory
# if it does not exist, then create the directory

# Bash variable
if [ -e $HOME/backups ]
then
echo "the Backup Directory exists."
else
echo "A Backup directory does not exist, one will be created."
mkdir $HOME/backups
echo "The backup directory has been created."
fi

# define variables and the location of the files to be archived
FILES=$HOME/Code

# Name of the compressed files
ARcHIVENAME=Code.tgz

#location of the archive directory
BACKUPDIR=/home/jamtech/backups

# Create compressed archive and copy the compressed
# files to the Archive directory
tar czf $ARCHIVENAME $FILES
cp -ap $ARCHIVENAME $BACKUPDIR

if [ -e $BACKUPDIR/$ARCHIVENAME ]
then
echo "The files were backedup!"
else
echo " the Files were not backup. DEBUG Backup.sh "
fi
################# End Code #########################

***********Error Message

jamtech@pandorra:~/Code$ bash backup.sh
the Backup Directory exists.
tar: Cowardly refusing to create an empty archive
Try `tar --help' for more information.
cp: missing destination file
Try `cp --help' for more information.
The files were backedup!
jamtech@pandorra:~/Code$
**********Error Message

Last edited by jamtech; 05-27-2007 at 11:36 PM.
 
Old 05-27-2007, 11:48 PM   #2
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
Code:
ARcHIVENAME=Code.tgz
Change the 'c' in to a capital 'C'. You might also want to tack on the date to the filename, maybe
something like
Code:
ARCHIVENAME=Code-`date +%F`.tgz
 
Old 05-28-2007, 12:58 AM   #3
jamtech
LQ Newbie
 
Registered: May 2007
Distribution: Suse 10 and 11
Posts: 25

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Berhanie
Code:
ARcHIVENAME=Code.tgz
Change the 'c' in to a capital 'C'. You might also want to tack on the date to the filename, maybe
something like
Code:
ARCHIVENAME=Code-`date +%F`.tgz
Berhanie

Thank you for you fast response as well as fixing my type O. Your suggestion for the code change worked as well.

My next step with this script is to:
1-cron it so that it will check the directory in question every 24hours for changes made to files with in the directory.

2-send an email with a list of the files that have been changed.

2-once the directory has been backup send an email out stating the job is complete and the next directory scan will start at this time
 
Old 05-28-2007, 01:51 AM   #4
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,438

Rep: Reputation: 52
Quote:
1-cron it so that it will check the directory in question every 24hours for changes made to files with in the directory.
Use the find command. Something like: find . -mtime +1. This will check the current directory for anything that has been modified over 1 day. You can read how cron works, it's very simple. Let us know if you need help with the syntax
Quote:
2-send an email with a list of the files that have been changed.
You can output the above command to a file and mail the contents of that file out.
Quote:
3-once the directory has been backup send an email out stating the job is complete and the next directory scan will start at this time
Read about the 'mail' command. Just add that to the end of your script.

-twantrd

Last edited by twantrd; 05-28-2007 at 01:52 AM.
 
Old 05-28-2007, 03:49 PM   #5
jamtech
LQ Newbie
 
Registered: May 2007
Distribution: Suse 10 and 11
Posts: 25

Original Poster
Rep: Reputation: 15
twantrd

Thank you
I will update my script with your key points on how to get the script to do what I need it to do. I will repost the script with the new changes.
 
Old 06-06-2007, 11:00 PM   #6
jamtech
LQ Newbie
 
Registered: May 2007
Distribution: Suse 10 and 11
Posts: 25

Original Poster
Rep: Reputation: 15
Hello all,

As stated in my last post I would update you on my current work and the additions to the back up script already in place.

I worked on a way to monitor the directory in question. I do not have it setup as a cron job yet.

I just need a bit of advice, Which would be more affective the script listed below or to use: (RED TEXT)

Use the find command. Something like: find . -mtime +1. This will check the current directory for anything that has been modified over 1 day. You can read how cron works, it's very simple. Let us know if you need help with the syntax

If I use the mtime command how do I incorporate it into a script that will email the results to me?

My plan for this script is to check the directory in question every 24hours send a report via email and if any changes were made to any files with in the directory in question the files within that directory should be backed up.

Am I going about this the wrong way?
Am I trying to reinvent the wheel?

I use Google and this Forum as my teaching guide with a copy of the advance bash shell scripting guide that I downloaded from the net

################### Code ######################

#!/bin/bash

# File Monitor
#
# 5/6/07 Initial Version


function list () {
ls -lsa > filescan.txt
sleep 20
}

while true; do
list
echo "Data of the last scan was sent to admin."
echo "Next file scan will start in 2 seconds!"
echo "Starting Scan!"
sleep 10
clear
cat filescan.txt
echo "directory Scan Report." | mail -s "File Monitor report" me@mail.com < filescan.txt
done

################### End Code ######################
 
Old 06-07-2007, 02:31 AM   #7
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,438

Rep: Reputation: 52
Since your just checking if files have been modified in 24 hours, the find command does it for you in one single command.

Quote:
If I use the mtime command how do I incorporate it into a script that will email the results to me?
A couple ways. For instance, the same way that you have outputted information to your text file. I would honestly just pipe the mail command to 'find' to make it a one-liner.

-twantrd

Last edited by twantrd; 06-07-2007 at 02:34 AM.
 
Old 06-08-2007, 12:34 AM   #8
jamtech
LQ Newbie
 
Registered: May 2007
Distribution: Suse 10 and 11
Posts: 25

Original Poster
Rep: Reputation: 15
I created the script below please review I am getting an error when it is ran see error below

################### Code ######################
#!/bin/bash
# File Monitor
#
# 5/6/07 Initial Version
#
# Scans the directory in question every 24 hours for any changes
find . -mtime +1 >filescan.txt
clear
cat filescan.txt
echo "directory Scan Report." | mail -s "File Monitor report" jamtech@pandorra.jamtech.com < filescan.txt
################### End Code ######################

When I run the above script the system locks up and my screen will only display:

################### Error Code ######################
jamtech@pandorra:~/Code$
New mail for jamtech@pandorra has arrived:
----
From: jamtech@pandorra.jamtech.com
Subject: File Monitoer report
----

jamtech@pandorra:~/Code$
 
Old 06-08-2007, 12:36 AM   #9
jamtech
LQ Newbie
 
Registered: May 2007
Distribution: Suse 10 and 11
Posts: 25

Original Poster
Rep: Reputation: 15
never mind I see the type-o

echo "directory Scan Report." | mail -s "File Monitor report" jamtech@pandorra.jamtech.com < filescan.txt

it should be
echo "directory Scan Report." | mail -s "File Monitor report" jamtech@pandorra.jamtech.com > filescan.txt
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Need help with my backup script written in bash! guy12345 Programming 14 05-25-2007 06:09 AM
Backup script in bash gauge73 Programming 13 10-17-2005 07:25 AM
Bash backup script failing using tar and --newer option saldkj Programming 3 03-12-2005 01:03 PM
Bash backup script - If multiple files starting with a exist problem demoncheese Programming 2 07-29-2004 11:47 PM
bash shell backup script turnip Programming 0 04-03-2003 07:00 PM


All times are GMT -5. The time now is 10:55 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration