Skip to content

Instantly share code, notes, and snippets.

@xacrimon
Last active April 24, 2024 22:22
Show Gist options
  • Save xacrimon/976fe1fda7180ba3e6cd823c8005a74d to your computer and use it in GitHub Desktop.
Save xacrimon/976fe1fda7180ba3e6cd823c8005a74d to your computer and use it in GitHub Desktop.
Backup script using tar and xz for arch linux
#!/bin/sh
if [[ $(whoami) != "root" ]]
then echo "Please run as root"
exit 1
fi
if [ "$(ls /bin | grep trizen)" == "trizen" ]
then
PKGMAN=trizen
else
PKGMAN=pacman
fi
clear
echo "Enter the name of the directory where the backup will be stored with no trailing slash, preferably on a seperate drive"
read BDIR
DATE=$(date +%Y-%m-%d)
clear
echo "Starting backup..."
sleep 2
mkdir "${BDIR}/.temp"
mkdir "${BDIR}/.temp/pkglist"
TEMPDIR="${BDIR}/.temp"
${PKGMAN} -Q > "${TEMPDIR}/pkglist/packagelist-all"
${PKGMAN} -Qe > "${TEMPDIR}/pkglist/packagelist-explicit"
if [ "$1" == "--pac" ]
then
bfile="${BDIR}/pacbackup-${DATE}.tar.xz"
tar --xattrs -cpvf - "/var/lib/pacman/local" "${TEMPDIR}/pkglist" | xz -z9k --memory=70% --stdout > ${bfile}
echo "If no errors were shown, a new backup has been created at ${BDIR}/pacbackup-${DATE}.tar.xz"
else
bfile="${BDIR}/backup-${DATE}.tar.xz"
tar --xattrs -cpvf - "/home" "/var" "/etc" "/boot" "${TEMPDIR}/pkglist" | xz -z9k --memory=70% --stdout > ${bfile}
echo "If no errors were shown, a new backup has been created at ${BDIR}/backup-${DATE}.tar.xz"
fi
rm -r "${TEMPDIR}"
read
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment