0

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?

Draken
  • 3,134
  • 13
  • 34
  • 54

2 Answers2

1

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
ysth
  • 96,171
  • 6
  • 121
  • 214
0

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!"
Mustafa DOGRU
  • 3,994
  • 1
  • 16
  • 24