Linux

How to auto backup data all website MySQL Database and file data website






For manage data website is very difficulty for you, because you need to backup data of MySQL or mariadb and data in your website, and you will spent much time to do it. In easy way for you, today Twelveshow will share how to auto backup mysql or mariadb and data website automatic, it is easy to do, you just use scrip code and use cron (/etc/crontab ) in order to Ubuntu system work to Backup all data website your website by thyself.
Below is the way how to Auto backup MYSQL or Mariadb and data website.

1.How to backup MySQL or Mariadb auto.

-First you need to creat a Scrip code which has Example code mysqlbackup.sh below

!/bin/bash
—————————————-
OPTIONS
—————————————-
USER=’root’ # MySQL User
PASSWORD=’mast3r*+’ # MySQL Password
DAYS_TO_KEEP=0 # 0 to keep forever
GZIP=1 # 1 = Compress
BACKUP_PATH=’/backups/www’
—————————————-
Create the backup folder
if [ ! -d $BACKUP_PATH ]; then
mkdir -p $BACKUP_PATH
fi
Get list of database names
databases=mysql -u $USER -p$PASSWORD -e "SHOW DATABASES;" | tr -d "|" | grep -v Database
for db in $databases; do
if [ $db == ‘information_schema’ ] || [ $db == ‘performance_schema’ ] || [ $db == ‘mysql’ ] || [ $db == ‘sys’ ]; then
echo “Skipping database: $db”
continue
fi
date=$(date -I)
if [ “$GZIP” -eq 0 ] ; then
echo “Backing up database: $db without compression”
mysqldump -u $USER -p$PASSWORD –databases $db > $BACKUP_PATH/$date-$db.sql
else
echo “Backing up database: $db with compression”
mysqldump -u $USER -p$PASSWORD –databases $db | gzip -c > $BACKUP_PATH/$date-$db.gz
fi
done
Delete old backups
if [ “$DAYS_TO_KEEP” -gt 0 ] ; then
echo “Deleting backups older than $DAYS_TO_KEEP days”
find $BACKUP_PATH/* -mtime +$DAYS_TO_KEEP -exec rm {} \;
fi

You need to create a file with filename extension .sh Example mysqlbackup.sh and pass MySQL User & MySQL Password and place Partition with File backup.
Example:
USER=’root’ # MySQL User
PASSWORD=’mypassword’ # MySQL Password
DAYS_TO_KEEP=0 # 0 to keep forever
GZIP=1 # 1 = Compress
BACKUP_PATH=’/backups/www’

2.How to auto backup data website.

below code is Script code for auto zip file your data website for keep the place that you want to backup.

!/bin/bash
—————————————-
OPTIONS
—————————————-
DAYS_TO_KEEP=2 # 0 to keep forever
WWW_PATH=’/var/www’
BACKUP_PATH=’/backups/www’
—————————————-
Create the backup folder
if [ ! -d $BACKUP_PATH ]; then
mkdir -p $BACKUP_PATH
fi
change into the web root directory
cd “$WWW_PATH”
if [ “$(pwd)” != “$WWW_PATH” ] ; then
echo “Failed to change directory to root of web path”
exit
fi
for website in * ; do
if [[ -d $website && ! -L “$website” ]]; then
echo “Found website folder: $website”
date=$(date -I)
tar -cvpzf $BACKUP_PATH/$date-$website.tar.gz $website
fi
done
Delete old backups
if [ “$DAYS_TO_KEEP” -gt 0 ] ; then
echo “Deleting backups older than $DAYS_TO_KEEP days”
find $BACKUP_PATH/* -mtime +$DAYS_TO_KEEP -exec rm {} \;
fi

You create Filename extension .sh example www-backup.sh and pass Address website and address backup Example :
WWW_PATH=’/var/www’ is and website location
BACKUP_PATH=’/backups/www’ is a place for keeping File backup website.

3.Crontab auto run.

Created Script code backup data website and MySQL or mariadb database already, so now you will do on cron in order all codes process by themself.
First, you do user root and visite adress #nano /etc/crontab and add more codes below:

m h dom mon dow user command
15 18 * * * root /root/SQL/mysqlbackup.sh
15 18 * * * root /scripts/www-backup.sh

17 * * * * root cd / && run-parts –report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts –report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts –report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts –report /etc/cron.monthly )
#

Point you need to add more
15 18 * * * root /root/SQL/mysqlbackup.sh it processes to run code mysqlbackup.sh auto for your backup mysql។
15 18 * * * root /scripts/www-backup.sh it processes to run code www-backup.sh auto your backup data website.

Conclusion

We hope these strategies will help you to keep and manage your datas website, so it is easy way for backup your data website.

Leave a Reply

Your email address will not be published. Required fields are marked *

Most Popular

To Top