Debian Codegraph @

src/mini-buildd-backup

raw
1 #!/bin/bash -e
2
3 DESCRIPTION="Create (or restore from) a mini-buildd backup archive."
4
5 _exit()
6 {
7 {
8 printf "E: %s\n\n" "${1}"
9 ${0} --help
10 } >&2
11 exit 1
12 }
13
14 _exit_no_root() { [ $(id -u) -eq 0 ] || _exit "Needs to be run as root."; }
15
16 case "${1}" in
17 "--help"|"-h")
18 cat <<EOF
19 Usage: $(basename "${0}") [--help|--restore <backup_tarball>]
20
21 Only run as user root.
22
23 ${DESCRIPTION}
24
25 EOF
26 exit 0
27 ;;
28 "")
29 _exit_no_root
30 MBD_BACKUP="$(pwd)/$(hostname)_$(date --rfc-3339=date)_$(dpkg-query --showformat '${Version}' --show mini-buildd).tar.xz"
31 [ ! -e "${MBD_BACKUP}" ] || _exit "Backup file already exists: ${MBD_BACKUP}"
32 MBD_HOME="$(getent passwd mini-buildd | cut -d: -f6)"
33 [ -n "${MBD_HOME}" ] || _exit "No mini-buildd home found."
34
35 read -e -i "Y" -p "Backup from '${MBD_HOME}' to '${MBD_BACKUP}' (Y/n)? " ANSWER
36 if [ "${ANSWER}" = "Y" ]; then
37 service mini-buildd stop
38 tar --create --file "${MBD_BACKUP}" "${MBD_HOME}" /etc/default/mini-buildd /etc/schroot/mini-buildd/ $(find "/etc/ssl/mini-buildd/" -mindepth 0 -maxdepth 0) $(find /etc/schroot/chroot.d/ -name "mini-buildd-*.conf")
39 fi
40 ;;
41 "--restore")
42 _exit_no_root
43 MBD_BACKUP="${2}"
44 [ -e "${MBD_BACKUP}" ] || _exit "No such backup archive: ${MBD_BACKUP}"
45
46 if getent passwd mini-buildd; then
47 _exit "mini-buildd user exist. Please restore only where mini-buildd is not installed yet (then install mini-buildd on top of that)."
48 fi
49
50 read -e -i "n" -p "Add mini-buildd user && restore '${MBD_BACKUP}' -- DANGER: will unpack in / and happily overwrite (Y/n)? " ANSWER
51 if [ "${ANSWER}" = "Y" ]; then
52 # Add mini-buildd user just the same way as postinstall, with debconf defaults
53 MBD_HOME="/var/lib/mini-buildd"
54 adduser --system --group --shell /bin/bash --home "${MBD_HOME}" --gecos "Custom Debian buildd" mini-buildd
55 tar --directory / --extract --file "${MBD_BACKUP}"
56 fi
57 ;;
58 *)
59 _exit "Wrong usage."
60 ;;
61 esac