LinuxQuestions.org
Visit Jeremy's Blog.
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, 07:55 AM   #1
tommytomato
Member
 
Registered: Nov 2003
Location: Narrogin Western Australia
Distribution: GUI Ubuntu 14.0.4 - Server Ubuntu 14.04.5 LTS
Posts: 963

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 07:59 AM.
 
Old 09-21-2006, 08: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 08:37 AM.
 
Old 09-21-2006, 10: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, 10:27 AM   #4
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
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 10:29 AM.
 
Old 09-21-2006, 10:31 AM   #5
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
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, 06:45 PM   #6
tommytomato
Member
 
Registered: Nov 2003
Location: Narrogin Western Australia
Distribution: GUI Ubuntu 14.0.4 - Server Ubuntu 14.04.5 LTS
Posts: 963

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, 02: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 05:12 PM
Need a backup script enygma Linux - General 5 11-04-2004 03:49 PM
Backup Script imsajjadali Linux - General 7 01-28-2004 03:30 PM
backup script lhoff Programming 2 05-28-2003 11:37 PM
backup script ixion Linux - Software 2 01-09-2003 06:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01: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
Open Source Consulting | Domain Registration