How to backup settings and list of installed packages

If I want to make a backup of everything I have done since the fresh install of Ubuntu, what are the possible options? What all should I backup? I want to get all the settings that I changed, all the packages I installed, etc.


Solution 1:

Programs

A quick way of backing up a list of programs is to run this:

dpkg --get-selections > ~/Package.list
sudo cp -R /etc/apt/sources.list* ~/
sudo apt-key exportall > ~/Repo.keys

It will back them up in a format that dpkg can read* for after your reinstall, like this:

sudo apt-key add ~/Repo.keys
sudo cp -R ~/sources.list* /etc/apt/
sudo apt-get update
sudo apt-get install dselect
sudo dselect update
sudo dpkg --set-selections < ~/Package.list
sudo apt-get dselect-upgrade -y

* You may have to update dpkg's list of available packages or it will just ignore your selections (see this debian bug for more info). You should do this before sudo dpkg --set-selections < ~/Package.list, like this:

apt-cache dumpavail > ~/temp_avail
sudo dpkg --merge-avail ~/temp_avail
rm ~/temp_avail

Settings and Personal Data

Before you reinstall, you should probably back up the settings from some of your programs, this can easily be done by grabbing folders from /etc and all the content from your user directory (not just the stuff you can see in nautilus!):

rsync --progress /home/`whoami` /path/to/user/profile/backup/here

After you reinstall, you can restore it with:

rsync --progress /path/to/user/profile/backup/here /home/`whoami`

So all together as a pseudo-bash script.

This assumes there is only one user on the machine (remove /'whoami' otherwise) and that you used the same username on both installs (modify dest. of rsync otherwise).

dpkg --get-selections > ~/Package.list
sudo cp -R /etc/apt/sources.list* ~/
sudo apt-key exportall > ~/Repo.keys
rsync --progress /home/`whoami` /path/to/user/profile/backup/here

##  Reinstall now

rsync --progress /path/to/user/profile/backup/here /home/`whoami`
sudo apt-key add ~/Repo.keys
sudo cp -R ~/sources.list* /etc/apt/
sudo apt-get update
sudo apt-get install dselect
sudo dpkg --set-selections < ~/Package.list
sudo dselect

Solution 2:

Who is this for: users that have normal regular use of their computer, that have done minimal or no configuration outside their home folder, did not mess up startup scripts and services. A user that wants to have his software restored to how it was when he installed it with all customizations being done and kept in their home folder.

Who this will not fit for: servers geeks, power users with software installed by source (restoring the package list might break your system), users that have changed the startup script of some application to fit better their needs. Caution: there is a big chance any modifications outside home will be over written.

Backup your current packages and user settings


Once you are ready with your system and happy with the software installed you can get a list of the installed packages using the command dpkg --get-selections and save the output in to a log file.

Create a backup folder in your home

sudo mkdir ~/backup

Save the currently installed packages list

dpkg --get-selections > ~/backup/installed_packages.log

Make a backup of your apt sources file

sudo cp /etc/apt/sources.list ~/backup/sources.bak

and a copy of your apt's list of trusted keys

sudo apt-key exportall > ~/backup/repositories.keys

Make a backup of your home folder with the integrated backup tool in Ubuntu, Deja-dup.

  • Set the backup folder

enter image description here

  • Verify that it will make a backup of your home folder and add any folders your might not want, like for instance the Trash folder

enter image description here

  • On the overview tab press Make Backup Now to backup your home folder

enter image description here

  • The backup tool will ask if you want to add a password to your backup, your choice. Your backup will start after you press Continue

enter image description here

When this is done you will have a backup of your packages and configuration files relative to your user. If when necessary you will be able to restore your files from the ~/backup folder.

Restoring your backup


Start by restoring the sources file from the backup made

sudo cp ~/backup/sources.bak /etc/apt/sources.list

The backed-up keys

sudo apt-key add ~/backup/repositories.keys

Update your sources lists

sudo apt-get update

Restore the packages from the saved installed_packages.log

sudo dpkg --clear-selections
sudo dpkg --set-selections < ~/backup/installed_packages.log && sudo apt-get dselect-upgrade

sudo dpkg --clear-selections will mark all current packages installed for removal, that way when you restore your saved package list the packages that are not on the list will be removed from your system.

Remove your current configuration from your home creating a backup of the folder in their current state (after all, whats the use of restoring fresh files if there are other there that can affect the configuration?)

mkdir ~/.old-gnome-config/ && mv ~/.gnome* ~/.old-gnome-config/ && mv ~/.gconf* ~/.old-gnome-config/ && mv ~/.metacity ~/.old-gnome-config/ && mv ~/.cache ~/.old-gnome-config/ && mv ~/.dbus ~/.old-gnome-config/ && mv ~/.dmrc ~/.old-gnome-config/ && mv ~/.mission-control ~/.old-gnome-config/ && mv ~/.thumbnails ~/.old-gnome-config/   && ~/.config/dconf/* ~/.old-gnome-config/

After this is done restore the backup created with Deja-dup.

  • Open the backup tool and on the overview tab press restore

enter image description here

  • Follow the prompts to restore the files to their original positions

enter image description here

After this is done you will have your packages back to the saved selection, our configuration restored and hopefully a working desktop, all of that without installing a single extra application and using the Ubuntu default tools.

The only thing left is to do some clean up and check that everything is working.

Something went wrong, my desktop is gone


There are a few posts in Ask Ubuntu that can guide you in case something when wrong and you need to hard reset your desktop. If something when wrong and you find your self needing to do so, please have a look at these posts:

  • How to reset Gnome to defaults?
  • How to reset Compiz/Unity configuration?

Solution 3:

Backups take some planning and there are several viable strategies. You will have to decide which method works best for you.

Be careful about "best way" , what works best for one person may not be best for another.

At the end of the day, the "best method" is one that has been tested and known to work. You need to test your backup strategy BEFORE you need it

Using images

One way is to simply copy and compress an image of your partitions. You can do this with several tools, anything from dd to partimage to clonezilla

partimage
clonezilla

The advantage of this strategy is that it is (relatively) easy and very complete. The disadvantage is that the back up images are large.

Smaller backups

You can make smaller backups by only backing up data and settings. There are several tools to do this, everything from dd to tar to rsync.

The key here is to know what you need to back up.

Advantage - Backups will be smaller.
Disadvantage - This is a manual method, so it is easy to forget about a critical file (/etc/passwd).

A list of what to back will always need to be reviewed to make sure the list is complete.

1) List of installed packages

Package list # Create a text list of an existing installation of all apt-get installed packages # in order to re-install on a newly installed distro

# make the list
[old distro] sudo dpkg --get-selections > packages

You would then perform a fresh install, and restore your packages. The following commands also update all the packages on your system (so restore and full update all at once).

# Now put them back on the new distro
[new distro] sudo dpkg --set-selections < packages

[new distro] sudo apt-get dselect-upgrade

2) Data. Generally this would be /home . Most user data and customization's are going to be in your users home directory. If you save data in other locations, include that (for example /media/data).

3) system settings . Here is where there is going to be some variation. Personally if I edit ANY system setting, I keep a copy of the original configuration file and my custom file in /root. So if I edit /etc/fstab for example, keep a copy in /root/etc/fstab and original in /root/etc/fstab.orig

You will also need /etc/passwd, /etc/shadow , /etc/group , /etc/sudoers, /etc/hostname, and /etc/hosts (you may need more in /etc, if I forgot something I will add it)

On a server you may need to include /var/www or other data directories.

I understand this takes some effort, so, it may be easier to include all of /etc.

4) A copy of your disk partition table.

sudo fdisk -l > fdisk.bak

You can use this information to restore your partition table if you replace your hard drive.

5) A copy of your MBR

sudo dd if=/dev/sda of=MBR.bak bs=512 count=1

You would then restore with

sudo dd if=MBR.bak of=/dev/sda bs=512 count=1

6) Other files/directories - Depending on your system and customization you may need to include additional directories. Considerations might include /opt , /usr/local, /usr/share`, and/or your .desktop files. Anything on your system you downloaded or customized outside of apt-get or software manager.

7) Put all that into an archive

sudo tar -cvpzf backup.tar.gz /home /root /etc ./MBR.bak ./fsdisk.bak ./packages

8) rsync

As an alternate to tar, you can use rsync.

See - https://help.ubuntu.com/community/rsync

Network backups

Another option is to use NFS or Samba to back up data.

Cron

You can automate backups by writing a backup script and running it (daily / hourly) with cron.

Restore

1) Using a live CD, restore your partitions using gparted or fdisk from the information in fdisk.bak
2) Install Ubuntu.
3) Boot your new install, update your packages.
4) Restore your data and settings

tar -xvpzf /home/test/backup.tar.gz -C / 

Then reboot

See also - https://help.ubuntu.com/community/BackupYourSystem/TAR

Other Strategies

There are many additional strategies for backup, some for backing up your home directory only, some graphical.

see: https://help.ubuntu.com/community/BackupYourSystem

Testing

It is not a backup unless it is tested. This is most important when you are NOT using an image of your root partition.

Test restoring your system in a VM, a spare computer, or a spare partition or hard drive.

Solution 4:

Installed Packages Selection using apt-clone

APT-Clone. This package can be used to clone/restore the packages on a apt based system. It will save/restore the packages, sources.list, keyring and automatic-installed states. It can also save/restore no longer downloadable packages using dpkg-repack.

APT-Clone is used by ubiquity (Ubuntu installer) for upgrade process.

  1. Install

    sudo apt-get install apt-clone
    
  2. Make backup

    sudo apt-clone clone path-to/apt-clone-state-ubuntu-$(lsb_release -sr)-$(date +%F).tar.gz
    
  3. Restore backup

    sudo apt-clone restore path-to/apt-clone-state-ubuntu.tar.gz
    

    Restore to newer release:

    sudo apt-clone restore-new-distro path-to/apt-clone-state-ubuntu.tar.gz $(lsb_release -sc)
    

Reference: man apt-clone


Home Data, Configuration /etc,.. using duplicity

Duplicity backs directories by producing encrypted tar-format volumes and uploading them to a remote or local file server. Because duplicity uses librsync, the incremental archives are space efficient and only record the parts of files that have changed since the last backup. Because duplicity uses GnuPG to encrypt and/or sign these archives, they will be safe from spying and/or modification by the server.

  1. Install

    sudo apt-get install duplicity
    
  2. Backup

    duplicity full path-to/source_folder/ file:///path_to/duplicity_backups/
    

    or incremental backup (It will backup only the difference from last backup):

    duplicity incremental path-to/source_folder/ file:///path_to/duplicity_backups/
    
  3. Restore

    duplicity restore file:///path_to/duplicity_backups/ path-to/target_folder/
    

Reference: man duplicity