Server shell backup script (bash) - bash

Name of a script - backup_script.sh
Location of a script on server - /home/company_folder/company_site_backups
Line added to the cron file:
#monthly /home/company_folder/company_site_backups/backup_script.sh
#!/bin/bash
DIR="/home/company_folder/company_applications/*"
BACKUPDIR="/home/company_folder/company_site_backups"
NOW=`date +\%Y\%m\%d`
cd $DIR
for i in $DIR; do zip -r "${i%/}.zip" "$BACKUPDIR/$i-$NOW"; done
ls -l
echo "Done!"
But unfortunately my script does not work properly. Actually. It does not run at all! I do not see any errors in the syntax.
Does anyone know how to fix it?

The cd $DIR seems strange; if the first entry found by /home/company_folder/company_applications/* is a directory it will change to that directory; if it is a file (or company_applications is empty) it will get an error.
Perhaps everything is running correctly except that because of the above your ls -l is not running in the directory you expect? Try removing the cd and changing it to ls -l $DIR.
It also seems very strange to me that you are zipping up content from a backup directory into an applications directory. Perhaps you meant to be doing:
zip -r "$BACKUPDIR/`basename $i`-$NOW" $i

could you try this;
#!/bin/bash
DIR="/home/company_folder/company_applications/*"
BACKUPDIR="/home/company_folder/company_site_backups"
NOW=`date +\%Y\%m\%d`
cd $DIR
for i in $DIR
do
base=$(basename "$i")
zip -r $BACKUPDIR/$base-${NOW}.zip $i
done
ls -l $BACKUPDIR
echo "Done!"

Related

File not adding in zip by bash script but work in command line in terminal

I am running a script in cron and trying to add a file in a zip by below code,
#!/bin/bash
cd /home/mainfolder/cron
date=$(date --date=yesterday +%F)
FILE=/home/mainfolder/folder/$date.zip
if test -f "$FILE"; then
echo $date
else
wget -r -np -nH --user=USER --password=PASSWORD https://sercret-website.com/$date.zip -P /home/mainfolder/folder/
zip -ur $date.zip file.txt
mkdir temp
cp /home/mainfolder/folder/$date.zip /home/mainfolder/temp/
cd /home/mainfolder/temp/
fi
my file name is yesterday date lets say today is 2019-12-18 (yyyy-mm-dd) my file name is 2019-12-17.zip
when I run in bash sh -x script.sh it didn't work
when i run normally in terminal it work well
zip -ur 2019-12-17.zip file.txt
I am using CentOS server.
Anyone know what I am doing wrong.
I tries
#!/bin/sh != #!/bin/bash
#!/bin/sh
#!/bin/bash
nothing work
I'd change date=$(date --date=yesterday +%F) to:
date=`date --date=yesterday +%F`
and write zip as
zip -ur ${date}.zip file.txt
and shebang as:
#!/bin/sh -x
Then I'd look for reason of failure in cron log files.

Bash: Creating subdirectories reading from a file

I have a file that contains some keywords and I intend to create subdirectories into the same directory of the same keyword using a bash script. Here is the code I am using but it doesn't seem to be working.
I don't know where I have gone wrong. Help me out
for i in `cat file.txt`
do
# if [[ ! -e $path/$i ]]; then
echo "creating" $i "directory"
mkdir $path/$i
# fi
grep $i file >> $path/$i/output.txt
done
echo "created the files in "$path/$TEMP/output.txt
You've gone wrong here, and you've gone wrong here.
while read i
do
echo "Creating $i directory"
mkdir "$path/$i"
grep "$i" file >> "$path/$i"/output.txt
done < file.txt
echo "created the files in $path/$TEMP/output.txt"
78mkdir will refuse to create a directory, if parts of it do not exist.
e.g. if there is no /foo/bar directory, then mkdir /foo/bar/baz will fail.
you can relax this a bit by using the -p flag, which will create parent directories if necessary (in the example, it might create /foo and /foo/bar).
you should also use quotes, in case your paths contain blanks.
mkdir -p "${path}/${i}"
finally, make sure that you are actually allowed to create directories in $path

Bash find where the current execution is located

I have a shell program in a directory (ie dir1/dothis.sh) - works fine when I cd to that directory and ./dothis.sh
if I created a ln to that directoy with a new name - dir2 and do dir2/dothis.sh
it would execute but it thinks the current execution path is the new dir where dir2 is pointing to at
in dothis.sh - how do I find where dothis.sh actually located? The problem I have is that the dir1/dothis.sh can be relocated from system to system so there is no warranty where dir1/dothis.sh can be hard code
Use the bash built-in
#!/bin/bash
echo "Current path: $PWD"
try this:
#!/bin/bash
echo $0
a=`pwd`
echo $a
b=$a"/"$0
echo `dirname $b`
How about
dirname $(readlink -f $0)
It will also resolve symbolic link if any...

Why aren't the BASH commands in for loop working

I have a simple code which is:
#!/bin/bash
#LaTex code generator for figures.
ls *.pdf > pdfs.file
ls *.ps > ps.file
pwd=$(pwd)
for i in {1..2}
do
# var=$(awk 'NR==$i' 'pdfs.file')
echo $pwd
echo $pwd > testfile
done
Why aren't the commands in the for loop working?
The $pwd isnt echoed neither is the testfile created.
I tried these commands without the for loop in a terminal and they work fine.
My bash file is made executable by chmod +x bashfile.sh
What I am trying to do is this:
Find pdfs or eps files and populate pdfs.file and eps.file with their file names.
Step through row by row and grab these file names and append to $pwd.
Then append $pwd$var to the include graphics command in latex.
I'm not sure what you're doing wrong, but this works fine for me:
for i in {1..2}; do
echo $PWD
echo $PWD > /tmp/testfile
done
echo "File contents: $(cat /tmp/testfile)"
This successfully returns the following:
/tmp
/tmp
File contents: /tmp
Did you write the bash file using a Windows editor? Maybe you have a problem with line terminators. Try dos2unix bashfile.sh.

Quick bash script to run a script in a specified folder?

I am attempting to write a bash script that changes directory and then runs an existing script in the new working directory.
This is what I have so far:
#!/bin/bash
cd /path/to/a/folder
./scriptname
scriptname is an executable file that exists in /path/to/a/folder - and (needless to say), I do have permission to run that script.
However, when I run this mind numbingly simple script (above), I get the response:
scriptname: No such file or directory
What am I missing?! the commands work as expected when entered at the CLI, so I am at a loss to explain the error message. How do I fix this?
Looking at your script makes me think that the script you want to launch a script which is locate in the initial directory. Since you change you directory before executing it won't work.
I suggest the following modified script:
#!/bin/bash
SCRIPT_DIR=$PWD
cd /path/to/a/folder
$SCRIPT_DIR/scriptname
cd /path/to/a/folder
pwd
ls
./scriptname
which'll show you what it thinks it's doing.
I usually have something like this in my useful script directory:
#!/bin/bash
# Provide usage information if not arguments were supplied
if [[ "$#" -le 0 ]]; then
echo "Usage: $0 <executable> [<argument>...]" >&2
exit 1
fi
# Get the executable by removing the last slash and anything before it
X="${1##*/}"
# Get the directory by removing the executable name
D="${1%$X}"
# Check if the directory exists
if [[ -d "$D" ]]; then
# If it does, cd into it
cd "$D"
else
if [[ "$D" ]]; then
# Complain if a directory was specified, but does not exist
echo "Directory '$D' does not exist" >&2
exit 1
fi
fi
# Check if the executable is, well, executable
if [[ -x "$X" ]]; then
# Run the executable in its directory with the supplied arguments
exec ./"$X" "${#:2}"
else
# Complain if the executable is not a valid
echo "Executable '$X' does not exist in '$D'" >&2
exit 1
fi
Usage:
$ cdexec
Usage: /home/archon/bin/cdexec <executable> [<argument>...]
$ cdexec /bin/ls ls
ls
$ cdexec /bin/xxx/ls ls
Directory '/bin/xxx/' does not exist
$ cdexec /ls ls
Executable 'ls' does not exist in '/'
One source of such error messages under those conditions is a broken symlink.
However, you say the script works when run from the command line. I would also check to see whether the directory is a symlink that's doing something other than what you expect.
Does it work if you call it in your script with the full path instead of using cd?
#!/bin/bash
/path/to/a/folder/scriptname
What about when called that way from the command line?

Resources