Skip to content

Instantly share code, notes, and snippets.

@tobalsan
Created July 29, 2014 11:32
Show Gist options
  • Save tobalsan/a930819f9c8d770f9cbd to your computer and use it in GitHub Desktop.
Save tobalsan/a930819f9c8d770f9cbd to your computer and use it in GitHub Desktop.

Full Debian backup procedure

The backup

Create the shell script

Before anything, create the backup dir:

$ sudo mkdir /backup

Copy this and create a new shell script file wherever you want (here we'll save it as/backup/sysbackup.sh).

#!/bin/bash

# Place ourselves at root
cd /

# Create a backup of the /etc/fstab, the hd configuration may change on reinstall
cp /etc/fstab /etc/fstab.bkp

# Saving a list of installed packages
dpkg --get-selections > /backup/dpkg-selections.txt

# Achiving and compressing system. CAREFUL: archive name must be identical to the name in the first exclude so the archive doesn't try to include itself
tar -cvpzf /backup/systembackup.tar.gz \
--directory=/ \
--exclude=backup/systembackup.tar.gz \
--exclude=dev/* \
--exclude=etc/fstab \
--exclude=etc/network/interface \
--exclude=etc/grub.conf \
--exclude=etc/lvm \
--exclude=home \
--exclude=lost+found \
--exclude=mnt/* \
--exclude=media/* \
--exclude=proc/* \
--exclude=run/* \
--exclude=sys/* \
--exclude=tmp/* \
--exclude=var/backups \
--exclude=var/lib/dpkg \
--exclude=var/lib/apt \
--exclude=var/cache/apt \
--exclude=var/lib/aptitude \
--exclude=var/run \
--exclude=var/tmp \
--exclude=var/lock/* \
--exclude=var/cache/apt/archives/* \
. 2> /backup/error.log

Set the script as executable and launch it

$ sudo chmod 750 /backup/sysbackup.sh
$ sudo /backup/sysbackup.sh

The restore

Say we placed the full backup file at the root folder /. Untar the archive:

$ cd /
$ tar -zxvpf fullbackup.tar.gz

Now two cases:

You used the exact script

your programs should be backed up and restored. I recommend doing:

# As root
$ apt-get update
$ apt-get upgrade

At this moment, it is very likely you will get an error message like:

A copy of the C library was found in an unexpected directory:
  '/lib/x86_64-linux-gnu/libc-2.13.so'
It is not safe to upgrade the C library in this situation;
please remove that copy of the C library or get it out of
'/lib/x86_64-linux-gnu' and try again.

dpkg : erreur de traitement de libc6_2.13-33_amd64.deb (--install) :
 le sous-processus nouveau script pre-installation a retourné une erreur de sortie d'état 1
Des erreurs ont été rencontrées pendant l'exécution :
 libc6_2.13-33_amd64.deb 

If you don't, good for you, but if you encounter this message, just do this:

$ aptitude download libc6
$ dpkg-deb --extract libc6_2.13-33_amd64.deb libc
$ dpkg-deb --control libc6_2.13-33_amd64.deb libc/DEBIAN

Then open libc/DEBIAN/preinst and comment out the line where it says exit 1 right after the warning message. Then do:

$ dpkg-deb --build libc
$ dpkg -i libc.deb

And you should be good. Just run again:

$ apt-get clean
$ apt-get autoclean
$ apt-get update
$ apt-get upgrade

You customized the script

But if you customized the script to exclude /usr (packages), you must reinstall them (or if you want to manually reinstall packages anyway) This is how we tell dpkg to reinstall all our packages:

$ dpkg --set-selections < selections.txt
$ apt-get update
$ apt-get dselect-upgrade
$ apt-get dist-upgrade

Done !

Sources:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment