Skip to main content

Bash script backup MySql database

As we all know, databases is one of the most important thing you need to backup before you want to upgrade, move to new server or before you want to do a task but do not really sure about the impact. I have written a article about the command line to MySql database - How to Backup and Restore. In this article, I will show you a little bash script backup MySql database at regular intervals and deleting backups which older than 15 days from current.
This Bash script backup MySql will contain your database information so please make sure you set the right permissions, more over please don’t place the backup folder in public_html folder othervise other people will be able to see the content of your database.

Implement Bash script backup MySql database

#!/bin/bash

# Database information
user="db_username"
password="your_db_password"
host="your_host"
db_name="your_db_name"
The line at the top is the shebang and is used to select the interpreter. Others are the database credentials. In next step we will define the derectory to store the backup files also the date
# Other options
backup_path="/path/to/your/home/_backup/mysql"
date=$(date +"%d-%b-%Y")
Next step, we will create the main backup script to make sql file gzip format and set the permission
# Set default file permissions
umask 177
# Dump database into SQL file
mysqldump --user=$user --password=$password --host=$host $db_name | gzip > $backup_path/$db_name-backup-$date.sql
The last step is to delete files older than 15 days, for this we will use the find utility. First we pass the path to the directory that contains the .sql files and use a wildcard to select all. Then we need to set 2 options, the first is mtime which we use to select files that are older than a given amount of days. The next option exec allows us pass the files that are found into another utility, which I will use rm to delete the files. The last few characters are to end the line.
#Delete files older than 15 days
find $backup_path/* -mtime +15 -exec rm -f {} \;
Finally, I will gather all path of script below:
#!/bin/bash

# Database credentials
user="db_username"
password="your_db_password"
host="your_host"
db_name="your_db_name"

# Other options
backup_path="/path/to/your/backup/folder"
date=$(date +"%d-%b-%Y")

#set Default file permission
umask 177

# Dump database into SQL file
mysqldump --user=$user --password=$password --host=$host $db_name | gzip > $backup_path/$db_name-bak-$date.sql.gz

#Delete files older than 15 days
find $backup_path/* -mtime +15 -exec rm -f {} \;
That’s, you can run this script manually or create a cron job to schedule the time this bash script backup mysql database will be execute. We hope that this article is helpful for you.

Comments

Popular posts from this blog

JazzCash Mobile Account

  JazzCash Mobile Account Help Center  > JazzCash Mobile Account What is JazzCash mobile account? JazzCash Mobile Account is an actual bank account that is tagged with your mobile number and can be operated through your phone. Through this Mobile Account you can enjoy complete freedom of accessing financial services anywhere, anytime! More importantly, you don’t have to rely on traveling to a Bank branch, wait at queues or complete any documentation. Mobile Account menu works on all types of mobile phones – smart phone is not required. Customers can make deposits or withdrawals through any Mobilink Microfinance Bank Branch, Mobilink Franchise, Mobilink Business Center and Jazzcash Agents spread across Pakistan. JazzCash Mobile App In line with the continuous digitization of its services to meet demands of growing number of smartphone users, JazzCash is proud to announce Android based App for its Mobile Account users. The App offers a user friendly inte...

Difference Between Primary Key and Unique Key In Sql Server

Both  PRIMARY KEY  and  UNIQUE KEY  enforces the Uniqueness of the values  (i.e. avoids duplicate values) on the column[s] on which it is defined.  Also these key’s can Uniquely identify each row in database table. Below table lists out the major  difference between PRIMARY KEY and UNIQUE KEY : PRIMARY KEY UNIQUE KEY NULL It doesn’t allow Null values. Because of this we refer PRIMARY KEY = UNIQUE KEY + Not Null CONSTRAINT Allows Null value. But only one Null value. INDEX By default it adds a clustered index By default it adds a UNIQUE non-clustered index LIMIT A table can have only one PRIMARY KEY Column[s] A table can have more than one UNIQUE Key Column[s] CREATE SYNTAX Below is the sample example for defining a single column as a PRIMARY KEY column while creating a table: CREATE TABLE  dbo.Customer ( Id  IN...

Remove the Loadblanks.ru Browser Hijacker (Removal Guide)

What is Loadblanks.ru Browser Hijacker? Skip this and learn how to remove Loadblanks.ru Browser Hijacker! The  Loadblanks.ru Browser Hijacker  is a computer infection from the  Adware/ShortcutHijacker  family that changes the shortcuts for your installed web browsers so that they automatically open the loadblanks.ru web page. It does this by pointing the browser shortcuts to batch files located in the  %UserProfile%\AppData\Roaming\Browsers\  folder as shown below. These batch files contain an obfuscated command that opens the specified browser to a specific page. In this case, the browser will be forced to automatically open the http://ic.loadblanks.ru/ or http://im.loadblanks.ru homepages. An example of the command executed by the batch file is  start "" "c:\PROGRA~2\google\chrome\APPLIC~1\chrome.exe" "http://im.loadblanks.ru/c/b26cdff542ee7ff6?" . You can see an example of this obfuscated batch file below. Due to these modified sho...