Linux Shell Script to backup MySQL database to remote server

Database is the most important file other than web file, hence the backup is very important for the web developer and web owner.

By referring to the current post, you may backup your database automatically from your Linux box.



# Specify the temporary backup directory

# Database Name

# store the current date
date=`date ‘+%Y-%m-%d’`

# Specify Ftp details

# Dump the mysql database with the current date and compress it.
/usr/bin/mysqldump -u$dbuser -p$dbpasswd $dbname | gzip > $BKUPDIR/$date.$dbname.sql.gz

# Change directory to the backup directory

# Upload the backup
ftp -n $ftpserver <<!EOF!
user $ftpuser $ftppass
mput *.sql.gz

# Remove the local backup file
/bin/rm -f /$BKUPDIR/$date.$dbname.sql.gz


With the above script, you may save it as and just schedule a cronjob to run it as your needed time. Linux Cheat Sheet Cheat Sheet:
Arrow up – list the history command that just done.
Ctrl + Z – Put the current process to the background.

date – display the server date and time.
df – show the disk space detail.
dmesg – print the boot message.
du – estimate the disk usage for the path.
finger – similar to who but you could use ‘finger <username>’ to show the selected user.
free – show free memory on the server.
hostname – show the hostname of your current server.
last – display who ever logged into the server and information
ps – showing all CPU processes.
pwd – show the current path.
uname – showing server information such as kernel, operating system, etc.
uptime – showing the server up time.
w – advance who option to show the server uptime, load and who is logged into the server.
who – show who is logged into the server.
whoami – show your current username.

man [command] – show the description of a command.
whatis [command] – show short description of a command.

more [filename] – show one screen of the file and you are able to scroll it.
less [filename] – similar to more but you are allow to scroll back.
head [filename] – show first 10 line of the file content.
tail [filename] – show last 10 line of the file content.
cat [filename] – print all file content.

top – show top process with server information.
kill – kill a certain process
killall [processname] – kill all of the process such as httpd, mysql, etc.
skill – kill all of the process from a user.

ls – list file.
dir – simple list file.
cd – change directory.
rm – remove a file
mkdir – create a folder.
rmdir – remove a folder.
touch – create a file.
mv – move or rename a file.
cp – copy a file.

tar zcf – create tar.gz file
tar cf – create tar file
tar zxf – untar tar.gz file
tar xf – untar tar file
gzip – create gz file
gunzip – unzip gz file

How to Disable directory listing

Some web hosting server setting allow the directory listing by default and you wish to disable it as you might not use the hosting space for web purpose but storage purpose.

There are several ways to disable it through your space as steps below.

Apache Linux with cPanel
If your hosting company provide cPanel, it will be great it allow you easily to disable it through the control panel.

In this guide, Mick Genie guide you how to disable it through cPanel 11.25(x3 themes).
1. Log into the cPanel control panel.
2. Look for Advance box and click on Index Manager.
3. A pop up box show and select the domain name that you wish to disable it.
4. Select the folder.
5. From the radio button, select No Indexing and save it.

Apache Linux without cPanel
If you do not come with cPanel, it isn’t a hard step to disable it.

1. Log into the SSH console.
2. Change directory to the public_html path(assume your root path is /home/mickgenie/public_html/)

cd /home/mickgenie/public_html

3. Edit/create the .htaccess file

vi .htaccess

4. Add the line as below to the .htaccess file by click on Insert button and insert,

Options -Indexes

5. Save it by type in :wq!.