msgbartop
Ubuntu, Linux Mint And Computer Repair Business Questions Answered Here – Just Ask!
msgbarbottom
03 Sep 13

How To Backup Your Ubuntu Software

Today Kyle writes:

 Dear Matt,

I have been using Ubuntu for a couple years now, and I am pretty comfortable with day-to-day computing on the platform. However when it comes time to do a version upgrade with a fresh install (I don’t trust the upgrader, it doesn’t work properly in my experience) I’m often frustrated by having to install a whole bunch of software, grab a bunch of PPAs, and deal with a bunch of broken settings. I’ve been using DejaVu to backup and transfer my home folder, but I’m wondering what is the “right” way to do a version upgrade?

Well Kyle, it sounds like you’re half way there. You already understand the value of backing up your home directory.  And this is a good thing, as it’s easier to piece together lost software than it is lost personal documents, pictures, etc. All of this said, I am going to show you a bullet proof method for backing up your software. This method works for both migrating software, as well as simply doing a fresh installation on the same system. Be sure to follow the steps carefully.

Backup your home directory

Using Déjà Dup to back up your home directory first needs to be your first order of business. Why? Because your home directory has your software settings, in addition to your pictures, documents, videos, and various other Ubuntu-specific account information. Restoring it, will make your life easier as it contains irreplaceable data. This article does not cover backing up this data — it only creates a special backup of your software titles, not the data within them. Moving on.

How To Backup Your Ubuntu SoftwareBacking up your Ubuntu Software

Backing up your software is much easier that most tutorials out there might have you believe. Most of them, rely on the dpkg –set-selections method which in my opinion, is a nightmare waiting to happen. Sure, it should work without any hassles. Unfortunately though, this isn’t always the case. I’ve found that more often than not, it creates conflicts. Worse than that, you really don’t get to sort out what you’re going to backup and what you’d rather just avoid. It’s messy and to be avoided in my opinion. Using Synaptic however, offers a simple, GUI solution that anyone of any skill level can follow.

There are line breakages on purpose, due to formatting — so copy carefully.

1) Backup your PPAs and your PPA keys. To do this, simply run the following commands in a terminal window.

sudo cp -r /etc/apt/sources.list.d /Like-a-FlashDrive

The above command, is what will pull your PPAs from your apt directory, make a copy of them and drop them into the directory of your choosing. I recommend something like Dropbox or a USB flash drive. Anything that isn’t attached only to your hard drive, is a safe bet. No need to add sources.list.d to the destination, since it’s merely a copy of the existing PPA area.

2) Backup your PPA keys! This is what will allow you to connect to the PPA servers and add/remove software from them. Without these keys, you’re going to be stuck rebuilding the keys from a terminal and if you have a lot of them, this is time-consuming.

sudo apt-key exportall > /Like-a-FlashDrive/repositories.key

This command will make sure that all the relevant gpg keys for your PPA repositories, will be backed up as well. From there, the next step in the backup process is simply to run Synaptic and do the following from within the GUI.

File>Save Markings As>Click “Save full state, not only changes”

Checkbox>choose Destination-like-a-FlashDrive>Click Save

Be sure to name the backup something you recognize, like “SoftwareBackup.”

Restoring your Ubuntu software

On your new computer or perhaps the same PC, with a clean installation of Ubuntu, follow these instructions to restore your Ubuntu software settings. Realize though, that these settings are pretty deep. So it pays to watch the video below to make sure you don’t miss anything. Okay, to restore your software. Once you’ve moved your “sources.list.d” folder, “repositories.key” file and “SoftwareBackup” file to your new home directory, you’re ready to begin the software restoration. If this is being done on a backed up home directory, simply restore your home directory and continue below instead.

1) Restore your PPA repos. If you’ve got everything in your home directory as instructed above, go ahead and do the following.

sudo cp -r ~/sources.list.d /etc/apt/

This copies your backed up sources.list.d folder from home to your apt folder.

2) Next, we need to authenticate your PPAs. They need their keys back!

sudo apt-key add /home/matt/repositories.key

Awesome, onto the last step!

3) Restore the software titles themselves. Remember, your software settings are stored in your home directory. So this restoration only restores the installation of the software, not their settings. Restoring a backup of your home directory will get the settings back into place. Open up Synaptic on the new install or new PC, and follow this last step.

File>Read Markings>Browse to home dirctory>Select SoftwareBackup file>Open>Review software selections>click Apply

The video below will show you how easy the restore process is.

Do you have questions about PC repair, Linux on the desktop, software or other tech related subjects? Don’t get frustrated, Just Ask Matt! Email me directly for help and perhaps I will be able to answer your questions right here at Matt Hartley Dot Com!

Reader's Comments

  1.    

    This is really brilliant. Having all the PPA and their keys restored to the new system. However I am have some questions regarding the software backup (not questioning your methods or anything, just not sure). When I see the video, you go through the list of software which will be removed and added. Now, there would it not install all backend stuff which might not be necessary in the new version of ubuntu?

    Sure, I would like it to backup the stuff that ONLY the user installed. Not all the software in the system currently.

    •    

      In the video, I was going from Xubuntu 12.04 machine to Ubuntu 12.10. That is why some stuff was setup to automatically be removed. Basically, it was to match the software settings found on the old machine. Also, I had installed alternative stuff on the new machine already, so it was going to uninstall that to match what I had on the old machine.

      *An Ubuntu 12.04 to 12.10 example:*

      Assuming you have a fresh installation, you have your home directory restored already, as it has your settings and personal files, you’re ready to move your software.

      The software backed up includes stuff like Compiz Fusion, software from PPAs, etc. What will be different on the new system however, are the version numbers. Why? Because we wisely avoided coping the standard repositories. Since there are huge differences between 12.04 to 12.10, you would want to make sure that you’re only restoring the software titles — not their older versions.

      Now, obviously, you may not want it to install EVERYTHING. Perhaps you’d just as soon avoid having it install the Compiz or perhaps avoiding something random like Filezilla. There are two ways of doing this.

      1) Edit it out in the Synaptic dialog before the restoration begins. It’s a pain, but it’s safe.

      2) Edit the restoration text file in Gedit. It’s more difficult, but you can edit stuff out.

      I recommend sticking with option #1 myself. :)

  2.    

    Matt, great article.
    How different is to save the applications list with dpkg using:
    $sudo dpkg –get-selections > /some_new_directory/app_bakup

    and then restore in the new machine with:

    $sudo dpkg –get selections < /some_new_directory/app_bakup
    sudo apt-get -y update
    sudo apt-get dselect-upgrade

    Do you have any comments?

    Thanks

    •    

      Thanks Tito.

      I touched on this above, in that I am not a fan of the dselect-upgrade method. Not because it’s bad. In reality, when it’s not throwing up dpkg errors, it’s a nice option. But because it’s does error easily, I opted to avoid including it here.

      As for the end results, honestly, it does much the same thing except that you will likely end up restoring more than you wanted. With my method, you can visually see what is about to go down before approving it. So I can take note of what is about to be installed, in a human readable way, then uncheck them in Synaptic before completing the restoration.

      My feeling comes down to this — if someone knows what the dselect-upgrade approach is, they are able to decide what works best for them. For newbies, it’s not likely to be a smooth in the end.

  3.    

    ***(Edit by Matt Hartley — as always, run scripts at your own risk — Matt assumes zero responsibility for the results)***

    OK. Here’s a script to run. You can add it to your crontab (example below). I do this so I can copy my entire home directory to another machine, then load the software from the backup. Works. great. Every morning at 1am the crontab does a backup. If you have a laptop, use the second line for backup on startup.

    0 1 * * * /home/me/bin/backup_ubuntu_software.sh /home/me/.backups >/dev/null 2>&1
    %reboot /home/me/bin/backup_ubuntu_software.sh /home/me/.backups >/dev/null 2>&1

    NOTE: Don’t forget to: chmod a+x backup_ubuntu_software.sh

    #!/bin/bash
    #
    # license LGPL
    # copyright: 2012 Richard A Bray
    # email: oscbrayra.com
    #
    PROG=”`basename $0`”
    command=”backup”
    HOST=`hostname`

    usage()
    {
    echo ‘usage: $PROG [ -H hostname ] [-r] path’
    echo ”
    echo ‘ This command will backup/restore your Ubuntu software settings’
    echo ‘ path : backup directory path [ EX: /media/flashdrive ]‘
    echo ‘ -r : restore settings’
    echo ‘ -H name : use “name” has nostname [ default: `hostname` ]‘
    echo ‘ -h : display this help’
    echo ”
    }
    while getopts “:rHh” opt; do
    case $opt in
    r ) command=”restore”
    ;;
    H ) HOST=”$OPTARG”
    ;;
    h ) usage
    exit 1
    ;;
    ? ) usage
    exit 1
    esac
    done
    shift $(($OPTIND -1))
    if [ $# -eq 0 ] ; then
    usage
    exit 1
    fi
    DESTINATION=”$1″
    if ! [ -d "$DESTINATION" ] ; then
    echo “ERROR: backup directory ($DESTINATION) not found!” >&2
    exit 2
    fi

    BACKUP_DIR=”$DESTINATION/$HOST”
    case $command in
    backup )
    #add machine to machine list
    mkdir “$BACKUP_DIR” 2>/dev/null
    if [ $? -ne 0 ] ; then
    echo “ERROR: cannot create backup directory: $BACKUP_DIR” >&2
    exit 6
    fi
    cp -r /etc/apt/sources.list.d “$BACKUP_DIR/sources.list.d”
    apt-key exportall > “$BACKUP_DIR/repositories.key”
    dpkg –get-selections | sort >”$BACKUP_DIR/selections.txt”
    ;;
    restore )
    if [ -d "$BACKUP_DIR/selections.txt" ] ; then
    sudo cp -r “$BACKUP_DIR/sources.list.d” /etc/apt
    sudo apt-key add “$BACKUP_DIR/repositories.key”
    sudo dpkg –set-selections &2
    exit 3
    fi
    ;;
    * )
    echo “Unknown command” >&2
    exit 4
    esac