- Published on
Restore Packages Using dselect-upgrade
- Authors

- Name
- Kevin van Zonneveld
- @kvz
It's always a good idea to backup important data. Your files and settings can easily be archived. But how can you backup & restore all applications that you've installed over the last couple of years? Here's an easy trick that works for both desktops & servers, and that can also be used to synchronize installed packages in a web cluster, making all the servers run the same software.
The method described in this article depends on the command apt-get, so it works on Debian & Ubuntu systems. This article does not describe a full backup & restore method, it's a trick to add to your existing backup procedure. Still, it's a trick that will really make your life easier.
APT Packages
The basic idea is that we generate a list of all currently installed packages, keep it some place safe, and upon a reinstall, we can upload this list again and have the system install all the packages in this list automatically.
How to Backup
So first we need to create a list of all the installed APT packages and save it in a file:
$ sudo dpkg --get-selections > /tmp/dpkglist.txt
That's it! The list is now stored in /tmp/dpkglist.txt. If you want you can
add this command to your crontab and then just include the file
/tmp/dpkglist.txt in your backup procedure so that it's safe and up to date
at all times.
How to Restore
Now if your system crashes (let's all hope it won't) and you need to reinstall, this will be the procedure:
- install a fresh OS (of course)
- restore the package list
- restore your important files & settings
But how can we restore the package list? Simple. Just copy your backed up
dpkglist.txt file to your fresh system's /tmp directory again and execute
the following:
$ sudo dpkg --set-selections < /tmp/dpkglist.txt
$ sudo apt-get -y update
$ sudo apt-get dselect-upgrade
Great! All of your apt packages have been restored!
(Don't worry! This method only adds and upgrades packages, it will not remove packages that do not exist in the list)
Additional Trick: PEAR Packages (Web Servers Only)
The same method can be used to restore PEAR extensions. Though there aren't any standard tools that I know of, with a little creativity it's not so hard.
How to Backup
This will generate a list of all installed PEAR packages and save it to a file:
$ sudo pear -q list | egrep 'alpha|beta|stable' |awk '{print $1}' > /tmp/pearlist.txt
That's it! A list of your installed PEAR packages is stored in the file
/tmp/pearlist.txt. Now, if you want you can add this command to your
crontab and then just include the file /tmp/pearlist.txt in your backup
procedure so that it's safe and up to date at all times.
How to Restore
To restore: make sure PEAR is installed, simply copy the pearlist.txt file
back to your new system's /tmp directory and type:
cat /tmp/pearlist.txt |awk '{print "pear install -f "$0}' |sudo bash
Great! All of your PEAR packages have been restored!
Legacy Comments (13)
These comments were imported from the previous blog system (Disqus).
cat abuse!
better:
sudo dpkg --set-selections < /tmp/dpkglist.txt
I wouldn\'t go so far as to call it abuse, but I\'ve updated the article with your tip, thanks.
Be nice to email that file to an external email address, I understand cron can do so easily?
Where is the default destination for mail from cron?
Hi Thomas,
If you want the output of all your cronjobs in your mail, begin your crontab with:
MAILTO=\"thomas@yourdomain.com\"
and remove the \'> /tmp/dpkglist.txt\' part from the jobs.
If you only want this cronjob\'s output in your mail, make sure the following package is installed: aptitude -y update && aptitude install mailx
And then change the cronjob like this:
dpkg --get-selections |mail thomas@yourdomain -s \"My APT packagelist $(date)\"
Same goes for PEAR
@ Thomas, again: BTW, if you want to know more about crontab, you might also want to read my article \'Schedule Tasks on Linux using crontabs\' here: http://kevin.vanzonneveld.n...
Ah, I see you\'ve already discussed crontab\'s mail output, natch. Thanks for the kind pointer, and all the great howtos.
Great article - just one comment.
I\'d put the backup in a more permanent place like /var/tmp - or a file in /etc - simply as /tmp will get wiped on each and every reboot.
just my 0.02c
@ Angus MacGyver: You\'re supposed to run the command before your actual backup, and have the backup include that file, so no problems there.
Great tip kevin.
I learned a new thing. :)
continue the good job.
this is simply great!!!!
dpkg is THE package manager.
thanks for sharing this. it\'s helping me with all my friend\'s ubuntu installations. it saves me a LOT of time.
i just keep one ubuntu on my brother\'s laptop and use this method.
thanks
--
cga
I LIKE LIKE LIKE LIKE this bit of info. This one as it has been a lifesaver to me, I just found this nugget.
Thanks keep good info up.
David
From Millry Al
very good trick to save nerves.
one more thing:
cp /etc/apt/sources.list tmp/sources.list
to be on the save site, if you have any repositories added.
Thank you very much!
Works smoothly and miraculous magical from Ubuntu 10.04 to Pear OS 3.0.
Great contribution to Linux lovers which is making life very comfortable.
Carpe Diem!
Best Regards!