Skip to content

Instantly share code, notes, and snippets.

@patrickjennings
Created January 8, 2017 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickjennings/8f26037ffa5fc42e0cbf54976cee9c81 to your computer and use it in GitHub Desktop.
Save patrickjennings/8f26037ffa5fc42e0cbf54976cee9c81 to your computer and use it in GitHub Desktop.
Backup utility for pacman
#!/bin/bash
# Creates a backup of modified files originally installed by pacman as well as
# a list of all installed packages through pacman.
# Usage: ./pacup.sh [OPTION] [ARCHIVE]
# Options:
# -t Only print the files which will be included in the backup. Can be used to
# build a blacklist file.
# Notes:
# Creates an archived backup with the following hierarchy:
# pkglist - List of installed core Arch linux packages.
# aurpkglist - List of installed AUR packages.
# package_backup - Directory of files which differ from what was installed by pacman.
# Can use a blacklist file with paths which should not be included in the
# backup. Paths should be absolute; one per line.
# Example blacklist:
# /etc/passwd
# /etc/group
set -e
archive_file='pacman-backup.tar.gz'
core_pkg_file='pkglist'
aur_pkg_file='aurpkglist'
blacklist_file='blacklist'
package_backup_dir='package_backup'
modified_files=$(pacman -Qii | awk '/^MODIFIED/ {print $2}')
if [[ -f "${blacklist_file}" ]]; then
modified_files=$(echo "${modified_files}" | fgrep -vxf blacklist)
fi
if [[ "$1" == '-t' ]]; then
echo "Files to backup:"
echo "${modified_files}"
exit
fi
if [[ -n "$1" ]]; then
archive_file="$1"
fi
tmp_directory=$(mktemp -d)
# Backup modified package files to temporary directory.
mkdir -p "${tmp_directory}/${package_backup_dir}"
echo "${modified_files}" | rsync -va --files-from=- / "${tmp_directory}/${package_backup_dir}/"
# Backup installed package names.
pacman -Qqen > "${tmp_directory}/${core_pkg_file}"
pacman -Qmq > "${tmp_directory}/${aur_pkg_file}"
tar cvpaf "${archive_file}" -C "${tmp_directory}/" "${core_pkg_file}" "${aur_pkg_file}" "${package_backup_dir}"
rm -r "${tmp_directory}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment