LDP/LDP/users/Jason-Evans/Linux-Complete-Backup-and-R.../Linux-Complete-Backup-and-R...

132 lines
6.7 KiB
Plaintext
Executable File

= How to Perform at Backup of a Simple LAMP Server.
Outline:
* What Do we need to back up?
** /home
** /etc
** /var/www
** MySQL/MariaDB
* How to we back it up?
** Network Storage and Cloud
** Physical Media
** Scripts
** Bakula
* How do we restore from backup?
* How do we automate the process?
** Crontabs
== Revision History
[cols="<,<,<,<",options="header",]
|==========================================================================================================================================================
|Revision |Date |Person |Note
|Revision 1.0 |2016-02-24 |Revised by: Jason Evans | Initial writing and compiling
|==========================================================================================================================================================
== Introduction
Losing data is no laughing matter. Disks fails, servers crash, and sometimes mistakes happen. How do we avoid catastrophic losses of data? We make backups! This guide is an attempt to show the Linux newbie how to back up a simple web server, but it's not limited to that. Using the techniques in this quide, you can back up your LAMP server to network, cloud, or physical storage. While it's written for a LAMP server specifically, this guide is meant to be more than just a dry howto for a specific type of server. Linux is flexible and easy to adapt to a number of uses. Where you learn one skill, you can apply it to a thousand other issues.
== License Information
This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/4.0/.
== Disclaimers
No liability for the contents of this documents can be accepted by the author, the http://www.tldp.org/[Linux Documentation Project] or anyone else. Use the concepts, examples and other content at your own risk. There may be errors and inaccuracies that may have unexpected results. Proceed with caution and although errors are unlikely, the author take no responsibility for them.
You are strongly recommended to take a backup of your system before major installation and backups at regular intervals. In addition, you are strongly recommended to use a sacrificial VM when experimenting.
== Credits
Thanks to Larisa Alekseeva for your support and to the http://trilug.org[Triangle Linux Users Group] for helping me get a foothold in Linux.
Thanks to http://www.charlescurley.com/[Charles Curley] for creating the original http://tldp.org/HOWTO/Linux-Complete-Backup-and-Recovery-HOWTO/index.html[Linux Complete Backup and Recovery HOWTO]
Thanks to Ubuntu for the https://help.ubuntu.com/lts/serverguide/backup-shellscripts.html[tar script].
== Feedback
Feedback is most certainly welcome for this document. Without your corrections, suggestions and other input, this document wouldn't exist. Please send your additions, comments and criticisms to me at: jason.s.evans@gmail.com
== Translations
Volunteers are welcome.
== Overview
== Preparation
In order to You will need a computer or server running CentOS or Ubuntu in a LAMP (Linux, Apache, MySQL, PHP) configuration. You may apply the strategies discussed to many other distributions such as Red Hat Enterprise Linux, Fedora, Debian, or one of their derivatives. This guide touches on physical servers and VM's (virtual machines).
Note:
Do your normal backups on their regular schedule. This HOWTO is useless if you don't do that.
=== How can I replicate backup the examples:
All examples in this HOWTO will be performed on fresh installations of https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-centos-7[CentOS 7] and https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-14-04[Ubuntu 14.04 LTS]. You can use the linked HOWTO's to bring your test system up to the same level if you wish. I will also create a small MySQL database using steps from https://codex.wordpress.org/Installing_WordPress#Using_the_MySQL_Client[Wordpress] in order to show how to backup and restore it.
== Where do we begin?
=== What do I need to back up?
There are three directories that absolutely must be backed up: `/home`, `/var/www`, and `/etc` as well as the MySQL database.
The following shell script uses tar to create an archive file on a remotely mounted NFS file system. The archive filename is determined using additional command line utilities.
[source,bash]
----
#!/bin/bash
####################################
#
# Backup to NFS mount script.
#
####################################
# What to backup.
backup_files="/home /var/spool/mail /etc /root /boot /opt"
# Where to backup to.
dest="/mnt/backup"
# Create archive filename.
day=$(date +%A)
hostname=$(hostname -s)
archive_file="$hostname-$day.tgz"
# Print start status message.
echo "Backing up $backup_files to $dest/$archive_file"
date
echo
# Backup the files using tar.
tar czf $dest/$archive_file $backup_files
# Print end status message.
echo
echo "Backup finished"
date
# Long listing of files in $dest to check file sizes.
ls -lh $dest
----
.$backup_files: a variable listing which directories you would like to backup. The list should be customised to fit your needs.
.$day: a variable holding the day of the week (Monday, Tuesday, Wednesday, etc). This is used to create an archive file for each day of the week, giving a backup history of seven days. There are other ways to accomplish this including using the date utility.
.$hostname: variable containing the short hostname of the system. Using the hostname in the archive filename gives you the option of placing daily archive files from multiple systems in the same directory.
.$archive_file: the full archive filename.
.$dest: destination of the archive file. The directory needs to be created and in this case mounted before executing the backup script. See Network File System (NFS) for details of using NFS.
.status messages: optional messages printed to the console using the echo utility.
.ar czf $dest/$archive_file $backup_files: the tar command used to create the archive file.
..c: creates an archive.
..z: filter the archive through the gzip utility compressing the archive.
..f: output to an archive file. Otherwise the tar output will be sent to STDOUT.
.ls -lh $dest: optional statement prints a -l long listing in -h human readable format of the destination directory. This is useful for a quick file size check of the archive file. This check should not replace testing the archive file.
This is a simple example of a backup shell script; however there are many options that can be included in such a script. See References for links to resources providing more in-depth shell scripting information.