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 :( |
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 Code:
dir /.Default_backup_repository/cheese.16/03/ |
the error is reporting /home/userhome/.Default_backup_repository/cheese.16*...etc not a directory that doesnt exist in root :/
|
Quote:
|
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! |
Quote:
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). |
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 :) |
Line 4
Code:
FILEDEFAULT=$(~/.Default_backup_repository/$1.$DATE.tar.gz) Code:
FILEDEFAULT=~/.Default_backup_repository/$1.$DATE.tar.gz |
The following runs the date command and assigns the result the to variable DATE.
Quote:
Quote:
FILE=$1 |
Awesome I ended up figuring out the issue thanks to your help and a little trial and error!
|
Quote:
|
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 |
Just a suggestion. Please try quoting variables properly:
Code:
#!/bin/bash |
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 |
All times are GMT -5. The time now is 10:26 PM. |