Backup Script

I don't know how to repair this script :confused:

My OS: Ubuntu Server 18.04 "Bionic Beaver" LTS (64bits)

#!/bin/bash
#
### Ustawienia - sciezki do plik�w ###
DIRS="/home/ts/ /home/mc/"
BACKUP=/home/backup.$$
NOW=$(date +"%d-%m-%Y")
DELDATE=$(date -d "-7 days" +"%d-%m-%Y")
DAY=$(date +"%a")
#
### MySQL ###
MUSER="hiden"
MPASS="hiden"
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
#
### FTP ###
FTPD="Backup"
FTPU="hiden"
FTPP="hiden"
FTPS="hiden"
NCFTP="$(which ncftpput)"
#
### Start Backup for file system ###
[ ! -d $BACKUP ] && mkdir -p $BACKUP || :
#
  FTPD=$FTPD
  FILE="files-$NOW.zip"
  zip -r $BACKUP/$FILE $DIRS
### Start MySQL Backup ###
# Get all databases name
DBS='$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse "show databases")'
for db in '$DBS'
do
 FILE=$BACKUP/mysql-$db.$NOW-$(date +"%T").gz
 $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done
#
### Dump backup using FTP ###
#Start FTP backup using ncftp
ncftp -u"$FTPU" -p"$FTPP" $FTPS<<EOF
cd $FTPD
rm -r $DELDATE/*
rm -r $DELDATE
cd
mkdir $FTPD
mkdir $FTPD/$NOW
cd $FTPD/$NOW
lcd $BACKUP
mput *
quit
EOF
#
### Find out if ftp backup failed or not ###
rm -rf $BACKUP

When I run it I get error:
zip warning: name not matched: /home/mc/
but when I will modified $DIRS to: DIRS="/home/ts/ /home/mc/ /home/mc2/" I will get that same error but with mc2, because the program found this directory: /home/mc/

and next get this error

./backup.sh: 35: ./backup.sh: Syntax error: word unexpected (expecting "do")

I don't know what is bad with loop :confused:

I don't see any syntactic problems, but these two lines:

DBS='$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse "show databases")'
for db in '$DBS'

Get rid of those single quotes:

DBS=$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse "show databases")
for db in $DBS

In both cases they are preventing expansion, first of the mysql command, then of the variable you stored the output of the mysql command into.

I couldn't see one, but I'm also guessing that there is a stray quote causing the syntax error you came across.

Andrew

Thanks for found quotation marks :wink: I don't know why this script working properly on: ubuntu server 17.10 artful aardvark but on 18.04 "Bionic Beaver" have problem...

And when I leave an empty line without a comment, I get this error:
root@mines:~# sh ./backup.sh
: not found: 2: ./backup.sh:

Can you check that the script has no funny characters in it?

cat -vet backup.sh

Can you also check that every command you use (mysql, mysqldump, ncftpput etc) are installed? It may be that if this is a new installation not all the utilities you use in this script may be installed by default.

Andrew

I had installed mysql, mysqldump ncftp etc.
when I use cat command I get this:

root@mines:~# cat -vet backup.sh
#!/bin/sh^M$
#^M$
### Ustawienia - sciezki do plikM-CM-3w ###^M$
DIRS="/home/ts/ /home/mc/ /home/X"^M$
BACKUP=/home/backup.$$^M$
NOW=$(date +"%d-%m-%Y")^M$
DELDATE=$(date -d "-7 days" +"%d-%m-%Y")^M$
DAY=$(date +"%a")^M$
#^M$
### MySQL ###^M$
MUSER="hiden"^M$
MPASS="hiden"^M$
MHOST="localhost"^M$
MYSQL="$(which mysql)"^M$
MYSQLDUMP="$(which mysqldump)"^M$
GZIP="$(which gzip)"^M$
#^M$
### FTP ###^M$
FTPD="Backup"^M$
FTPU="hiden"^M$
FTPP="hiden"^M$
FTPS="hiden"^M$
NCFTP="$(which ncftpput)"^M$
#^M$
### Start Backup for file system ###^M$
[ ! -d $BACKUP ] && mkdir -p $BACKUP || :^M$
#^M$
  FTPD=$FTPD^M$
  FILE="files-$NOW.zip"^M$
  zip -r $BACKUP/$FILE $DIRS^M$
### Start MySQL Backup ###^M$
# Get all databases name^M$
DBS='$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse "show databases")'^M$
for db in $DBS^M$
do^M$
 FILE=$BACKUP/mysql-$db.$NOW-$(date +"%T").gz^M$
 $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE^M$
done^M$
#^M$
### Dump backup using FTP ###^M$
#Start FTP backup using ncftp^M$
ncftp -u"$FTPU" -p"$FTPP" $FTPS<<EOF^M$
cd $FTPD^M$
rm -r $DELDATE/*^M$
rm -r $DELDATE^M$
cd^M$
mkdir $FTPD^M$
mkdir $FTPD/$NOW^M$
cd $FTPD/$NOW^M$
lcd $BACKUP^M$
mput *^M$
quit^M$
EOF^M$
#^M$
### Find out if ftp backup failed or not ###^M$
rm -rf $BACKUP^M$

I convert win format to unix using this command and all works: perl -p -e 's/\r$//' < winfile.txt > unixfile.txt

Thanks for all :wink: