HOWTO: Make and restore apt cache backups

本文提供了一种简便的方法来备份和恢复Debian或Ubuntu系统的软件包。通过创建脚本来复制APT缓存中的.deb文件,并使用dpkg工具扫描这些文件以便离线安装。此过程避免了重复下载相同的软件包。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I like to clean install a lot so I tend to burn my packages onto CDR. This spares me the trouble of having to download them over and over. If there's a simpler way to do this, I'd love to be informed.

Anyway, here's the way I currently do it:

1) Make a new folder somewhere in your Home folder.
2) Create this script file there called "apt-copy.sh":

Code:
#!/bin/sh
# Make a local copy of files from the apt cache
# except for files you list in "exclude.txt".

for FILE_NAME in `ls /var/cache/apt/archives/*.deb`
do {
  BASE_NAME=`basename $FILE_NAME`
  if ! grep -s $BASE_NAME exclude.txt >/dev/null 2>&1
  then {
    cp -v -u -p $FILE_NAME .
    echo $BASE_NAME >>exclude.txt
  }
  fi
}
done

# change %3a back to :
rename -v 's/%3a/:/' *%3a*

3) Make this script an executable (chmod +x apt-copy.sh).
4) Any time you do a major update, you can make a local copy of your files by running this script (./apt-copy.sh).
5) Remember to hang on to the "apt-copy.sh" and "exclude.txt" files if you move the files out of this folder.

Later on, when you want to restore all these packages, you simply do this:

1) sudo apt-get install dpkg-dev
2) Create a folder in your Home folder called "archive", then make a "binary" folder and "source" folder within the archive folder.
3) Put the binary packages you want to restore into the "binary" folder and the source packages you want to restore into the "source" folder.
4) Create this script file in the "archive" folder and call it "scan.sh":

Code:
#!/bin/sh
dpkg-scansources source /dev/null | gzip -9c > source/Sources.gz
dpkg-scanpackages binary /dev/null | gzip -9c > binary/Packages.gz

5) Make it executable (chmod +x scan.sh).
6) Once you have all the packages you want to use, run this script (./scan.sh).
7) To use the packages, add the following to your "/etc/apt/sources.list" at top of the file (sudo gedit /etc/apt/sources.list):

Code:
deb file:/home/htinn/archive binary/
deb-src file:/home/htinn/archive source/

Replace "htinn" with whatever user name you're using.

8) sudo apt-get update
9) Install your packages (using Synaptic or apt-get).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值