Linux backup/restore/reinstall

root's picture

Below you will find various ways to backup disks, files, package lists, etc.

How to re-install same packages to a different machine.
Or, how to backup the packages status and do a re-install when needed.

First method for deb linuxes:

To backup:

root@gz:~# cp -p /etc/apt/sources.list /backup/sources.list
root@gz:~# dpkg --get-selections > /backup/packages_list.txt

To restore:

root@gz:~# cp -p /backup/sources.list /etc/apt/sources.list
root@gz:~# dpkg --set-selections < /backup/packages_list.txt
root@gz:~# apt-get -y update
root@gz:~# apt-get dselect-upgrade
root@gz:~# cp -p /backup/sources.list /etc/apt/sources.list
root@gz:~# apt-get update
root@gz:~# dpkg --set-selections < /backup/packages_list.txt
root@gz:~# dselect --> Select 'i' to install the software.

Second method for deb linuxes:

To backup:

root@gz:~# aptitude search -F '%100p' '~i!~M' > /backup/packages_list.txt
To restore:

root@gz:~# xargs aptitude --schedule-only install < /backup/packages_list.txt; aptitude install;

For RPM based linuxes:

To backup:

root@gz:~# rpm -qa > /backup/rpms_list.txt
To restore:

root@gz:~# yum -y install $(cat /backup/rpms_list.txt)
root@gz:~# up2date -i $(cat /backup/rpms_list.txt)

How to backup and restore a partition or an entire disk with dd:

To backup: root@gz:~# dd if=/dev/hda conv=sync,noerror | gzip -c > /backup/hda.img.gz
To restore: root@gz:~# gunzip -c /backup/hda.img.gz | dd of=/dev/hda conv=sync,noerror

How to copy a disk with dd avoiding bad blocks:

root@gz:~# dd if=/dev/sda2 of=/dev/sdb2 bs=4096 conv=sync,noerror

How to backup an LVM partition:

root@gz:~# /sbin/lvcreate -L64M -s -n snapusrlv /dev/rootvg/usrlv
root@gz:~# /bin/dd if=/dev/rootvg/snapusrlv conv=sync,noerror | gzip -c > /backup/usrlv.dd.gz
root@gz:~# /sbin/lvremove -f /dev/rootvg/snapusrlv

How to backup mbr and partition table:

With dd: root@gz:~# /bin/dd if=/dev/sda of=$BKPLOC/backup-sda.mbr count=1 bs=512
With sfdisk: root@gz:~# sfdisk -d /dev/sda > $BKPLOC/backup-sda.sf

How to restore mbr and partition table:

With dd: root@gz:~# dd if=$BKPLOC/backup-sda.mbr of=/dev/sda
With sfdisk: root@gz:~# sfdisk /dev/sda < $BKPLOC/backup-sda.sf

How to backup mysql database with LVM snapshots:

root@gz:~# mysql -u root -p
mysql> flush tables with read lock;
mysql> flush logs;
mysql> quit;

root@gz:~# lvcreate --snapshot --size=1000M --name=backupmysql /dev/vg01/mysql

root@gz:~# mysql -u root -p
mysql> unlock tables;
mysql> quit;

root@gz:~# mkdir -p /backup/mysql
root@gz:~# mount -o ro /dev/vg01/backupmysql /backup/mysql
root@gz:~# cd /backup/mysql
root@gz:~# tar czvf mysql.$(date +"%m-%d%-%Y).tar.gz mysql
root@gz:~# umount /backup/mysql
root@gz:~# lvremove -f /dev/vg01/backupmysql

How to backup with dd and netcat over the network:

On target server: nc -l 19000|bzip2 -d|dd of=/Volumes/vbox/incoming/candy150g.raw
On the source server: dd if=/dev/vboxvg/candy150g|bzip2 -c|nc 19000
A bit of explanation:
On the target server you are starting nc to listen on port 19000 and behind telling bzip2 to uncompress and dd to write on a designated image (you can use partition too)
On the source server, you start dd and read from the image then bzip is compressing it and nc is sending the image over the network to the target server's port 19000.
Simple, no? :) Do not worry if you do not see any output. Just leave the commands running and check the network statistics, you will see it there.
At the end of the transfer, you should see the dd output and you will have to do CTRL+C on both ends:
314572800+0 records in
314572800+0 records out
161061273600 bytes (161 GB) copied, 29930.9 s, 5.4 MB/s

Note: if you are transferring within your internal network, remove the compression (bzip2) and your transfer will be faster:
bash-3.2$ nc -l 19000|dd bs=16000 of=/Volumes/vbox/incoming/pluto200g1.raw

root@core:~# dd bs=16000 if=/dev/vboxvg/pluto200g1|nc 19000
13421772+1 records in
13421772+1 records out
214748364800 bytes (215 GB) copied, 3608.66 s, 59.5 MB/s

Thou shalt not steal!

If you want to use this information on your own website, please remember: by doing copy/paste entirely it is always stealing and you should be ashamed of yourself!
Have at least the decency to create your own text and comments and run the commands on your own servers and provide your output, not what I did!

Or at least link back to this website.