Auto backup mysql ubuntu server

When you website or product have lots of important data then you might be always worry about database backup. But taking the database backup daily is a time-taking job. Here we are providing solution for automatic MySql database backup on Monthly / Hourly / Weekly / Daily or Hourly basis.

To take backup of mysql database automatically you need to do following steps:

Step 1:

Connect and login to your SSH Terminal of your Ubuntu server.

Step 2:

Install mysqldump and zip to take backup.

sudo apt install mysql-client-5.7

sudo apt-get install zip

Step 3:

Create a backup directory in your server root.

sudo mkdir /backup

Change permission of backup directory.

sudo chmod -Rf /backup

Step 4:

Write backup code in a shell script file.

touch /backup/script.sh

Open file for edit:

nano /backup/script.sh

Add following lines and save.

#!/bin/bash
now=$(date +%d%m%Y-%H:%M:%S)
filename=$1
backupfilename=$1-$now
mysqldump -u [Database Username] -p[Database Password]  -h [Database Host] [Database Name] > /backup/backup$backupfilename.sql
zip -r /backup/backup$backupfilename.zip /backup/backup$backupfilename.sql
rm /backup/backup$backupfilename.sql

Now your shell script is ready to take backup of your database. To test this script run following.

./backup/script.sh

Step 5:

Now add a scheduler to automatically execute this script. To do this use cron and use the following to add the script in cron:

crontab -e

Add the line below to execute the script at daily midnight.

0 0 * * * /backup/script.sh

Now save and exit.

Now you will get back up on daily basis.

Send MySql Database Backup on your mail

If you want your backup receive on your mail then follow the following steps to configure email.

Step 1:

Install mail client mutt.

sudo apt install mutt

Step 2:

Allow sending email without size limit.

sudo postconf -e mailbox_size_limit=0
sudo postconf -e message_size_limit=0

Step 3:

Send the email with backup attachment to an email id.

Add following in script.sh file:

echo "Hi, Your database backup for date $backupfilename is ready" | mutt -a /backup/backup$backupfilename.zip -s "Database Backup - $backupfilename" --

#!/bin/bash
now=$(date +%d%m%Y-%H:%M:%S)
filename=$1
backupfilename=$1-$now
mysqldump -u [Database Username] -p[Database Password]  -h [Database Host] [Database Name] > /backup/backup$backupfilename.sql
zip -r /backup/backup$backupfilename.zip /backup/backup$backupfilename.sql
rm /backup/backup$backupfilename.sql
echo "Hi, Your database backup for date $backupfilename is ready" | mutt -a /backup/backup$backupfilename.zip -s "Database Backup - $backupfilename" -- 

Now you will get a daily email. Enjoy backup.

Keywords:


Creating backups to increase your site’s redundancy is a smart move in the face of a disaster. This is why most website admins build and download an offline version of their website data every single day for recovery. However, this traditional method of doing things has two major drawbacks. For one, it’s effort-intensive (i.e., not lazy-proof), and two, you lose all your recent data if your website crashes before you have a chance to create your daily backup!

In this article, we’ll look at how to back up your website’s backend MySQL database in Linux or Ubuntu. But for those of you who are looking for a more elegant solution, we will also cover automated backups. With automation, once you set it up, you don’t need to worry about that one time you forgot to create a backup or pushing all changes made to your website onto the recovery copy. It takes care of it for you!

MySQL provides a command-line utility, mysqldump, that can be utilized to create backups by exporting databases as raw SQL files. You can run the command manually every day or install an automation script, like automysql backup, that will do it for you on a daily basis.

  • Use the mysqldump Command:

From your terminal, without logging into your MySQL console, execute the command using the basic syntax:

$ mysqldump -u [username] -p[password] [database_name] > [backup_file_name].sql

Note that there is no space between -p and the password you input.

  • Back-Up a Single Table

You can also back up the data of an individual table directly onto a text file using the general syntax mentioned below:

Select * Into Outfile 'table_backup_file' From name_of_table;

  • Restore MySQL Database:

To restore your database from the backup you created in the previous step, log in to MySQL console by entering your credentials:

$ mysql -u [username] -p

Once you’re logged in, type the following command to restore the database, modifying the parameters as required:

$ mysql -u [username] -p[password] [database_name] < [backup_name].sql

  • Automate the Backup Process:

To automatically back up your database, you will first need to install the automysqlbackup package. To do this, use the following command:

$ sudo apt-get install automysqlbackup

The script will run automatically on a daily basis, and backups will be stored under the “/var/lib/automysqlbackup” directory. Additionally, you can create a crontab entry to run automysqlbackup at any particular time.

To view the contents of the daily backup folder, run the following command:

$ sudo ls -a /var/lib/automysqlbackup/daily

If you’d like to modify any configurations or add customizations to the utility, you can edit the file located at “/etc/default/automysqlbackup” by executing the command below:

$ sudo nano /etc/default/automysqlbackup

To create on-demand backups, run the command manually, as shown below:

$ sudo automysqlbackup

CodeGuard Website Backup & Restore

As an automated backup solution for website data, CodeGuard safeguards you from experiencing massive downtime for issues related to data loss. It acts like an undo button or an autosave checkpoint in a game, except it works for your website and associated databases. In case of any buggy developmental updates, crashes, etc., you can revert to the last clean version with a single click.

Let’s look at how to back up your MySQL Database using CodeGuard:

How to Automatically Schedule Your Backups with CodeGuard

CodeGuard can back up your site either through FTP/SFTP or using a WordPress plugin. You can set it up to back up a few files, copy the entire website, or only make a copy of the data that has changed.

Get CodeGuard for Only $1.63/Month!

Schedule Automatic FTP Backup with CodeGuard. Buy it Today and Save Up to 50% With SectigoStore!

Shop Now

Login to your CodeGuard Dashboard and follow the steps below:

Step 1: Choose Your Backup Option

You can add a website to back up using a WordPress plugin or via FTP. You can use either, but it is recommended to use the FTP/SFTP option.

Auto backup mysql ubuntu server

Step 2: Obtain the Required Information to Add Your Website

For adding a website via FTP or SFTP, you will need some information from your hosting provider such as:

  • the URL of the website you want to add,
  • the FTP or SFTP server,
  • user credentials, and
  • port number.

You can find this information under settings in your hosting account. It’s always recommended to use SFTP instead of FTP whenever possible, as it’s more secure.

Auto backup mysql ubuntu server

Step 3: Select Your Root Directory

If the connection is successful, the next step takes you to the Capture Your Code window, where you must select or manually specify your root directory. Next, press Select Root Directory.

Auto backup mysql ubuntu server

Step 4: Select the Contents to Backup

Once that’s done, select the website content (specific files and folders) that you would like to back up and click Begin First Backup.

Auto backup mysql ubuntu server

Step 5: Add a Database from the Completion Window

Once your first backup is complete, you will be taken to the completion window. From here, you can add a database to back it up, or you can also do it from your website dashboard.

Note: You cannot add a database without adding a website first.

Auto backup mysql ubuntu server

Step 6: Get Database Details

To add a database, you will need:

  • a database name,
  • MySQL hostname or IP,
  • your username,
  • your password and
  • the port number.

If you are unable to find this information in your hosting account, please contact the CodeGuard support team to help you with the details. The default port number for MySQL is 3306.

Step 7: Enter Database Credentials and Test Connection

Access your CodeGuard dashboard and select the website for which you wish to add a database. This step will take you to the website’s dashboard.

Auto backup mysql ubuntu server

On the website dashboard, you will find an option to add your database. Once you click on it, you will be taken to the test database connection page where you need to enter your database’s IP or hostname.

Auto backup mysql ubuntu server

Auto backup mysql ubuntu server

Enter the credentials to access your database.

Auto backup mysql ubuntu server

In the next section, select the databases you want to add, click on Add Databases, and you’re done!

Auto backup mysql ubuntu server

How do I automatically backup MySQL?

SQLBackupAndFTP.
Create a backup job by clicking Jobs > Add Backup Job..
Establish a connection with your MySQL Server..
Select the databases you want to back up..
Specify the location where the backups will be stored. ... .
Create a backup schedule..
Enter your email to receive fail/success notifications..

How do I backup MySQL database in Ubuntu?

How to Back Up a MySQL Database on Linux / Ubuntu?.
Backup MySQL Database on Linux/Ubuntu..
CodeGuard Website Backup & Restore..
Step 1: Choose Your Backup Option..
Step 2: Obtain the Required Information to Add Your Website..
Step 3: Select Your Root Directory..
Step 4: Select the Contents to Backup..

How do I schedule a MySQL backup in Linux?

How to Automate MySQL Database Backups in Linux.
Create a database backup..
Zip the backup..
Encrypt the compressed file..
Send the backup to Internet storage using FTP, Dropbox, AWS, Google Drive, etc..
Receive email notification concerning backup results..
Create a backup schedule..
Delete old backups..

Is percona XtraBackup free?

Percona XtraBackup is a free, online, open source, complete database backups solution for all versions of Percona Server for MySQL and MySQL®.