Difference between revisions of "User:Gen2ly/System backup and reinstall"
m (→Backup Script) |
Kynikos.bot (talk | contribs) (Template:i18n is deprecated, use interlanguage links, see Help talk:I18n#"Dummy" interlanguage links and deprecation of Template:i18n) |
||
Line 1: | Line 1: | ||
[[Category:System recovery]] | [[Category:System recovery]] | ||
− | |||
This article is intended to show you how to backup your configurations and your package list, then to do a full system restore, restore packages, and finally restore your configurations.. | This article is intended to show you how to backup your configurations and your package list, then to do a full system restore, restore packages, and finally restore your configurations.. | ||
Revision as of 13:53, 13 June 2012
This article is intended to show you how to backup your configurations and your package list, then to do a full system restore, restore packages, and finally restore your configurations..
Contents
Motivation
The need for this documentation is uncommon in the sense that the need to restore from configurations only is only really necessary for the following reasons:
- You would like to change your system architecture (e.g. 32bit to 64bit).
- If a program or programs begin to behave unexpectedly and no help in the forums (or elsewhere) is available is able to fix the problem. By chance reinstalling your programs and other configurations might fix the problem.
- You have limited hard disk space and are not capable of doing a full restore from backup.
Backup
Using tar
in a script can make archiving configurations get done in just a couple steps.
Include and Exclude Files
Tar has the ability to read from both an include and an exclude file. This means that you can tell tar everything you would like to include in the backup and exclude just by using two files. The format used is one line per file or directory that indicates the full path. For example:
/etc/pacman.conf /etc/rc.conf /home/user ...
And is invoked like this:
tar --files-from=include.txt --exclude-from=exclude.txt -c --xz -f backupname.tar.xz
The name of the files can be anything you want. The exclude file is like the include file but additionally has the ability to be able to use regexps, as well as being able to be commented and have blank lines.
Backup Script
This backup script has the ability to add to the include and exclude files and the ability to backup the configurations. The usage is:
bckcfg <i|e|n|c> - backup configurations i - add to the include list a file or folder e - add to the exclude list a file or folder or regexp pattern n - add note c - create backup
For example to add to the include file:
bckcfg i /etc/rc.conf
And to add to the exclude file:
bckcfg e .thumbnails/
The script is able to detect the full path so writing a relative or partial path is acceptable.
To backup, this script names your backup by several definable variables, removes old backups if desired, then backs up the configurations. The include and exclude files are placed in the same directory as the backup script.
bckcfg
#!/bin/bash # Backup configurations # Backup destination directory #bckp_dest="/home/todd/Desktop" bckp_dest="/run/media/todd/Backup/backup-configs" # Backup name comp="${HOSTNAME}" # Computer name dist="arch" # Distro type="configs" date="$(date "+%F")" bckp_file="$bckp_dest/$comp-$dist-$type-$date.tar.xz" # Include and exclude file locations scrpt_dir="/home/todd/.scripts/backup" incl_file="$scrpt_dir"/${0##*/}-inc.txt excl_file="$scrpt_dir"/${0##*/}-exc.txt note_file="$scrpt_dir"/${0##*/}-nte.txt scrp_help () { echo " ${0##*/} <i|e|n|c> - backup configurations i - add to the include list a file or folder e - add to the exclude list a file or folder or regexp pattern n - add note c - create backup" } del_old_bckps () { if [[ -n "$(find "$bckp_dest" -mtime +30)" ]]; then find "$bckp_dest" -name "$distro-$type-*" -mtime +30 -exec rm {} \ \; && echo " Deleted backups older than one month." fi } case $1 in # Add to include list file or folder i ) shift for f in "$@"; do # Check if selection(s) exist if [ ! -e "$f" ]; then echo " File \""$f"\" does not exist." continue fi # Append file/folder to include list full_path=$(readlink -f "$f") echo "$full_path" >> "$incl_file" && \ echo " Added \""$f"\" to ${0##*/} include file." done # Sort entries sort -u "$incl_file" -o "$incl_file" ;; # Add to exclude list file, folder, or regexp pattern e ) shift echo " * \"${0##*/}\" doesn't check if patch is correct because the exclude file can contain regexps. Be sure the path is correct (e.g. '/mnt/win/*')" | fmt -c -u -w 80 read -p " Add \""$@"\" to ${0##*/} exclude file? (y/n): " add_exclude if [[ "$add_exclude" == [Yy] ]]; then echo "$@" >> "$excl_file" && \ echo " Added \""$@"\" to ${0##*/} exclude file." else echo " Error: \""$@"\" not added." fi ;; # Add to note file n ) shift echo ""$date"-"$(date "+%r")": "$@"" >> "$note_file" && \ echo " Added string to \""$note_file"\"." ;; # Create backup c ) # Check if backup directory exists if [ ! -d "$bckp_dest" ]; then echo " Directory \""$bckp_dest"\" does not exist." exit fi # Delete old backups #del_old_bckps # Backup configurations sudo tar --exclude-from=$excl_file --files-from=$incl_file -c --xz -f $bckp_file ;; * ) # Display usage if no parameters give scrp_help ;; esac
Packages
Package lists can be created that can re-install your programs upon a restore. If you have the hard disk space available, you might also want to consider saving the install packages (*.pkg.tar.gz
) as well.
Creating a Package List
You can create a list of all installed official packages with:
pacman -Qqe | grep -v "$(pacman -Qqm)" > pkglist-off.txt
This will create a list of all packages in the official, enabled pacman repositories (i.e. core, extra, community and testing).
To create a list of all local packages (includes packages installed from the AUR):
pacman -Qqm > pkglist-loc.txt
Saving Package Tarballs
Pacman saves all package tarballs in /var/cache/pacman/pkg/
. Saving these will increase your re-install speed so consider saving these as well. You might want to think about reducing the size of the cache before backing up too. Pacman has the ability to remove any uninstalled packages from the cache with:
pacman -Sc
Yaourt
If you use Yaourt to install packages from the AUR, you might want to consider setting up a cache for it (Yaourt by default does not save the built package tarballs). To setup a cache directory, edit /etc/yaourtrc
to include one:
ExportToLocalRepository /var/cache/pacman/pkg-local
Then give the directory the necessary permissions so Yaourt can write to it as a regular user:
mkdir -p /var/cache/pacman/pkg-local chmod 766 /var/cache/pacman/pkg-local
Copy these packages to your seperate medium.
Storing the Backup
After you have made up your tarred configurations, package lists, and (optionally) your install packages, you are going to need to store them on a seperate medium than your install partition/drive. Do not put your package lists and install packages in your tarred configurations. This is because all packages must be reinstalled first before you restore your configurations to prevent file conflicts (pacman will not install packages with file conflicts). If you have large enough USB Flash Drive these work well. Optionally you can burn them to a CD or use a partition utility like gparted to create an extra partition. If using CD's you can span large archives by using the split utility. To create a new partition consider using the Parted Magic CD which has gparted on it.
Restoring
Restoring will involve:
- Installing the base system through the AIF (Arch Installation Framework).
- Changing root.
- Reinstalling all your packages.
- Extracting your configurations.
- Adding a new user.
AIF Install
Install Arch Linux as you normally would through the AIF on the LiveCD.
Change Root
When finished, mount your USB Flash Drive (or whatever medium you choose to save your configurations... on).
mkdir /backup-files mount /dev/<disk-drive-parition> /backup-files
Your Arch install will already be mounted on /mnt
so now copy these files to your Arch install:
mkdir -p /mnt/opt/restore cd /backup-files cp -a * /mnt/opt/restore
Now you will need to chroot
(Change Root) to your Arch install:
cd /mnt cp /etc/resolv.conf /mnt/etc mount -t proc none /mnt/arch/proc mount -t sysfs none /mnt/arch/sys mount -o bind /dev /mnt/arch/dev chroot . /bin/bash
Reinstall your Packages
Reinstall packages from the official repositories, the AUR, and locally installed packages separately to better diagnose problems if they occur.
Official
First reinstall packages from the official repositories;
pacman -Sy pacman -S --needed $(cat /opt/restore/pkglist-off.txt)
The AUR
Yaourt comes in handy here. To quickly install yaourt again:
wget http://aur.archlinux.org/packages/yaourt/yaourt.tar.gz tar xvf yaourt.tar.gz && cd yaourt* makepkg -s pacman -U yaourt-*.pkg.tar.gz
Then to install AUR pakages from the list:
yaourt -S $(cat /opt/restore/pkglist-loc.txt | grep -vx "$(pacman -Qqm)")
grep -vx ...
here is used to remove packages that are already installed. This comes in useful in case you have to restart the command because you had trouble installing one of the packages. If you have packages already built by yaourt and in your yaourt cache, you can avoid recompiling again by going to that cache and installing the packages manually (pacman -U ...
).
Extract Configurations
Once all packages have been installed you can extract the configurations:
tar xvf /opt/restore/hostname-arch-configs-date-tar.gz -C /mnt
A couple things to look out for:
- Be aware of any changes to your partition layout. If you changed your partition, you will need to edit both
/etc/fstab
and/boot/grub/menu.lst
. - If you had special options for the kernel ram disk (initrd), then you will have to re-compile it before your reboot to get your expected behavior.
Final Details
Good time to add your user now before you reboot. When creating a user, consider giving the user a unique user id (UID). This will help prevent conflicts in the future with other users and programs having the same UID (UIDs for users generally start at 1000):
useradd -m -u 1050 -G audio,optical,power,storage,users,video -s /bin/bash user
If you have restored a user home directory (/home/user) from your backup configurations, the -m switch will give a warning about an already existing home directory but will not alter the directory. Do not forget to change permissions in your home directory if your UIDs differ:
chown -R username:users /home/user
Now, reboot. Expect a few unexpected things here. No re-install is perfect. ALSA may pop up a warning and may have to be configure again and there may be a few other things unconsidered. That's it. Congratulations on your reinstall.