Simple Script to backup dir and Mysql db

Hi all,
I have a simple script, which for sure i need to enhance more and more. For now, it is just making a compress file in tar.gz format in output dir.
Here is script:

#!/bin/bash

#Defines output file
OUTPUT=/Documents/backup_$(date +%Y%m%d%H%M%S).tar.gz

#Defines directory to backup
INPUTDIR="/Documents/Assignments"

#Display message about starting the backup
echo “Starting backup of directory $INPUTDIR to file $OUTPUT”

#Start the backup
tar -czf $OUTPUT $INPUTDIR

#Checking the status of the last process:
if $? == 0 ]; then
#Display confirmation message
echo "The file: "$OUTPUT
echo "was created as a backup for: "$INPUTDIR
else
#Display error message message
echo "There was a problem creating: "$OUTPUT
echo "as a backup for: "$INPUTDIR
fi

But whenever i run it in terminal, it make the backup file but also throw this error in terminal, it also compress the Documents dir inside it. Which i don’t want.

Starting backup of directory /Documents/Assignments to file /Documents/backup_20091201141406.tar.gz
tar: Removing leading `/’ from member names
: 24: 0: unexpected operator
There was a problem creating: /Documents/backup_20091201141406.tar.gz
as a backup for: /Documents/Assignments

Also i want to know a bit about the leading ‘/’ from member names.

Any ideas to enhance and add MySQL database code to it would be greatly appreciated.

Thanks

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

First the tar issue… the leading slash is removed so that when you
extract it you do not forcefully try to extract to / (the root of the
filesystem) because that’s stupid to try to do. Having that gone lets you
extract to anywhere which is a good thing. Ignore that informational
message (add 2>/dev/null to your tar -cvf line).

Regarding the error you only need one ‘=’ for comparison in bash, so
change your lines to ‘if $? = 0 ] ; then’ and you will probably be fine.

Good luck.

mmarif4u wrote:
> Hi all,
> I have a simple script, which for sure i need to enhance more and more.
> For now, it is just making a compress file in tar.gz format in output
> dir.
> Here is script:
>> #!/bin/bash
>>
>> #Defines output file
>> OUTPUT=/Documents/backup_$(date +%Y%m%d%H%M%S).tar.gz
>>
>> #Defines directory to backup
>> INPUTDIR="/Documents/Assignments"
>>
>> #Display message about starting the backup
>> echo “Starting backup of directory $INPUTDIR to file $OUTPUT”
>>
>> #Start the backup
>> tar -czf $OUTPUT $INPUTDIR
>>
>> #Checking the status of the last process:
>> if $? == 0 ]; then
>> #Display confirmation message
>> echo "The file: "$OUTPUT
>> echo "was created as a backup for: "$INPUTDIR
>> else
>> #Display error message message
>> echo "There was a problem creating: "$OUTPUT
>> echo "as a backup for: "$INPUTDIR
>> fi
>
> but whenever i run it in terminal, it make the backup file but also
> throw this error in terminal:
>> Starting backup of directory /Documents/Assignments to file
>> /Documents/backup_20091201141406.tar.gz
>> tar: Removing leading `/’ from member names
>> : 24: 0: unexpected operator
>> There was a problem creating: /Documents/backup_20091201141406.tar.gz
>> as a backup for: /Documents/Assignments
>>
> Also i want to know a bit about the leading ‘/’ from member names.
>
> Any ideas to enhance and add MySQL database code to it would be greatly
> appreciated.
>
> Thanks
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJLFLrNAAoJEF+XTK08PnB5jz0P/jRkX7A6VfaqfgPkJpO+liiV
0YfAwbffj0XNvDmLz1rz53gPVte9j2bNgDL1S8iEJhXRhv2rgS/kkKjE5oIkvVkd
W2NtREPdurCdoJiibZ9gjfpuqhXYzx5uGzueV0mdBvAj7V8RVMHsFwIcWR9UuQAr
D72Q6GDB2i4KwBgaz+djxoY6gFWvRKQK87sj7SLm5FRdcGDXAFUvJbhKlGgLnZSb
GoMsbQW2459/aOgMWBZtKshlGf68/Hs9ZCQL+zd7a4fcfmt4LyY4IW7mzocbtXl+
x/QyzLAYJwVlqFFhyG5cA6obyBU8mIGXNkxlCSFRI/qkwhYBnWAx+A3z6G0Ovqkh
3z4ZUyUd0JEbsF4MOizenlOheQlavgBK4CEwWhmQ6Y+BdL+hShdZ6wE7OsDNz+hX
vwqmh/TF4nvCenOE1fUtny950O/yYfJb1Mpv5JUpTwGvcOwZkTi/Ja75wSoP87nn
Z6d+lUTGMZ0PpLtiZjEpcb6JYFC7/o0fsCOF33PYgUrrUeEItmmOIGy+VbZfgwm6
dYvQii3sl/X0WPq4v6WYe2BPMsGNP2tq2+Yd89QlTla4sMLhI6Z2DgC0BalGsOcb
JGrU0x8oqpBFHIdfQvs4BdUc25PD2xUZh5GKIXoiujivrHpAYp5a0YsWegoxOUxr
0CVOIOU5bTqzhJhESQJo
=dXTx
-----END PGP SIGNATURE-----

Thanks ab, that lines did the trick and working fine now.
One more thing, it is also including the base(main) dir in this case which is ‘Documents’, is there any way to exclude it in the compression.

Ok, i successfully manage to exclude the Documents dir.
A bit addition to script at the top:

cd /Documents/
#Defines output file
OUTPUT=backup_$(date +%Y%m%d%H%M%S).tar.gz

#Defines directory to backup
INPUTDIR=“Assignments”

I am now working on MySQL databases now, will post the result here.

Here is a simple addition to script for backing up MySQL database(s).

cd /Documents/
mysqldump -u root -p pwd --opt db_server1 > db_server1_$(date +%Y%m%d).sql

ODIR=db_server1_$(date +%Y%m%d%H%M%S).tgz
tar -zcvf $ODIR db_server1_$(date +%Y%m%d).sql 2>/dev/null
if  $? = 0 ]; then
echo "Backup created successfully for: "db_server1_$(date +%Y%m%d).sql
rm db_server1_$(date +%Y%m%d).sql
else
echo "Error occurred while creating backup for: "db_server1_$(date +%Y%m%d).sql
fi

So the final script would be like this which is doing the following tasks:
1- backup the mentioned dir with compression.
2- backup the selected MySQL database with compression.
3- Good to use for web server backup.

#!/bin/bash

cd /Documents/

#Defines output file
OUTPUT=backup_$(date +%Y%m%d%H%M%S).tar.gz

#Defines directory to backup
INPUTDIR="Assignments"

#Display message about starting the backup
echo "Starting backup of directory $INPUTDIR to file $OUTPUT"

#Start the backup
tar -czf $OUTPUT $INPUTDIR 2>/dev/null

#Checking the status of the last process:
if  $? = 0 ]; then
#Display confirmation message
echo "The file: "$OUTPUT
echo "was created as a backup for: "$INPUTDIR
else
#Display error message
echo "There was a problem creating: "$OUTPUT
echo "as a backup for: "$INPUTDIR
fi

#MySQL dump 
mysqldump -u**root** -p**pwd** --opt db_server1 > db_server1_$(date +%Y%m%d).sql

#Output backup file name
ODIR=db_server1_$(date +%Y%m%d%H%M%S).tgz
tar -zcvf $ODIR db_server1_$(date +%Y%m%d).sql 2>/dev/null

#Check status of last process
if  $? = 0 ]; then

#Print successful msg with removing the *.sql file from the dir
echo "Backup created successfully for: "db_server1_$(date +%Y%m%d).sql
rm db_server1_$(date +%Y%m%d).sql
else

#Print error, if some thing went wrong
echo "Error occurred while creating backup for: "db_server1_$(date +%Y%m%d).sql
fi

Next step is store the files remotely on another server.

Any ideas and enhancements are welcome.

There are always many ways to do things in Unix/Linux. So the following expresses only my peronal taste.

To me the construction

tar ....
if ${?}
then   ...
else   ...
fi

is needlessly complicated. I prefer

if tar ....
then   ...
else   ...
fi

You may also deduct from this that I prefer the ${…} construct instead of $… where you always have the chance that you neverteless have to usee the {…} in some places like:

$version.tar.gz

would not work, but

${version}.tar.gz

would. Likewise you van not leave the { } out in

mv ${file%htm} ${file}html

Thanks Henk van Velden,
That was really very useful information.

Any ideas about connecting to remote server over ssh and make the backup files on local server(from where the script will run in cron).

I will not start a discussion about “how to make backups” here, because that is very dependant on what you want to achive and it seeems that you already made up your mind on what you want.

File transfer using ssh is best done using sftp, which is part of ssh. When using privat/public keys and passwordless login, it can even be done from a cron script. But do not use root for the transfer then.

When talking about backups rsync comes to mind. It is very efficient in transport. It can also be used with ssh, but I never did this.

Hi
Have a look through the script here for ideas (sftp);
http://gnu.ethz.ch/linuks.mine.nu/ibackup/


Cheers Malcolm °¿° (Linux Counter #276890)
SUSE Linux Enterprise Desktop 11 (x86_64) Kernel 2.6.27.37-0.1-default
up 2 days 12:40, 2 users, load average: 0.00, 0.06, 0.07
GPU GeForce 8600 GTS Silent - CUDA Driver Version: 190.18

have a look at my dumpmysql script in my repo. It is a cron script which automatically backs up DBs on a daily basis and has a config file in /etc where you can configure things and enable it