LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   backup script (https://www.linuxquestions.org/questions/linux-general-1/backup-script-485610/)

tommytomato 09-21-2006 07:55 AM

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

kstan 09-21-2006 08:35 AM

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

timmeke 09-21-2006 10:25 AM

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.

trickykid 09-21-2006 10:27 AM

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?

trickykid 09-21-2006 10:31 AM

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.. ;)

tommytomato 09-21-2006 06:45 PM

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

timmeke 09-22-2006 02:41 AM

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?


All times are GMT -5. The time now is 06:28 AM.