Skip to content

Instantly share code, notes, and snippets.

@schtobia
Last active May 3, 2021
Embed
What would you like to do?
Backup a primary user without referring to a username
#! /bin/bash
DATE="$(date -u '+%Y%m%dT%H%M%SZ')"
IFS=':' read -r -a PASSWD_ENTRY <<<"$(getent passwd 1000)"
IFS=':' read -r -a GROUP_ENTRY <<< "$(getent group 1000)"
USERNAME=${PASSWD_ENTRY[0]}
HOME=${PASSWD_ENTRY[5]}
GROUP=${GROUP_ENTRY[0]}
# shellcheck disable=SC1090
[ -r "$HOME"/.config/user-dirs.dirs ] && . "$HOME"/.config/user-dirs.dirs
TARGET_DIR=${XDG_DESKTOP_DIR:-$HOME}
tar cvp --exclude-tag-under=.backup-ignore /etc /home /usr/local | \
gzip --best --verbose --no-name > "${TARGET_DIR}/backup-${DATE}.tgz" && \
chown --verbose "${USERNAME}":"${GROUP}" "${TARGET_DIR}/backup-${DATE}.tgz"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment