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
It seems that dpkg
doesn't show explicitly any information about installation dates of packages.
So, for a single run I would use something like the following one-liner:
cat /var/log/apt/history.log |perl -lne 'next unless /^Start-Date: ([^ ]+)/ && $1 ge "2015-04-26" .. 1; next unless /^(Install|Purge|Remove): (.*)$/; $c = $1; $s = $2; $s =~ s/\([^\)]*\)//g; @packages = map { /^(.*?):/ ? $1 : $_} split /\s*,\s*/, $s; if ($c=~/^Install$/){ $h{$_} = 1 for @packages;} if ($c=~/^(Purge|Remove)$/) {delete $h{$_} for @packages;} END{print for sort keys %h;}'
Start date (x_0) has been hard-coded in the command ("2015-04-26"
).
For time to time usage more suitable would be a stand-alone script, like this installed_packages.pl
:
#!/usr/bin/perl
# use as follows:
# ./installed_packages.pl '2015-04-26' /var/log/apt/history.log
# or
# ./installed_packages.pl '2015-04-26 16:08:36' /var/log/apt/history.log
use strict;
use warnings;
# the script expects start date as first argument
my $START_DATE = shift @ARGV;
# hash (dict) to accumulate installed & not removed packages
my %installed;
# flag to mark the beginning of "interesting" period of time
my $start = 0;
# loop over lines from the input file
while (my $line = <>){
# remove end-of-line character
chomp $line;
# skip everything until date becomes greater (or equal) than our x_0
$start ||= $line =~ /^Start-Date: ([^ ]+)/ && $1 ge $START_DATE;
next unless $start;
# we're only interested in lines like
# Install: tupi-data:amd64 (0.2+git02-3build1, automatic), tupi:amd64 (0.2+git02-3build1), libquazip0:amd64 (0.6.2-0ubuntu1, automatic)
# or
# Remove: aptitude-common:amd64 (0.6.8.2-1ubuntu4)
# + separate action (install/remove/purge) from package list
next unless $line =~ /^(Install|Purge|Remove): (.*)$/;
my ($action, $packages_str) = ($1, $2);
# remove versions from the list (they're in parentheses)
$packages_str =~ s/\(.*?\)//g;
# split single line into array of package names
my @packages = split /\s*,\s*/, $packages_str;
# remove architecture name (comes after ':')
s/:.*// for @packages;
# if packages have been installed, add them all to the hash (dict) of installed packages
if ($action =~ /^Install$/ ){
$installed{$_} = 1 for @packages;
}
# if packages have been removed, remove them all from the hash (dict) of installed packages
if ($action =~ /^(Purge|Remove)$/) {
delete $installed{$_} for @packages;
}
}
# print all installed and not removed packages, in alphabetical order
for my $p ( sort keys %installed ){
print "$p\n";
}
Usage:
./installed_packages.pl '2015-04-26' /var/log/apt/history.log
or
perl ./installed_packages.pl '2015-04-26' /var/log/apt/history.log
For frequent interactive use I'd add validation of script arguments (format of start date), implement -h
switch to display short help, and possibly convert start date to named switch (--start
).
Good luck!
Best Answer
either
or
or
will list all the packages.
Just re-route the output to a file. The 1st two lines will list it from smallest to largest with the size in front of the package name. The 3rd is in alphabetical order.