Oli Warner About Contact Oli on Twitter Subscribe

How to back up PPAs

Wednesday, 15 August 2012 bash ubuntu

Personal Packaging Archives are a popular method for sharing software not yet in the main repositories. You might be using one to gain a particular update you need. But how can you quickly get a list of all the PPAs you’re using?

Well it’s really very simple with this one line:

grep -RoPish "ppa.launchpad.net/[^/]+/[^/ ]+" /etc/apt |
sort -u | sed -r 's/\.[^/]+\//:/'

And that will pump out a nice list of ppa:.../... addresses.

It took several iterations to get that command down to something that short (it was much longer originally) but the legibility of it has suffered so I’m going to take some time to explain what it’s doing, in a hope to inspire and improve your Bash-Fu:

Search files in /etc/apt/
-R (recursively)
-o (only output what you match)
-P (using Perl regex syntax)
-i (ignoring case)
-s (suppress any error messages)
-h (don't output the filename before the matched output)
Sort that and weed out any duplicates (the -u means unique)
Then replace the first . to the first / with a :

If you want to back that up to a file, stick > ~/ppa-backup.txt on the end of the command and the output will be forced into that file.

Restoring from a backup

If you want to reapply your PPAs to another install of Ubuntu --be that a fresh installation or just another computer-- you can just run:

<~/ppa-backup.txt xargs -I % sudo add-apt-repository %

And this translates out as:

Pipe the content of ~/ppa-backup.txt to xargs
-I % (substitute any mention of % with the current output)
Run add-apt-repository as root, using the substitution from %

I hope you learned something new about Bash reading this article. I certainly did writing it. Perhaps you can improve on it! I was considering using gawk but that’s not installed by default.