0

This script1 is not working as intended. I will explain below:

#!/bin/bash
### SETUP ###
USER="MYUSER"
DIRS="MYDIR"
BUCKET="mybucket"
DOACCESS="ACCESSKEY"
DOSECRET="SECRETKEY"
NAME="FILENAME"
EXPIRE="7 days"
NOW=$(date +"%d-%m-%Y")
DAY=$(date +"%a")

# ...lots of code that is working great...

### CLEAN OLD FILE FROM BUCKET ###

### This is the line that I am having issues with.
sh ./s3-del-old.sh ''"${BUCKET}"'/backup' '"${EXPIRE}"'

END

The script2 got copied from here.

What I had prior to following some instructions on Bash: Variable in single quote linked below was:

sh ./s3-del-old.sh "$BUCKET/backup" "$EXPIRE"

This did not work and was ignored when running the bash script.

I attempted to leave out the stuff that doesn't matter to the question below, although I believe I may have confused things. For this I apologize. Very simply, I have a line in script1 that calls another script2. I use variables to meet the needs of the script. To which it is not working and I cannot find a easy to understand solution online, thus the need to post the question.

----END OF UPDATE----

I have looked at some of the answered questions, but I am not finding a solution that fits my needs or one that I can understand fully to use for my needs.

I have tried following this, although I need a little more help.

This is what I am trying to do:

I have a backup script that uses DreamHost's DreamObjects to store my backups. The annoying part with DreamObjects is that it doesn't have any built in features for removing files created x days ago. Hence my problem. I would like to add a call to a bash file from my bash file. If that makes sense. :) If not, the code in question is below, you should be able to understand then.

I would really like to be able to add the code to my current script instead of using a separate file. I just don't know how to rewrite it properly without spending more time than I have on it. I found the code at.

My variables that matter for this problem:

BUCKET="mybucket"<br>
EXPIRE="7 days"

This is the line that calls the file:

sh ./s3-del-old.sh ''"${BUCKET}"'/backup' '"${EXPIRE}"'

This provides me with an error of date:

invalid date `-"${EXPIRE}"'

The file uses the following syntax to work:

s3-del-old "bucket" "30 days"

It does work perfectly when I use it in the command line on it's own, I just would like to add the call to one file so that I can use one cronjob instead of two. Plus, this way I can use the script with any of my domains/buckets by changing the variables. :)

Community
  • 1
  • 1
TAG
  • 68
  • 1
  • 8
  • do you mean that BUCKET variable is passed correctly but only EXPIRE fails? If so, does EXPIRE needs to be a variable? – Stefano Maffulli Oct 05 '16 at 17:29
  • The argument `'"${EXPIRE}"'` is inside single quotes `' "${EXPIRE}" '`. Inside single quotes shell variables are not expanded. Is this what you want to do?. If you need more help do provide the part of the script that use `'"${EXPIRE}"'`. –  Oct 05 '16 at 20:16
  • i think you just need this: `sh ./s3-del-old.sh "$BUCKET/backup" "$EXPIRE"` – webb Oct 05 '16 at 20:21
  • @StefanoMaffulli EXPIRE is the variable for the amount of days that is used to determine what to delete. I guess I don't need it to be a variable, although I still would like to learn how it should be done. The bucket variable passes for the script since my files are uploading to the bucket. I am not sure about the line in question. – TAG Oct 05 '16 at 21:03
  • @sorontar The part of the script that uses EXPIRE is included in the question (This is the line that calls the file: sh ./s3-del-old.sh ''"${BUCKET}"'/backup' '"${EXPIRE}"'). I have the variables declared before the line as BUCKET="mybucket" and EXPIRE="7 days". – TAG Oct 05 '16 at 21:04
  • @webb That is what I tried originally to no avail. – TAG Oct 05 '16 at 21:04
  • Thanks for all of the comments! I appreciate it greatly! – TAG Oct 05 '16 at 21:05
  • @webb Sorry I should have included more information. When I use sh ./s3-del-old.sh "$BUCKET/backup" "$EXPIRE" it seems to ignore the line completely. I have also tried using "$BUCKET/backup/*" which seems to be working when I run the command in the command line, just not when I try to call it in the bash file itself. – TAG Oct 05 '16 at 21:31
  • No, you have not provided the line inside the script `./s3-del-old.sh` that make use of the variable `$EXPIRE`. Without that we are unable to understand what actually happens inside the script, good luck!. –  Oct 05 '16 at 22:07
  • And at this point there are so many comments that it's hard to understand what's going on. Consider updating the question, adding the details requested and the information you provided in the comments. – Stefano Maffulli Oct 05 '16 at 22:15
  • @StefanoMaffulli I have updated the question as best as I can. – TAG Oct 05 '16 at 23:35
  • @sorontar I'm not sure you are reading the question correctly. I have updated the question with "condensed" version of my bash script. I have left out the page length of code I am using to make it easier to read. Thanks for your help! Plus I don't want to escape all of my code to make it easier to read on here... :) – TAG Oct 05 '16 at 23:36
  • @webb I updated my question in hopes that it clarifies what I'm trying to do. If you can have another look I'd appreciate it. :) – TAG Oct 06 '16 at 01:17
  • I am perfectly clear with 'your script'. It is the `that calls another bash script` part that you haven't clarify. –  Oct 06 '16 at 01:37
  • The only line of the new `other script` that you finally showed that is relevant to the value of the second argument is ```older_than_unixtime=`date -d "-$2" +'%s'` ```. For that line you certainly should **not** use single quotes. If using simple double quotes doesn't work, the problem is with something else. –  Oct 06 '16 at 01:52
  • @sorontar Oh I see why you are saying what you are saying. The second script is what I am trying to start with the first script. The second script works great when using it in the command line: ./s3-del-old.sh "mybucket/backup/*" "7 days". I am trying to start it from my first script by adding a line at the bottom that uses the BUCKET and EXPIRE variables. The problem is that I cannot get the variable values to work with the syntax. When I run my main script it doesn't seem to recognize the line trying to start the second script. – TAG Oct 06 '16 at 02:05

2 Answers2

1

The "other script" that you need to call is a bash script.

A bash script (usually) will not work if called as sh, as you are doing:

sh ./s3-del-old.sh ''"${BUCKET}"'/backup' '"${EXPIRE}"'

Please call the script with bash:

bash ./s3-del-old.sh "${BUCKET}"/backup "${EXPIRE}"

Or even better, let the script choose the shell that should run it:

./s3-del-old.sh "${BUCKET}"/backup "${EXPIRE}"

With the shebang of the file s3-del-old.sh:

#!/bin/bash
  • Thank you, I feel I am getting close or this may be the answer, although I did get the following error: ERROR: Parameter problem: Expecting S3 URI with a filename or --recursive: s3://webbk/backup/ – TAG Oct 06 '16 at 02:38
  • You're setting variables in the wrong places, maybe? I get the impression that your script is now spaghetti :) instead of calling scripts that call script, use bash functions and clean up your code. – Stefano Maffulli Oct 06 '16 at 16:56
  • @StefanoMaffulli wow! I didn't know you could read my code to know that it is spaghetti. lol! I didn't write the code for the script2 (s3-del-old.sh), I just need to use the function of it. By the way the code that I labeled ...lots of code... is just a simple MySQL dump and tar of my file system. Not hard to do! If you think I could make spaghetti out of that then... – TAG Oct 06 '16 at 20:58
  • @sorontar I may have to figure out how to do what the script2 file is doing and rewrite it for my original script. I was just trying to save time instead of trying to figure out how the author of script2 got the created date and then delete the files older than x days. I thank you again for your help and for clearing up my question! Being that at least the variables are passing correctly, I will mark your answer as the answer for this. Thank you again! – TAG Oct 06 '16 at 21:04
0

Sometimes I amaze myself at how difficult I try to make things...s3cmd has an expire function for files by create date...that will be a lot easier...

Really a big thank you to all that helped!

My bad this was suppose to be a comment not an answer. :)

TAG
  • 68
  • 1
  • 8