LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-16-2013, 04:36 PM   #1
DaemonHunter
LQ Newbie
 
Registered: Mar 2013
Posts: 13

Rep: Reputation: Disabled
Unhappy Linux Backup script issues please help


Hi! I am trying to make a basic backup script that is supposed to tar and zip a target file. Essentially it will change the syntax of tar to a more straightforward cp style command. The issue is that tar is not able to tar the first argument $1. Here is the code I have at this point; please dont be too harsh, I am relatively new to scripting.Also the over all goal of the script is to tar and zip the file in the ~/.Default_backup_repository if only one argument is supplied, else use the second argument as the directory for the file to be backed up to.

#!/bin/bash
# Script Variables
DATE=$(date +%d/%m/%Y-%H:%M:%S)
FILEDEFAULT=$(~/.Default_backup_repository/$1.$DATE.tar.gz)
DIRECTORY=$($2)
FILE=$($1)
if [ "$1" = "?" ]; then
echo "{Usage:}-----First argument is the target file for backup."
echo "{Usage:}-----Second argument is location for backup"
echo "{Usage:}-----If only one argument is supplied backup is made in ~/.Default_backup_repository/"


elif [ $# -eq 0 ]; then

echo "Supply at least one file to backup and Supply a second argument for location of backup"
echo "If no second argument is supplied the file will be tared and zipped in default location ~/.Default_backup_repository"
exit 1

elif [ $# -eq 1 ]; then

tar -cvzf $FILEDEFAULT $FILE


echo "The file will be tared and zipped in default location ~/.Default_backup_repository"

elif [ $# -eq 2 ]; then

tar -cvzf $DIRECTORY/$FILE $1
fi
echo "File backed up to "$2
exit 0

command entered: backup cheese
errors received : line 4: /home/Userhome"not my actual home folder named edited for post"/.Default_backup_repository/cheese.16/03/2013-17:16:35.tar.gz: No such file or directory
/home/Userhome/bin/backup: line 37: syntax error: unexpected end of file
please help

Last edited by DaemonHunter; 03-16-2013 at 04:55 PM.
 
Old 03-16-2013, 05:03 PM   #2
Automatic
Member
 
Registered: Mar 2013
Posts: 42

Rep: Reputation: Disabled
I'm very new to linux, so, just a guess, but, you're referring to:-

Code:
/.Default_backup_repository/cheese.16/03/2013-17:16:35.tar.gz
In the output, / means the root directory, not the home directory, which is what I assume you're trying to achieve. If you are trying to achieve directly writing to the above file, are you sure that that location exists? Try:-

Code:
dir /.Default_backup_repository/cheese.16/03/
 
Old 03-16-2013, 08:26 PM   #3
DaemonHunter
LQ Newbie
 
Registered: Mar 2013
Posts: 13

Original Poster
Rep: Reputation: Disabled
the error is reporting /home/userhome/.Default_backup_repository/cheese.16*...etc not a directory that doesnt exist in root :/
 
Old 03-16-2013, 08:28 PM   #4
Automatic
Member
 
Registered: Mar 2013
Posts: 42

Rep: Reputation: Disabled
Quote:
Originally Posted by DaemonHunter View Post
the error is reporting /home/userhome/.Default_backup_repository/cheese.16*...etc not a directory that doesnt exist in root :/
Guess I'm wrong then, although, /.Default seems to have no home/xyz/ between it.
 
Old 03-16-2013, 08:31 PM   #5
DaemonHunter
LQ Newbie
 
Registered: Mar 2013
Posts: 13

Original Poster
Rep: Reputation: Disabled
line 4: /home/Userhome"not my actual home folder named edited for post"/.Default_backup_repository/cheese.16/03/2013-17:16:35.tar.gz: No such file or directory

and in the variable ~ is used to mean /home/user/...

---------- Post added 03-16-13 at 09:32 PM ----------

thanks for your suggestion though!
 
Old 03-16-2013, 08:33 PM   #6
Automatic
Member
 
Registered: Mar 2013
Posts: 42

Rep: Reputation: Disabled
Quote:
Originally Posted by DaemonHunter View Post
line 4: /home/Userhome"not my actual home folder named edited for post"/.Default_backup_repository/cheese.16/03/2013-17:16:35.tar.gz: No such file or directory

and in the variable ~ is used to mean /home/user/...

---------- Post added 03-16-13 at 09:32 PM ----------

thanks for your suggestion though!
I know Userhome is not your actual home folder, I never suggested that, in fact, I believe I made that clear that I knew that with "XYZ" in my example, I also know that ~ is meant to mean/home/XYZ/, it's simply the fact that the output contains no data like that.

Once again, I'm very new to linux and have done next to no bash, so, ignore all my posts, they're more than likely useless (No sarcasm).
 
Old 03-16-2013, 08:52 PM   #7
DaemonHunter
LQ Newbie
 
Registered: Mar 2013
Posts: 13

Original Poster
Rep: Reputation: Disabled
i know i was saying that dir /.Default_backup_repository/ does not exist; not that u didnt know that /home/username/ was not my real home dir, i just copied what i posted in the original message so that wasnt directed at you.

tar -cvzf ~/.Default_backup_repository/cheese.tar.gz cheese
exicutes properly so i think it has something to do with how the file is used as a variable in the script.


Im replying not to start some kind of argument or prove im right, simply so that if someone else sees the post they may be better suited to help

again thanks a lot for your posts
 
Old 03-16-2013, 09:10 PM   #8
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 981
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
Line 4
Code:
FILEDEFAULT=$(~/.Default_backup_repository/$1.$DATE.tar.gz)
currently tries to run that filename and save the output when Ithink you just want to stuff the value into a variable.
Code:
FILEDEFAULT=~/.Default_backup_repository/$1.$DATE.tar.gz
And the same applies on other lines.
 
Old 03-16-2013, 09:34 PM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 15,383

Rep: Reputation: 1650Reputation: 1650Reputation: 1650Reputation: 1650Reputation: 1650Reputation: 1650Reputation: 1650Reputation: 1650Reputation: 1650Reputation: 1650Reputation: 1650
The following runs the date command and assigns the result the to variable DATE.
Quote:
DATE=$(date +%d/%m/%Y-%H:%M:%S)
This statement is trying to run "~/.Default_backup_repository/$1.$DATE.tar.gz" as command which fails since it does not exist.
Quote:
FILEDEFAULT=$(~/.Default_backup_repository/$1.$DATE.tar.gz)
Your mistake is that you used $() for all variable assignments. Unless your trying to run a command it is not required. The correct syntax is:
FILE=$1
 
Old 03-17-2013, 10:52 AM   #10
DaemonHunter
LQ Newbie
 
Registered: Mar 2013
Posts: 13

Original Poster
Rep: Reputation: Disabled
Awesome I ended up figuring out the issue thanks to your help and a little trial and error!
 
Old 03-17-2013, 11:38 AM   #11
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
Quote:
Originally Posted by DaemonHunter View Post
Awesome I ended up figuring out the issue thanks to your help and a little trial and error!
gratz, please post exactly what fixed it, the code if you dont mind, and mark this post as [SOLVED]. Congratz to resolving the issue.
 
Old 04-13-2013, 11:02 PM   #12
DaemonHunter
LQ Newbie
 
Registered: Mar 2013
Posts: 13

Original Poster
Rep: Reputation: Disabled
Dunzzzo

sorry this took so long to get back to but here is the script that finally worked as intended.

#!/bin/bash
# Script Variables
DATE=$(date +%d-%m-%Y.%H:%M:%S)
WHO=$(whoami)
FILEDEFAULT=/home/(yourusername)/.Default_backup_repository/
DIRECTORY=$2
FILE=$1
if [ "$1" = "?" ]; then
echo "{Usage:}-----First argument is the target file for backup."
echo "{Usage:}-----Second argument is location for backup"
echo "{Usage:}-----If only one argument is supplied backup is made in ~/.Default_backup_repository/"


elif [ $# -eq 0 ]; then

echo "Supply at least one file to backup and Supply a second argument for location of backup"
echo "If no second argument is supplied the file will be tared and zipped in default location ~/.Default_backup_repository"
exit 1


elif [ $# -eq 1 ]; then

tar -vzcf $FILEDEFAULT/$FILE.$DATE.$WHO.tar.gz $FILE

echo "The file will be tared and zipped in default location ~/.Default_backup_repository"

elif [ $# -gt 1 ]; then

tar -vzcf $DIRECTORY/$FILE.$DATE.$WHO.tar.gz $FILE

echo "File backed up to "$2
fi
exit 0
 
Old 04-14-2013, 12:26 AM   #13
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Just a suggestion. Please try quoting variables properly:
Code:
#!/bin/bash
# Script Variables
DATE=$(date +%d-%m-%Y.%H:%M:%S)
WHO=$(whoami)
FILEDEFAULT=/home/(yourusername)/.Default_backup_repository/
DIRECTORY=$2
FILE=$1
if [ "$1" = "?" ]; then
    echo "{Usage:}-----First argument is the target file for backup."
    echo "{Usage:}-----Second argument is location for backup" 
    echo "{Usage:}-----If only one argument is supplied backup is made in ~/.Default_backup_repository/"
elif [ "$#" -eq 0  ]; then
    echo "Supply at least one file to backup and Supply a second argument for location of backup"
    echo "If no second argument is supplied the file will be tared and zipped in default location ~/.Default_backup_repository"
    exit 1
elif [ "$#" -eq 1 ]; then
    tar -vzcf "$FILEDEFAULT/$FILE.$DATE.$WHO.tar.gz" "$FILE"
    echo "The file will be tared and zipped in default location ~/.Default_backup_repository"
elif [ "$#" -gt 1 ]; then 
    tar -vzcf "$DIRECTORY/$FILE.$DATE.$WHO.tar.gz" "$FILE"
    echo "File backed up to $2"
fi
exit 0
 
Old 04-14-2013, 08:42 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,294

Rep: Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358
Conventionally, you'd check the input args first, as there is no point doing anything else if the input is incorrect.
Also, consider putting the 'usage' stuff into a fn.

Useful links
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

Debugging:
Code:
#!/bin/bash
set -xv
 
  


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
Newbie trying to write a simple backup script to backup a single folder Nd for school stryker759a Linux - Newbie 2 09-16-2009 08:52 AM
how to create backup MYSQL Script to backup my database for every 1hour RMLinux Linux - Newbie 3 11-20-2008 10:13 AM
Backup script issues onthefritz Programming 3 06-18-2008 06:32 PM
ntfs backup script with linux amishdisco Linux - General 1 08-04-2004 07:25 PM


All times are GMT -5. The time now is 07:02 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration