Script to backup/restore/migrate web app

This forum is dedicated to advanced help and support :

Ask here your questions about advanced usage of Mageia. For example you may post here all your questions about network and automated installs, complex server configurations, kernel tuning, creating your own Mageia mirrors, and all tasks likely to be touchy even for skilled users.

Script to backup/restore/migrate web app

Postby xboxboy » May 15th, '16, 12:18

I've got several web apps that I've setup on my development box. So far I've been been manually performing a mariadb dump, and zipping the web directory folder. Now I'd like to move it to a production environment, so I need to be backing it up properly.

So far this is what I've been using, I've copied it more or less from a couple of google searches.

Code: Select all
#!/bin/sh

# Set date
THEDATE=`date +%d%m%y%H%M`

# Site 1
THESITE1="websitename"
THEDB1="db_name"
THEDBUSER1="dbusername"
THEDBPW1="dbpassword"


mysqldump -u $THEDBUSER1 -p${THEDBPW1} $THEDB1 | gzip > /home/user/sitebackups/files/dbbackup_${THEDB1}_${THEDATE}.bak.gz

tar czf /home/user/sitebackups/files/sitebackup_${THESITE1}_${THEDATE}.tar -C /var/www/html/ $THESITE1/
gzip /home/user/sitebackups/files/sitebackup_${THESITE1}_${THEDATE}.tar

# Uncomment and adjust to remove backups > 5days old
# find /var/www/html/$THESITE1/backups/files/site* -mtime +5 -exec rm {} \;
# find /var/www/html/$THESITE1/backups/files/db* -mtime +5 -exec rm {} \;


For ease of storage/migrating I'm thinking the tar step should add the db dump to it, so then there's only one file for a complete backup/migrating.

Couple quick questions:
A: Should I halt the webserver while the mariadb dump is performed?
B: Can anyone suggest any improvements or confirm this is ok?

I intend to chron this job.
xboxboy
 
Posts: 400
Joined: Jun 2nd, '13, 06:41

Re: Script to backup/restore/migrate web app

Postby doktor5000 » May 15th, '16, 14:29

Slightly related, if you use date formats in filenames, better use date +"%Y%m%d" as that way the files will also be sorted in file manager etc. by their name same as their date, ascendingly by year down to day.
Cauldron is not for the faint of heart!
Caution: Hot, bubbling magic inside. May explode or cook your kittens!
----
Disclaimer: Beware of allergic reactions in answer to unconstructive complaint-type posts
User avatar
doktor5000
 
Posts: 18020
Joined: Jun 4th, '11, 10:10
Location: Leipzig, Germany

Re: Script to backup/restore/migrate web app

Postby jiml8 » May 15th, '16, 23:35

Your tar step has the z flag set, so the subsequent gzip should be redundant. You should just rename your created tar file as .tar.gz or .tgz.

Given that one of the most important roles for a database engine is to enforce consistency, I would not expect mariah to do any commits to a database while a dump was underway. This should mean that you don't have to stop mariah or the website while taking a backup; your backup will be consistent. Now, I've never encountered the particular problem so I won't guarantee what I just said. However, I would most certainly expect this behavior.
jiml8
 
Posts: 1254
Joined: Jul 7th, '13, 18:09

Re: Script to backup/restore/migrate web app

Postby doktor5000 » May 16th, '16, 10:43

For consistency and for safety, better always quote your variables, especially for filenames - you may have some which contain blanks or any other special characters, so better always use e.g.
Code: Select all
"${THESITE1}"
Cauldron is not for the faint of heart!
Caution: Hot, bubbling magic inside. May explode or cook your kittens!
----
Disclaimer: Beware of allergic reactions in answer to unconstructive complaint-type posts
User avatar
doktor5000
 
Posts: 18020
Joined: Jun 4th, '11, 10:10
Location: Leipzig, Germany


Return to Advanced support

Who is online

Users browsing this forum: No registered users and 1 guest