LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Linux Backup script issues please help (https://www.linuxquestions.org/questions/linux-newbie-8/linux-backup-script-issues-please-help-4175454357/)

DaemonHunter 03-16-2013 04:36 PM

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 :(

Automatic 03-16-2013 05:03 PM

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/

DaemonHunter 03-16-2013 08:26 PM

the error is reporting /home/userhome/.Default_backup_repository/cheese.16*...etc not a directory that doesnt exist in root :/

Automatic 03-16-2013 08:28 PM

Quote:

Originally Posted by DaemonHunter (Post 4913106)
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.

DaemonHunter 03-16-2013 08:31 PM

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!

Automatic 03-16-2013 08:33 PM

Quote:

Originally Posted by DaemonHunter (Post 4913109)
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).

DaemonHunter 03-16-2013 08:52 PM

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 :)

linosaurusroot 03-16-2013 09:10 PM

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.

michaelk 03-16-2013 09:34 PM

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

DaemonHunter 03-17-2013 10:52 AM

Awesome I ended up figuring out the issue thanks to your help and a little trial and error!

lleb 03-17-2013 11:38 AM

Quote:

Originally Posted by DaemonHunter (Post 4913377)
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.

DaemonHunter 04-13-2013 11:02 PM

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

konsolebox 04-14-2013 12:26 AM

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


chrism01 04-14-2013 08:42 PM

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



All times are GMT -5. The time now is 10:26 PM.