1. Home
  2. Docs
  3. Pacman
  4. How to backup Pacman database automatically

How to backup Pacman database automatically

by Joekamprad

corrupted pacman database ?

I can see this kinda often is happening to users regularly often here… caused by powering off in between updating the system… or powerloss…

Backup automatic with systemd:

Automated Backup In order to keep the backup up-to-date, automation of the aforementioned backup command makes sense.

This can be realized with the tool pakbak-git [AUR]
Pakbak is a bash script that is triggered by a Systemd service once the contents of the /var/lib/pacman/local directory have been changed.

After installing Pakbak, the destination and the number of backups to be kept in rotation can be set.

sudo nano /etc/pakbak.conf

# Backup the database to this folder
target_folder = /var/local/pakbak
# Define how long backups should be kept
keep_days = 5
# Define how many backups should be kept
keep_number = 5


The target directory must be created or already exist.
Environment variables in the path, such as $HOME will not be processed.

test if it is working

To test whether the bash script works as expected, you can manually initiate the execution with root privileges:

sudo sh /usr/lib/systemd/scripts/pakbak

Then enable the Systemd service, which is the directory /var/lib/pacman/local automatically monitored for changes:

sudo systemctl enable pakbak.path


Now you will have created regularly: pakbak-year-month-day-220137.tar.xz archives in the path you specified inside config file (target_folder = /var/local/pakbak)

You can easely unpack and replace to /var/lib/pacman/local in case of pamac databse corruption.

Follow us
Was this article helpful to you? Yes 6 No 1

How can we help?