Backup Partitions Using LVM Snapshots and FSArchiver
This method will allow online backup of the server at the partition level. It is designed to run via crontab schedule but can also be run manually.
This should be considered a full backup which means you will probably need to rely on other methods for granular backups and restores such as using rsync at the file level.
This method is great for backing up a system just prior to and just after a major upgrade of the OS or application. It is not very helpful for retrieving individual files although it could be done but would require a bit of work by temporarily restoring to an unused area, retrieving the file(s) and then destroying the temporary partition.
The /bak partition is skipped because that is where the archives are being stored.
The /tmp partition is skipped because there should not be anything in there that needs to be restored...but feel free to include it if you like.
The script below was built around a few very basic commands that do the bulk of the work but most of the code is for error handling.
Here are examples of the commands:
Code:
## Create the snapshot volume of the partition to be backed up.
lvcreate --size=5G --snapshot --name="tempsnap" /dev/LVG/root
## Create the compressed and encrypted archive of the snapshot.
fsarchiver savefs --compress=7 --jobs=1 --cryptpass="abc123" --label="insert comment here" /bak/root.fsa /dev/LVG/tempsnap
## Create an informational text file about the archive.
fsarchiver archinfo --cryptpass="abc123" /bak/root.fsa > /bak/root.txt 2>&1
## Remove the snapshot.
lvremove --force /dev/LVG/tempsnap
## Create a checksum file about the archive.
md5sum /bak/root.fsa > /bak/root.md5
## Verify that the checksum file can validate against the archive.
md5sum --check /bak/root.md5
Here is an example of a crontab entry to run the script once a day.
/var/scripts/data/crontab.root
Code:
0 4 * * * /var/scripts/prod/back-parts.sh > /dev/null 2>&1
Here is the script.
/var/scripts/prod/back-parts.sh
Code:
#!/bin/bash
#############################################################
## Name : back-parts.sh (Backup Partitions)
## Version : 1.0
## Date : 2012-06-28
## Author : LHammonds
## Purpose : Backup partitions
## Compatibility : Verified on Ubuntu Server 12.04 LTS
## Requirements : Fsarchiver, Sendemail, run as root
## Run Frequency : Once per day or as often as desired.
## Parameters : None
## Exit Codes :
## 0 = Success
## 1 = ERROR: Must be root user
## 2 = ERROR: Missing software
## 4 = ERROR: LVM problems
## 8 = ERROR: File creation problems
## 16 = ERROR: Mount/Unmount problems
###################### CHANGE LOG ###########################
## DATE VER WHO WHAT WAS CHANGED
## ---------- --- --- ---------------------------------------
## 2012-06-28 1.0 LTH Created script.
#############################################################
#### Still working on it and testing various failure conditions. ####
Here is an example of the log file:
/var/log/back-parts.log
Code:
2012-06-28_18:19:36 - Backup started.
2012-06-28_18:19:46 --- Created: /bak/srv-ubuntu-boot.fsa, 40M
2012-06-28_18:19:46 --- Created: /bak/srv-ubuntu-home.fsa, 1M
2012-06-28_18:20:33 --- Created: /bak/srv-ubuntu-root.fsa, 96M
2012-06-28_18:20:44 --- Created: /bak/srv-ubuntu-opt.fsa, 1M
2012-06-28_18:20:55 --- Created: /bak/srv-ubuntu-srv.fsa, 1M
2012-06-28_18:22:21 --- Created: /bak/srv-ubuntu-usr.fsa, 162M
2012-06-28_18:23:28 --- Created: /bak/srv-ubuntu-var.fsa, 189M
2012-06-28_18:23:40 --- Backup time: 0 hour(s) 4 minute(s) 4 second(s)
2012-06-28_18:23:40 - Backup Finished.
An example email notification when a fatal error occured:
Code:
#### Still working on it ####
An example email notification when non-fatal errors occured:
Code:
#### Still working on it ####
An example email notification when no errors occur and email notifications turned on:
Code:
From: admin@mydomain.com
To: lhammonds@mydomain.com
Sent: Friday, June 29, 2012 10:36:45 AM
Subject: Backup Completed
INFO: The partition backup job has completed without any errors.
Server: srv-ubuntu
Program: /var/scripts/prod/back-parts.sh
Log: /var/log/back-parts.log
NOTE: If the snapshot volume could not be automatically removed, here is how you do it:
Code:
LVG-srv (252, 6)
LVG-tempsnap (252, 9)
LVG-opt (252, 7)
LVG-swap (252, 1)
LVG-root (252, 0)
LVG-opt-real (252, 10)
LVG-bak (252, 8)
LVG-tmp (252, 3)
LVG-tempsnap-cow (252, 11)
LVG-usr (252, 4)
LVG-var (252, 5)
LVG-home (252, 2)
Code:
dmsetup remove LVG-tempsnap
dmsetup remove LVG-tempsnap-cow
Backup Test
Before the partitions are backed on your server, create a couple of empty test files to verify that the restore in the next section will work.
Type the following commands:
Code:
touch /important.txt
touch /srv/samba/share/important.txt
Make sure the above files are included in your backup before testing the restore in the next section.
Bookmarks