LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 09-21-2006, 08:55 AM   #1
tommytomato
Member
 
Registered: Nov 2003
Location: Narrogin Western Australia
Distribution: GUI Ubuntu 12.04 - Server 12.04.4 LTS
Posts: 935

Rep: Reputation: 32
backup script


Hi all

I'm trying to create a back up script to back up my httpd_conf file.

how ever when the cron runs it works ok but the file ends up been 350 meg in size.

can any one tell me where I went wrong please

My bash script

Code:
#!/bin/bash

# this file is an automated backup script, httpd_backup.sh.
# this backs up my httpd config file.
# cron is ( 45 * * * * /Downloads/scripts/httpd_backup.sh )

# what directory or file to back up
cd /etc/httpd/conf/httpd.conf
# path to back up folder
tar -zcf /Downloads/httpd_backup.sh.tar.gz .
And my Mutt mail reads

/Downloads/scripts/httpd_backup.sh: line 9: cd: /etc/httpd/conf/httpd_config: No such file or directory

TT

Last edited by tommytomato; 09-21-2006 at 08:59 AM.
 
Old 09-21-2006, 09:35 AM   #2
kstan
Member
 
Registered: Sep 2004
Location: Malaysia, Johor
Distribution: Dual boot MacOS X/Ubuntu 9.10
Posts: 851

Rep: Reputation: 31
cd /etc/httpd/conf/httpd.conf(it is a file, not folder)
you can't 'cd' to here, so you will remain at current directory
tar -zcf /Downloads/httpd_backup.sh.tar.gz .
since you not inside the /etc/httpd/conf folder but remain at home directory, it won't backup the file for you. What it do is backup the entire home directory for you(maybe root or your home directory)

so, you can change to:

#!/bin/bash

# this file is an automated backup script, httpd_backup.sh.
# this backs up my httpd config file.
# cron is ( 45 * * * * /Downloads/scripts/httpd_backup.sh )

# what directory or file to back up
cd /etc/httpd/conf
# path to back up folder
tar -zcf /Downloads/httpd_backup.sh.tar.gz .


Ks

Last edited by kstan; 09-21-2006 at 09:37 AM.
 
Old 09-21-2006, 11:25 AM   #3
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Actually, it's a better practice to check if the "cd" works.
So, do something like:
Code:
#!/bin/bash

# this file is an automated backup script, httpd_backup.sh.
# this backs up my httpd config file.
# cron is ( 45 * * * * /Downloads/scripts/httpd_backup.sh )

dir="/etc/httpd/conf";

# what directory or file to back up
cd ${dir}

if [[ $? != 0 ]]; then
   #cd failed, we'll exit with an error message
   echo "Error: could not cd to ${dir}. No backup was created."
   exit 1;
fi;

# path to back up folder
tar -zcf /Downloads/httpd_backup.sh.tar.gz .
In this case, the tar won't get executed if the "cd" fails.

Alternatively, you could just add the file(s) to be tar'ed at the end of the tar command. This means you don't have to do a "cd" at all.

Tar also has an option to specify a file that contains a list of files to be tar'red. Maybe useful for you too.
 
Old 09-21-2006, 11:27 AM   #4
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,133

Rep: Reputation: 199Reputation: 199
Change your script to something like this, which will timestamp the backup:

Code:
#!/bin/bash
#

# Place a date in the name of the backed up file
date=$(date +%F)
# Filename format + date of backup
filename=httpd_backup.$date.tar
# File to backup
file=/etc/httpd/conf/httpd.conf
# Backup directory to place file in
bakdir=/Downloads

cd $bakdir
tar zcf $filename $file

exit 0
My only question would be, does your httpd.conf file change daily? If it doesn't, why would you need to back it up daily?

Last edited by trickykid; 09-21-2006 at 11:29 AM.
 
Old 09-21-2006, 11:31 AM   #5
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,133

Rep: Reputation: 199Reputation: 199
Quote:
Originally Posted by timmeke
Actually, it's a better practice to check if the "cd" works.
So, do something like:
Code:
#!/bin/bash

# this file is an automated backup script, httpd_backup.sh.
# this backs up my httpd config file.
# cron is ( 45 * * * * /Downloads/scripts/httpd_backup.sh )

dir="/etc/httpd/conf";

# what directory or file to back up
cd ${dir}

if [[ $? != 0 ]]; then
   #cd failed, we'll exit with an error message
   echo "Error: could not cd to ${dir}. No backup was created."
   exit 1;
fi;

# path to back up folder
tar -zcf /Downloads/httpd_backup.sh.tar.gz .
In this case, the tar won't get executed if the "cd" fails.

Alternatively, you could just add the file(s) to be tar'ed at the end of the tar command. This means you don't have to do a "cd" at all.

Tar also has an option to specify a file that contains a list of files to be tar'red. Maybe useful for you too.
Actually, if your going to go as far as directory checks, might as well just check to make sure the file is there, since he only wants to backup the one file..
 
Old 09-21-2006, 07:45 PM   #6
tommytomato
Member
 
Registered: Nov 2003
Location: Narrogin Western Australia
Distribution: GUI Ubuntu 12.04 - Server 12.04.4 LTS
Posts: 935

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by trickykid
Change your script to something like this, which will timestamp the backup:

Code:
#!/bin/bash
#

# Place a date in the name of the backed up file
date=$(date +%F)
# Filename format + date of backup
filename=httpd_backup.$date.tar
# File to backup
file=/etc/httpd/conf/httpd.conf
# Backup directory to place file in
bakdir=/Downloads

cd $bakdir
tar zcf $filename $file

exit 0
My only question would be, does your httpd.conf file change daily? If it doesn't, why would you need to back it up daily?
cheers guys

so what your saying is there's no point in backing up one file

TT
 
Old 09-22-2006, 03:41 AM   #7
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
There is a point in backing up one file, if it's an important one, but only if that file changes regularly (ie daily, weekly,...).
If it doesn't, you can create just one backup copy (for recovery later on), and install some file integrity tools like samhain, aide or tripwire. They are designed to detect (and prevent) illegal changes to critical system files.
If it does change regularly, you should adapt your backup frequency to match the change frequency. There's no point in taking daily backups if the file only changes once a month, right?
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
backup script wiltzius Linux - Software 8 03-23-2005 06:12 PM
Need a backup script enygma Linux - General 5 11-04-2004 04:49 PM
Backup Script imsajjadali Linux - General 7 01-28-2004 04:30 PM
backup script lhoff Programming 2 05-29-2003 12:37 AM
backup script ixion Linux - Software 2 01-09-2003 07:39 AM


All times are GMT -5. The time now is 11:12 AM.

Main Menu
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