Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Dear all,
you will find attached my current version
I have tried to explain in the code what I am doing.
I am also trying to capture error messages and notify me accordingly
Code:
#!/bin/bash
# Description. This script is for compressing my thesis folder and then sending it over email. Once the email is succesfully sent remove the compressed file.
# What type of checks should I do
SCRIPTNAME=$(basename ${0})
MAILADDRESS="HideForNow"
THESISFOLDER="HideForNow"
TEMPSAVEFOLDER="HideForNow"
doEmail() { case "$1" in
3) REASON="Backup Script Finished. $(echo $SCRIPTNAME)"
SUBJECT="Thesis Backup Completed"
;;
4) REASON="Failed To compress File. $(echo $SCRIPTNAME)"
SUBJECT="Thesis Backup Script failed"
;;
5) REASON="Failed to send email $(date). $(echo $SCRIPTNAME)"
SUBJECT="Thesis Backup Script failed "
;;
*) echo "Please don't do that, Dave..." >/dev/stderr;;
esac; echo "${REASON}" | mail -r $MAILADDRESS -s "${SUBJECT}" $MAILADDRESS; }
# 4. Missing a date format without spaces
tar -jcvf $(TEMPSAVEFOLDER)myname_thesis$(date).tar.bz2 "$THESISFOLDER$"
# Questions. How to chech the above is correctly completed? How to mail it? and how to remove it afterward from the hard disk.
# 5.
doEmail 5; exit 0; # Exit 0 when normally returns
Major ones are.
a.Make the compression of the file, sending it over email and removing the compressed file afterwards
b. Use a date time stamp for the file name that has no spaces
c. Track if error happened and report the error. I tried to include the function doEmail that sends the eror message based on the current status.
I'll start with the easy question: How to list the date without spaces.
You can specify the date format with codes given in the manual (man date).
Examples:
Code:
$ date +F # This is sufficient for daily backups
2015-06-08
$ date +%F_%T # This appends the time, but ':' separators may be a problem
2015-06-08_09:53:43
$ date +%F_%H%M%S # This expresses the time without ':' separators
2015-06-08_095350
It is worth noting that files (or directories) named according to these formats will appear in chronological order when listed in alphabetical order (as with the ls command). Sometimes this is useful for managing multiple backup sets.
To detect errors and take appropriate actions, you need to test the exit codes. Each command in your script returns an integer value, known as exit code, which can be examined via the variable $? immediately after the command.
Exit code 0 (zero) generally means there was no functional error. There may still be logical errors, as when you specify the wrong files to archive.
If your script requires more specific information about errors, you will have to look up the possible exit codes for each command of interest. Sometimes they are listed in the man pages. Other times you might have to search online (e.g., google tar exit codes).
Example at the command line:
Code:
$ tar -cjf my_sound_files.tar.bz2 sounds
$ echo $?
0
Example in a script:
Code:
#!/bin/bash
# Example of exit code tests
tar -cjf my_sound_files.tar.bz2 sounds
if [[ $? == 0 ]]
then
echo "no error"
else
echo "error"
fi
Dear all,
I have been using your feedback to improve my script as following
-Using date to create a unique file name that contains no spaces. Since I would be taking these backups one per week the selected date format would be fine.
-Using date at once at the beginning to make a unique file that is used next in the code.
My code should do the following:
-Compress a folder to a file
-Send the file as attachment over email
-Remove the more recent file.
1. Currently I am getting a syntax error but I can not really find it
2. I am also missing the part where I want to remove files (so there are not files accumulated on my hard disk). What would be the safe way to do it.. so acceidentaly one day my script removes files I still need?
I would like to thank you in advance for your further support.
The code follows:
Code:
#!/bin/bash
# Description. This script is for compressing my thesis folder and then sending it over email. Once the email is succesfully sent remove the compressed file.
# What type of checks should I do
SCRIPTNAME=$(basename ${0})
MAILADDRESS="HideForNow"
THESISFOLDER="HideForNow"
TEMPSAVEFOLDER="HideForNow"
TIMESTAMP=$(data +F)
COMPRESSEDFILENAME=$(TEMPSAVEFOLDER)myname_thesis$(TIMESTAMP).tar.bz2
doEmail() { case "$1" in
4) REASON="Failed To compress File. $(echo $SCRIPTNAME)"
SUBJECT="Thesis Backup Script failed"
;;
5) REASON="Failed to send email $(date). $(echo $SCRIPTNAME)"
SUBJECT="Thesis Backup Script failed "
;;
6) REASON="Failed to remove file$(date). $(echo $SCRIPTNAME)"
SUBJECT="Thesis Backup Script failed "
;;
7) REASON="Backup Script Finished. $(echo $SCRIPTNAME)"
SUBJECT="Thesis Backup Completed"
;;
*) echo "Please don't do that, Dave..." >/dev/stderr;;
esac; echo "${REASON}" | mail -r $MAILADDRESS -s "${SUBJECT}" $MAILADDRESS; }
# 4. Compress Email
tar -jcvf COMPRESSEDFILENAME "$THESISFOLDER"
if [[ $? -ne 0 ]]
do Email 4; exit 1; # Compressing the file failed
fi
#5. Send the File by email.
mail -r $MAILADDRESS -s "Thesis Backup $TIMESTAMP" -a $COMPRESSEDFILENAME $MAILADDRESS;
if [[ $? -ne 0 ]]
do Email 5; exit 1; # Sending email failed
fi
# 6. Remove the file that it was sent over email. Would not be dangerous to call rm and delete also other files?
# 7. Everything went as planned. Bye By
doEmail 7; exit 0; # Exit 0 when normally returns
$ date +F # This is sufficient for daily backups
2015-06-08
Sorry, I must have inadvertently deleted the % character after I pasted the command and its output. The command as written above would output only the letter F. The correct command is:
1. Currently I am getting a syntax error but I can not really find it
These are simple mistakes that you should first try to figure out on your own. LinuxQuestions should not be your first (or even second) round of debugging. Run your script, or run fragments of it on the command line, and analyze the error messages. For example, look at this:
Code:
$ TIMESTAMP=$(data +F)
-bash: data: command not found
That tells you that "data" is not a valid command. Let's try "date" instead:
$ COMPRESSEDFILENAME=$(TEMPSAVEFOLDER)myname_thesis$(TIMESTAMP).tar.bz2
-bash: TEMPSAVEFOLDER: command not found
-bash: TIMESTAMP: command not found
The names "TEMPSAVEFOLDER" and "TIMESTAMP" were used in a place where bash requires a command, so it didn't work. The command may be written in this way:
The error message of an incorrectly formed "if" command is a little tricky:
Code:
if [[ $? == 0 ]]
echo "It worked."
fi
-bash: syntax error near unexpected token `fi'
but with experience you will come to recognize the meaning of that. It means that something between "if" and "fi" is missing, though it is not always easy to figure out what. In this case, the "then" keyword is missing. This is the correct form:
Code:
if [[ $? == 0 ]]
then
echo "It worked."
fi
The following line contains an error that will not give an error message:
Code:
tar -jcvf COMPRESSEDFILENAME "$THESISFOLDER"
The only problem is that the tar file will be named "COMPRESSEDFILENAME" literally.
You need the $ character to read out the value of the variable COMPRESSEDFILENAME:
Code:
tar -jcvf "$COMPRESSEDFILENAME" "$THESISFOLDER"
The quotation marks are not necessary in the above line, but some programmers throw them in anyway because they are needed in other situations.
Dear all,
Thanks for your help to improve my script.
Now I am able to compress the file correctly .. getting the right timestamp and having the folders with their correct names. Two issues
1 The if statements give me a syntax error
2 I am not sure which would be the safest way to remove a file... What is the less prone mode to write the rm command?
The code with the fixes. ( I turns until and the tar command and fails on the if below)
Code:
#!/bin/bash
# Description. This script is for compressing my thesis folder and then sending it over email. Once the email is succesfully sent remove the compressed file.
# What type of checks should I do
SCRIPTNAME=$(basename ${0})
MAILADDRESS="HideForNow"
THESISFOLDER="HideForNow"
TEMPSAVEFOLDER="HideForNow"
TIMESTAMP=$(date +%F)
COMPRESSEDFILENAME="$TEMPSAVEFOLDER"myname_thesis"$TIMESTAMP".tar.bz2
doEmail() { case "$1" in
4) REASON="Failed To compress File. $(echo $SCRIPTNAME)"
SUBJECT="Thesis Backup Script failed"
;;
5) REASON="Failed to send email $(date). $(echo $SCRIPTNAME)"
SUBJECT="Thesis Backup Script failed "
;;
6) REASON="Failed to remove file$(date). $(echo $SCRIPTNAME)"
SUBJECT="Thesis Backup Script failed "
;;
7) REASON="Backup Script Finished. $(echo $SCRIPTNAME)"
SUBJECT="Thesis Backup Completed"
;;
*) echo "Please don't do that, Dave..." >/dev/stderr;;
esac; echo "${REASON}" | mail -r $MAILADDRESS -s "${SUBJECT}" $MAILADDRESS; }
# 4. Compress Email
tar -jcvf "$COMPRESSEDFILENAME" "$THESISFOLDER"
if [[ $? == 0 ]]
then
do Email 4; exit 1; # Compressing the file failed
fi
#5. Send the File by email.
mail -r $MAILADDRESS -s "Thesis Backup $TIMESTAMP" -a $COMPRESSEDFILENAME $MAILADDRESS;
if [[ $? == 0 ]]
then
do Email 5; exit 1; # Sending email failed
fi
# 6. Remove the file that it was sent over email. Would not be dangerous to call rm and delete also other files?
# 7. Everything went as planned. Bye By
doEmail 7; exit 0; # Exit 0 when normally returns
Your function is defined as "doEmail", but in two places you invoke it as "do Email". Remove the extra space.
Both of your if statements should test for not-equal, like this:
Code:
if [[ $? != 0 ]]
To delete the tar file, you could simply use:
Code:
rm -f $COMPRESSEDFILENAME
I added the -f option so that it will ignore (that is, will not give error message) if the file does not exist.
I recommend putting this rm command in every exit path, to delete the tar file even if there are errors.
You asked:
Quote:
Would not be dangerous to call rm and delete also other files?
I cannot think of any way it could go wrong and delete other files if you use the above command. If $COMPRESSEDFILENAME names an existing file, it will be deleted. If $COMPRESSEDFILENAME names a file that does not exist, the rm command will do nothing. If $COMPRESSEDFILENAME is undefined, the rm command will do nothing.
Now the code seems to work.. up to the point of compressing my thesis directory (I can see the tar.bz2 file in my directory).. although I get an email saying that it failed to compress the file....
I guess my if statements do not check correctly if thinks have progressed.
I also still have commented the following
# 6. Remove the file that it was sent over email. Would not be dangerous to call rm and delete also other files?
#rm -f $COMPRESSEDFILENAME
I am scared that the $COMPRESSEDFILENAME might get some "funny" value and destroy my file system. I am running these scripts as root user and I still do not feel safe.
Regards
Alex
Code:
#!/bin/bash
# Description. This script is for compressing my thesis folder and then sending it over email. Once the email is succesfully sent remove the compressed file.
# What type of checks should I do
SCRIPTNAME=$(basename ${0})
MAILADDRESS="HideForNow"
THESISFOLDER="HideForNow"
TEMPSAVEFOLDER="HideForNow"
TIMESTAMP=$(date +%F)
COMPRESSEDFILENAME="$TEMPSAVEFOLDER"myname_thesis"$TIMESTAMP".tar.bz2
doEmail() { case "$1" in
4) REASON="Failed To compress File. $(echo $SCRIPTNAME)"
SUBJECT="Thesis Backup Script failed"
;;
5) REASON="Failed to send email $(date). $(echo $SCRIPTNAME)"
SUBJECT="Thesis Backup Script failed "
;;
6) REASON="Failed to remove file$(date). $(echo $SCRIPTNAME)"
SUBJECT="Thesis Backup Script failed "
;;
7) REASON="Backup Script Finished. $(echo $SCRIPTNAME)"
SUBJECT="Thesis Backup Completed"
;;
*) echo "Please don't do that, Dave..." >/dev/stderr;;
esac; echo "${REASON}" | mail -r $MAILADDRESS -s "${SUBJECT}" $MAILADDRESS; }
# 4. Compress Email
tar -jcvf "$COMPRESSEDFILENAME" "$THESISFOLDER"
if [[ $! -eq 0 ]]
then
doEmail 4; exit 1; # Compressing the file failed
fi
#5. Send the File by email.
mail -r $MAILADDRESS -s "Thesis Backup $TIMESTAMP" -a $COMPRESSEDFILENAME $MAILADDRESS;
if [[ $! -eq 0 ]]
then
doEmail 5; exit 1; # Sending email failed
fi
# 6. Remove the file that it was sent over email. Would not be dangerous to call rm and delete also other files?
#rm -f $COMPRESSEDFILENAME
# 7. Everything went as planned. Bye By
doEmail 7; exit 0; # Exit 0 when normally returns
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.