Full system backup with tar: Difference between revisions

From ArchWiki
(Rewrite with some corrections, clarifications, and generalizations)
(Hard to vet this edit, see ArchWiki:Contributing#The_3_fundamental_rules and make small, transparent edits - Undo revision 362643 by Ninly (talk))
Line 1: Line 1:
[[Category:System recovery]]
[[Category:System recovery]]
This describes one approach to making a full system backup using [[tar]].
This article will show you how to do a full system backup with [[tar]].


== Overview ==
== Overview ==


Backing up with tar has the advantages of simplicity and the option of using compression to save disk space. The approach given here uses a backup script to simplify the process, and only requires a few steps. They are:
Backing up with tar has the advantages of using compression that can help save disk space, and simplicity. The process only requires several steps, they are:
# Boot from a LiveCD
# Change root to the Linux install
# Mount additional (if any) partitions/drives
# Add exclusions
# Use the backup script to backup


# Boot from live media
To minimize downtime the backup can alternatively be performed on a running system using [[Lvm#Snapshots|LVM snapshots]],
# Mount the root partition at some location
if all filesystems reside on LVM volumes.
# Mount any additional partitions or drives in the appropriate location
# Change root (chroot) to the installed linux system's root
# Add exclusions (files that won't go into the backup)
# Execute the backup script


If all filesystems reside on LVM volumes, downtime can be minimized by performing the backup on a running system using [[Lvm#Snapshots|LVM snapshots]], .
== Boot with LiveCD ==


== Prepare Backup Script ==
Many Linux bootable CDs, USBs... have the ability to let you change root to your install.  While changing root isn't necessary to do a backup, it provides the ability to just run the script without need to transfer it to a temporary drive or having to locate it on the filesystem.  The Live medium must be of the same architecture that your Linux install currently is (i.e. i686 or x86_64).


A bash script performs the backup itself. The following provides basic functionality with a few checks -- you'll need to modify it to define a suitable location for your backup tarball(s) and exclude file (if you have one).
== Changing Root ==
 
First you should have a scripting environment set up on your current Linux install.  If you do not know what that is, it means that you are able to execute any scripts that you may have as if they are regular programs.  If you do not, see this [http://linuxtidbits.wordpress.com/2009/12/03/setting-up-a-scripting-environment/ article] on how to do that.  What you'll need to do next is change root, to learn more about what changing root is, read [[Change root|this]].  When you change root, you do not need to mount any temporary file systems (<tt>/proc</tt>, <tt>/sys</tt>, and <tt>/dev</tt>).  These temporary file systems get populated on boot and you actually do not want to backup them up because they can interfere with the normal (and necessary) population process which can change on any upgrade.  To change root, you'll need to mount your current Linux installs root partition.  For example:
 
mkdir /mnt/arch
mount /dev/<your-partition-or-drive>
 
Use {{Ic|fdisk -l}} to discover you partitions and drives.  Now {{Ic|chroot}}:
 
cd /mnt/arch
chroot . /bin/bash
 
This example obviously uses bash but you can use other shells if available.  Now you will be in your scripted environment (this is provided that you have your {{ic|~/.bashrc}} sourced on entry):
 
<pre>
cat ~/.bash_profile
# If using bash, source the local .bashrc
source ~/.bashrc
</pre>
 
== Mount Other Partitions ==
 
Other partitions that you use (if any) will need to be mounted in their proper places (e.g. if you have a separate <tt>/home</tt> partition).
 
== Exclude File ==
 
{{Ic|tar}} has the ability to ignore specified files and directories.  The syntax is one definition per line.  {{Ic|tar}} also has the capability to understand regular expressions (regexps).  For example:
 
<pre>
# Not old backups                                                             
/opt/backup/arch-full*                                                                 
                                                                               
# Not temporary files                                                         
/tmp/*
 
# Not the cache for pacman
/var/cache/pacman/pkg/
...
</pre>
 
== Backup Script ==
 
Backing up with tar is straight-forward process. Here is a basic script that can do it and provides a couple checks.  You'll need to modify this script to define your backup location, and exclude file (if you have one), and then just run this command after you've {{Ic|chroot}}ed and mounted all your partitions.


<pre>
<pre>
#!/bin/bash
#!/bin/bash
# perform full system backup
# full system backup


## Backup destination
# Backup destination
#
backdest=/opt/backup
backdest=/opt/backup


## Labels for backup name
# Labels for backup name
#
#PC=${HOSTNAME}
pc=pavilion
distro=arch
distro=arch
type=full
type=full
Line 34: Line 78:
backupfile="$backdest/$distro-$type-$date.tar.gz"
backupfile="$backdest/$distro-$type-$date.tar.gz"


## Exclude file location
# Exclude file location
# update with the location of your exclude file
# (in this example, it resides in the same directory as the backup script)
prog=${0##*/} # Program name from filename
prog=${0##*/} # Program name from filename
excdir="/home/<user>/.bin/root/backup"
excdir="/home/<user>/.bin/root/backup"
exclude_file="$excdir/$prog-exc.txt"
exclude_file="$excdir/$prog-exc.txt"


## Check if chrooted
# Check if chrooted prompt.
echo -n "First chroot from live media. Are you ready to backup? (y/n): "
echo -n "First chroot from a LiveCD. Are you ready to backup? (y/n): "
read executeback
read executeback


## Check if exclude file exists
# Check if exclude file exists
if [ ! -f $exclude_file ]; then
if [ ! -f $exclude_file ]; then
   echo -n "No exclude file exists, continue? (y/n): "
   echo -n "No exclude file exists, continue? (y/n): "
Line 54: Line 96:
if [ $executeback = "y" ]; then
if [ $executeback = "y" ]; then
   tar --exclude-from=$exclude_file -czpvf $backupfile /
   tar --exclude-from=$exclude_file -czpvf $backupfile /
  # (to split the tarball into 2GB chunks, use the following line instead)
  # tar --exclude-from=$exclude_file -czpv / | split -db 2GB - $backupfile
fi
fi
</pre>
</pre>
== Splitting the Tarball ==
If you plan to burn the tar archive to optical or other external media, you may wish to split the file into chunks of a given size. This can be achieved by uncommenting the second tar command in the script (and deleting the first), and specifying the desired chunk size. Files will be saved with numeric suffixes (.tar.gz00, .tar.gz01, etc.) See {{Ic|split}} for details.
To restore from a split tarball, simply {{Ic|cat}} the files together before unpacking:
    # cat backup.tar.gz00 backup.tar.gz01 backup.tar.gz02 ... backup.tar.gzNN > backup.tar.gz
== Prepare Exclude File ==
{{Ic|tar}} has the ability to ignore specified files and directories.  The syntax is one definition per line.  {{Ic|tar}} also has the capability to understand regular expressions (regexps). For example:
<pre>
# Don't backup old backups
/opt/backup/*  # put your backup location here
# Don't backup temporary files
/tmp/*
# Don't backup the pacman cache
/var/cache/pacman/pkg/*
# Add other files (browser cache, anything else you don't need or want backed up)
</pre>
== Boot Live Media ==
You can boot from almost any handy Linux live media, as long as it offers the capability to change root and has the same architecture as the install you are backing up (i686 or x86_64). An Arch live DVD will work fine as long as you specify the correct architecture upon boot. While changing root isn't necessary to do a backup, it simplifies the backup process so that you won't need to transfer data to a temporary drive or locate backup data under some arbitrary mountpoint.
== Mount Partitions ==
First, identify the drive(s)/partition(s) with {{Ic|fdisk -l}}, then create a mount point and mount the system to be backed up:
    # mkdir /mnt/arch
    # mount /dev/<your-partition-or-drive> /mnt/arch
If you have multiple partitions or drives , you will now mount this at the appropriate point under the mount point above. For example, if you have a separate /home partition:
    # mount /dev/<your-home-partition> /mnt/arch/home
== Change Root ==
We [[change root]] because you will not want to have any of the kernel's special file systems (<tt>/proc</tt>, <tt>/sys</tt>, and <tt>/dev</tt>) mounted when you run the backup script. Because these file systems are populated "on the fly" by the kernel, having them in a backup could interfere with normal kernel operation during a system restore.
To change root:
    # cd /mnt/arch
    # chroot . /bin/bash
You may use another shell (zsh, tcsh, etc.) provided it is available in your live media. Now are now at the root level of your normal system. To make ourselves a little more "at home":
    # cd /home/<user>
    # source .bashrc
== Run the Backup Script ==
At this point, run the prepared backup script as you normally would an executable script in your environment. The tar file will be created in the target location specified in the script, and will remain there when you reboot into your normal system.

Revision as of 15:58, 25 February 2015

This article will show you how to do a full system backup with tar.

Overview

Backing up with tar has the advantages of using compression that can help save disk space, and simplicity. The process only requires several steps, they are:

  1. Boot from a LiveCD
  2. Change root to the Linux install
  3. Mount additional (if any) partitions/drives
  4. Add exclusions
  5. Use the backup script to backup

To minimize downtime the backup can alternatively be performed on a running system using LVM snapshots, if all filesystems reside on LVM volumes.

Boot with LiveCD

Many Linux bootable CDs, USBs... have the ability to let you change root to your install. While changing root isn't necessary to do a backup, it provides the ability to just run the script without need to transfer it to a temporary drive or having to locate it on the filesystem. The Live medium must be of the same architecture that your Linux install currently is (i.e. i686 or x86_64).

Changing Root

First you should have a scripting environment set up on your current Linux install. If you do not know what that is, it means that you are able to execute any scripts that you may have as if they are regular programs. If you do not, see this article on how to do that. What you'll need to do next is change root, to learn more about what changing root is, read this. When you change root, you do not need to mount any temporary file systems (/proc, /sys, and /dev). These temporary file systems get populated on boot and you actually do not want to backup them up because they can interfere with the normal (and necessary) population process which can change on any upgrade. To change root, you'll need to mount your current Linux installs root partition. For example:

mkdir /mnt/arch
mount /dev/<your-partition-or-drive>

Use fdisk -l to discover you partitions and drives. Now chroot:

cd /mnt/arch
chroot . /bin/bash

This example obviously uses bash but you can use other shells if available. Now you will be in your scripted environment (this is provided that you have your ~/.bashrc sourced on entry):

cat ~/.bash_profile
# If using bash, source the local .bashrc
source ~/.bashrc

Mount Other Partitions

Other partitions that you use (if any) will need to be mounted in their proper places (e.g. if you have a separate /home partition).

Exclude File

tar has the ability to ignore specified files and directories. The syntax is one definition per line. tar also has the capability to understand regular expressions (regexps). For example:

# Not old backups                                                               
/opt/backup/arch-full*                                                                   
                                                                                
# Not temporary files                                                           
/tmp/*

# Not the cache for pacman
/var/cache/pacman/pkg/
...

Backup Script

Backing up with tar is straight-forward process. Here is a basic script that can do it and provides a couple checks. You'll need to modify this script to define your backup location, and exclude file (if you have one), and then just run this command after you've chrooted and mounted all your partitions.

#!/bin/bash
# full system backup

# Backup destination
backdest=/opt/backup

# Labels for backup name
#PC=${HOSTNAME}
pc=pavilion
distro=arch
type=full
date=$(date "+%F")
backupfile="$backdest/$distro-$type-$date.tar.gz"

# Exclude file location
prog=${0##*/} # Program name from filename
excdir="/home/<user>/.bin/root/backup"
exclude_file="$excdir/$prog-exc.txt"

# Check if chrooted prompt.
echo -n "First chroot from a LiveCD.  Are you ready to backup? (y/n): "
read executeback

# Check if exclude file exists
if [ ! -f $exclude_file ]; then
  echo -n "No exclude file exists, continue? (y/n): "
  read continue
  if [ $continue == "n" ]; then exit; fi
fi

if [ $executeback = "y" ]; then
  tar --exclude-from=$exclude_file -czpvf $backupfile /
fi