Page 1 of 1

Script to backup/restore/migrate web app

PostPosted: May 15th, '16, 12:18
by xboxboy
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.

Re: Script to backup/restore/migrate web app

PostPosted: May 15th, '16, 14:29
by doktor5000
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.

Re: Script to backup/restore/migrate web app

PostPosted: May 15th, '16, 23:35
by jiml8
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.

Re: Script to backup/restore/migrate web app

PostPosted: May 16th, '16, 10:43
by doktor5000
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}"