You are not logged in.

#1 2004-06-17 22:21:43

iotc247
Member
From: Florida
Registered: 2004-05-31
Posts: 177
Website

Help with script..

Ok I have a backup script made but i need to have a small part that checks whether there is a /var/backups and /var/backups/work and if there isnt create them.. Also i need to have it where the script creates a file filename.tar.bz2 and move its to /var/backups/packagename-Date.tar.bz2
but i need to know how to make it generate the date based on the os somehow like the clock if it has a date im not sure... But you should understand what i mean... The script is in bash well i think its bash if it has #! /bin/bash at the top so plz help..

Offline

#2 2004-06-17 22:24:58

iotc247
Member
From: Florida
Registered: 2004-05-31
Posts: 177
Website

Re: Help with script..

O i would also like to make this a package so if anyone knows how to make /sbin/files that would be great so that way it can execute that script which will reside in a directory that maybe you can tell me where it should be placed... If you understand what i mean.. If this isnt how it works then could please someone just help me..

Offline

#3 2004-06-17 22:30:19

iotc247
Member
From: Florida
Registered: 2004-05-31
Posts: 177
Website

Re: Help with script..

Any suggestions with this following script would be nice

New version below..

Offline

#4 2004-06-17 23:15:40

iotc247
Member
From: Florida
Registered: 2004-05-31
Posts: 177
Website

Re: Help with script..

anyone?

Offline

#5 2004-06-17 23:15:42

i3839
Member
Registered: 2004-02-04
Posts: 1,185

Re: Help with script..

Use backticks, e.g.

# touch `date +arc-section-%F.tar.bz2`

To make your script more portable you should use #!/bin/sh instead of /bin/bash.

For a good bash scripting guide see http://www.tldp.org/LDP/abs/html
You''ll find info about tests and much more there.

I'm not sure what you mean with installing the script, but simply moving it to /usr/sbin and making it executable should be enough.

(And don't be so impatient...)

Offline

#6 2004-06-17 23:17:32

iotc247
Member
From: Florida
Registered: 2004-05-31
Posts: 177
Website

Re: Help with script..

O ok that will allow me to just run arch-hd-backup right? thanks..

Offline

#7 2004-06-17 23:20:19

iotc247
Member
From: Florida
Registered: 2004-05-31
Posts: 177
Website

Re: Help with script..

where would i do the touch part how would i do that..

Offline

#8 2004-06-18 00:22:41

iotc247
Member
From: Florida
Registered: 2004-05-31
Posts: 177
Website

Re: Help with script..

Can you possibly tell me how to have it ask the differnt ways to tar it lets say it asks this

Do you want sepeate files? [y/n] y
Do you want /home [y/n] y Then that makes the script tar /home seperatly
Do you want /boot [y/n] n That makes this question be asked
Should it be included with / ? [y/n] y

Can you explain that... If no i guess ill just read that whole thing.. Its just i need this for my script.. It will probably be a big hit then because it will back ur entire hd up... IThen later ill have it where its like filename -R which would make it ask some stuff about restoring it to a specified partition and mount stuff like that.... Is that possible with shell scripting or is that c or something?

Offline

#9 2004-06-18 00:47:20

iotc247
Member
From: Florida
Registered: 2004-05-31
Posts: 177
Website

Re: Help with script..

New version below

Offline

#10 2004-06-18 10:53:17

i3839
Member
Registered: 2004-02-04
Posts: 1,185

Re: Help with script..

Offline

#11 2004-06-18 12:38:05

iotc247
Member
From: Florida
Registered: 2004-05-31
Posts: 177
Website

Re: Help with script..

Anyone willing to try it its in its last beta stage.. Just needs to be tested.. So anyone that would host it for people to download that would be nice also.. Its very small 3kb... But anyways thx in advance..

Offline

#12 2004-06-18 19:19:49

iotc247
Member
From: Florida
Registered: 2004-05-31
Posts: 177
Website

Re: Help with script..

New version available..

Offline

#13 2004-06-19 00:00:48

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,615
Website

Re: Help with script..

probably the syntactical error on that line..
elif [ "$action" ...
should probably be
if [ "$action"

I use bash for my scripting, so I dont offhand recall what sh uses for if statements..


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#14 2004-06-19 00:03:30

iotc247
Member
From: Florida
Registered: 2004-05-31
Posts: 177
Website

Re: Help with script..

well sh and bash is almost the same isnt? I was told to do sh instead of bash.. But ah it is correct i had another error i fixed it now and now im changing the script alot.. Less questions... More arguments (ie hdbackup -verbose)

Offline

#15 2004-06-19 00:49:33

olly-bh
Member
Registered: 2004-04-22
Posts: 14

Re: Help with script..

iotc247 wrote:

i need to have a small part that checks whether there is a /var/backups and /var/backups/work and if there isnt create them

mkdir -p /var/backups/work will do this - it will create the directory (and its parents if required), and not give an error if it already exists.

BTW, you should consider splitting your script up into functions.  It'd make it a lot easier to read and understand.

- olly

Offline

#16 2004-06-19 00:54:54

iotc247
Member
From: Florida
Registered: 2004-05-31
Posts: 177
Website

Re: Help with script..

Rewritten

Offline

#17 2004-06-19 00:58:41

iotc247
Member
From: Florida
Registered: 2004-05-31
Posts: 177
Website

Re: Help with script..

And i get alot of these

/usr/bin/hdbackup: line 83: [: =: unary operator expected
/usr/bin/hdbackup: line 111: [: =: unary operator expected

How to fix that?

Offline

#18 2004-06-19 01:09:32

olly-bh
Member
Registered: 2004-04-22
Posts: 14

Re: Help with script..

iotc247 wrote:

/usr/bin/hdbackup: line 83: [: =: unary operator expected
/usr/bin/hdbackup: line 111: [: =: unary operator expected

These are from lines like

if [ $1 = -vs ]; then

Think what happens if $1 is undefined - there's nothing on the left-hand of the test, which is giving the error.

If you testing args it's more common to use case ... esac.

Your script would be a lot more readable if you got rid of all the tests to detect files created and just tested $? instead.  And dump the verbose error message if it fails - you'll have got an error message from the command that went wrong.

- olly

Offline

#19 2004-06-19 01:32:52

iotc247
Member
From: Florida
Registered: 2004-05-31
Posts: 177
Website

Re: Help with script..

The error messages are so that you know it failed and it doesnt keep going... Im going to make it where you input the directories you want and all later if i can get some help on how to do it.. Really i need help on rewriting the script to make it cleaner... Then again i just started last night (24 hours ago)

Offline

#20 2004-06-19 01:46:42

olly-bh
Member
Registered: 2004-04-22
Posts: 14

Re: Help with script..

iotc247 wrote:

The error messages are so that you know it failed and it doesnt keep going

I'm not suggesting you get rid of the tests.  Just do them a bit less verbosely.

Instead of...

$HTARCN
if [ -x "$DIR2/$HNM" ]; then #Checks to see if the file was created to make sure its there.
   $E "$HDIR" backup completed.
   $HMV
else
   $E "$HDIR" backup failed. Exiting and please retry.; exit 1
fi

...you can just do...

$HTARCN || exit 1
$HMV

- olly

Offline

#21 2004-06-19 14:00:17

iotc247
Member
From: Florida
Registered: 2004-05-31
Posts: 177
Website

Re: Help with script..

Meant to say i need to know how to check if they were created if not create them and then check if they were created or it iwll exit...

Offline

#22 2004-06-19 14:01:36

iotc247
Member
From: Florida
Registered: 2004-05-31
Posts: 177
Website

Re: Help with script..

That really does it? I though youd have to do an if then thing to check if its there...

Offline

#23 2004-06-19 14:24:58

iotc247
Member
From: Florida
Registered: 2004-05-31
Posts: 177
Website

Re: Help with script..

Rewritten with suggestions by olly-bh so olly-bh is this what you say it should look like...

#!/bin/sh
# This script is becoming an advanced backup script.. Any suggestions are welcome.. Email to iotc247@hotmail.com
# Contributed by Alex Heck (iotc247)
# Starting variables
# Echo variables
E=echo 
# End echo variables
# Begin Directory Variables
ADIR=/
HDIR=/home
MDIR=/
BDIR=/boot
DIR1=/var/backups
DIR2=/var/backups/work
# End Directory variables
# Name variables
AN=`date +arch-all-%F.tar.bz2`
MN=`date +arch-main-%F.tar.bz2`
BN=`date +arch-boot-%F.tar.bz2`

#if [ $1 = --help ]; then
#echo "hdbackup [-vso] [-hso]"
#echo "hdbackup -vs This is verbose mode with seperate partition backups."
#echo "               This means that you will get one for /home one for /boot and so on.."
#echo "hdbackup -vo This is verbose mode with only one the / directory and everything in it except for /proc"
#echo 
#echo 
#echo 
#echo "hdbackup -hs This is hidden mode with seperate partition backups."
#echo "             This means that you will get one for /home one for /boot and so on.."
#echo "hdbackup -ho This is hidden mode with only one the / directory and everything in it except for /proc"
#exit 0
#fi
HN=`date +arch-home-%F.tar.bz2`
# End name variables
# Move Name variables
ANM=arch-all-*.tar.bz2
MNM=arch-main-*.tar.bz2
BNM=arch-boot-*.tar.bz2
HNM=arch-home-*.tar.bz2
# End name variables
# Begin Taring Variables
# Verbose
ATARC="tar -jcvf $DIR2/$AN $ADIR --exclude=$DIR1/# --exclude=$MNT/*"
MTARC="tar -jcvf $DIR2/$MN $MDIR --exclude=$BDIR/* --exclude=$HDIR/* --exclude=$DIR1/* --exclude=/mnt/*"
BTARC="tar -jcvf $DIR2/$BN $BDIR" 
HTARC="tar -jcvf $DIR2/$HN $HDIR"
# Non-Verbose
ATARCN="tar -jcf $DIR2/$AN $ADIR --exclude=$DIR1/# --exclude=$MNT/*"
MTARCN="tar -jcf $DIR2/$MN $MDIR --exclude=$BDIR/* --exclude=$HDIR/* --exclude=$DIR1/* --exclude=/mnt/*"
BTARCN="tar -jcf $DIR2/$BN $BDIR" 
HTARCN="tar -jcf $DIR2/$HN $HDIR"
# End Taring Variables
# Moving Variables
AMV="mv $DIR2/$ANM $DIR1/"
MMV="mv $DIR2/$MNM $DIR1/"
HMV="mv $DIR2/$HNM $DIR1/"
BMV="mv $DIR2/$BNM $DIR1/" 
ROOT_UID=0  
E_NOTROOT=67
# Finished with Variables

# Check to see if you are root
if [ "$UID" -ne "$ROOT_UID" ]
then
  $E "Must be root to run this script."
  exit $E_NOTROOT # Your not root so its gonna quit
fi  
# Done checking for root access

if [ -x "$DIR1" ]; then # Checking for Dir1
    :
else
    mkdir "$DIR1"
fi
if [ -x "$DIR2" ]; then # Checking for Dir2
    :
else 
    mkdir $DIR2
fi
cd $DIR2
# Begin verbose actions
if [ $1 = -vs ]; then
$E Starting "$MDIR" backup
$MTARC || $ $E "Baclup failed"; exit 1
$E Backup completed.
$MMV
$E Starting "$HDIR" backup
$HTARC || $ $E "Baclup failed"; exit 1
$HMV; $E Backup completed
$E Beginning "$BDIR" backup
$BTARC || $ $E "Baclup failed"; exit 1 
$BMV; rm $DIR2; $E Backup completed. Finished; exit 0
fi
if [ $1 = -vo ]; then
$E "Starting full backup"
$ATARC || $ $E "Baclup failed"; exit 1
$AMV; rm $DIR2; $E Backup completed. Finished; exit 0
fi

# Ending verbose process

# Begin non-verbose process
if [ $1 = -hs ]; then
$E Starting "$MDIR" backup
$MTARCN || $ $E "Baclup failed"; exit 1
$MMV; $E Backup completed
$E Starting "$HDIR" backup
$HTARCN || $ $E "Baclup failed"; exit 1
$E Beginning "$BDIR" backup
$BTARCN || $ $E "Baclup failed"; exit 1 
$EMV; rm $DIR2; $E Backup completed. Finished; exit 0
fi
if [ $1 = -ho ]; then
$E "Starting full backup"
$ATARCN || $ $E "Baclup failed"; exit 1
$AMV; rm $DIR2; $E Backup completed. Finished; exit 0
fi
# End non-verbose process

# normal output for plain hdbackup
echo "hdbackup [-vso] [-hso]"
echo "hdbackup -vs This is verbose mode with seperate partition backups."
echo "         This means that you will get one for /home one for /boot and so on.."
echo "hdbackup -vo sThis is verbose mode with only one the / directory and" 
echo "             everything in it except for /proc"
echo 
echo 
echo 
echo "hdbackup -hs This is hidden mode with seperate partition backups."
echo "             This means that you will get one for /home one for /boot and so on.."
echo "hdbackup -ho This is hidden mode with only one the / directory and"
echo "             everything in it except for /proc"
exit 0

Offline

#24 2004-06-19 16:01:13

iotc247
Member
From: Florida
Registered: 2004-05-31
Posts: 177
Website

Re: Help with script..

How would i make this use getopts instead of the if [ $1 = -c ]; then stuff.

Offline

#25 2004-06-19 16:51:10

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Offline

Board footer

Powered by FluxBB