grml_chroot: fix broken mount argument handling
[grml-scripts.git] / usr_sbin / dirvish-setup
1 #!/bin/sh
2 # Filename:      dirvish-setup
3 # Purpose:       create basic setup for dirvish(8)
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 ################################################################################
8 # Resources:
9 #   http://apt-get.dk/howto/backup/
10 #   http://edseek.com/~jasonb/articles/dirvish_backup/advanced.html
11 #   http://www.dirvish.org/svn/contrib/admin/DailyEmailScript/dirvish-status.sh
12 ################################################################################
13
14 set -e
15
16 # shellcheck disable=SC1091
17 . /etc/grml/script-functions
18 # shellcheck disable=SC1091
19 . /etc/grml/lsb-functions
20
21 check4root
22 check4progs dialog dirvish rsync ssh-keygen ssh-copy-id
23
24 PN="$0"
25 CONFFILE=/etc/dirvish/master.conf
26
27 set +e
28
29 write_conffile() {
30   if [ -f "$CONFFILE" ] ; then
31      MASTERINFO="Notice: $CONFFILE exists already so I will not touch it.
32 Please do not forget to add $CLIENT to the file if you wan to use the 'Runall' functionality."
33   else
34      MASTERINFO="An initial master configuration ($CONFFILE) has been generated.
35 Please adjust it according to your needs."
36      cat > $CONFFILE << EOF
37 # Master configuration file for dirvish, created by $PN on $(date)
38
39 bank:
40      $BACKUP_DIR
41
42 exclude:
43       lost+found/
44 #      core
45 #      *~
46 #      .nfs*
47 #      /var/lib/nfs/*tab
48 #      var/cache/apt/archives
49 #      var/cache/man
50 #      var/tmp
51 #      tmp
52 #      /dev
53 #      .kde/share/cache/*
54 #      .firefox/default/*/Cache/*
55
56 Runall:
57      $CLIENT 22:00
58      # add-another-client-here 22:00
59
60      # See http://www.dirvish.org/debian.howto.html for further details:
61 expire-default: +30 days
62 expire-rule:
63 #       MIN HR    DOM MON       DOW  STRFTIME_FMT
64         *   *     *   *         1    +3 months
65         *   *     1-7 *         1    +1 year
66         *   *     1-7 1,4,7,10  1
67         *   10-20 *   *         *    +4 days
68 #       *   *     *   *         2-7  +15 days
69
70 EOF
71   fi
72 }
73
74 # TODO / integrate?
75 cronsetup() {
76 # shellcheck disable=SC1090
77   test -f "${HOME}/.keychain/$(uname -n)-sh" && . "${HOME}/.keychain/$(uname -n)-sh"
78 }
79
80 get_backup_dir() {
81   # prompt user for directory which should be used
82   BACKUP_DIR="$(dialog --stdout --inputbox 'Please choose the directory where backups should be placed' 0 0 /backups)"
83
84   if ! [ -d "$BACKUP_DIR" ] ; then
85     dialog --stdout --title "${PN}" --yesno "The directory $BACKUP_DIR does not yet exist. Do you want me to create it for you? " 0 0
86     RC=$?
87
88     if [ ${RC} -eq 0 ]; then
89        echo "mkdir $BACKUP_DIR"
90        echo "chmod 700 $BACKUP_DIR"
91     else
92        echo "warning: $BACKUP_DIR does not exist, skipped creation as requested"
93     fi
94   fi
95 }
96
97 client_name() {
98   CLIENT="$(dialog --stdout --inputbox 'Please choose the name for your client instance, also known as vault' 0 0 client1)"
99 }
100
101 tree_name() {
102   TREE="$(dialog --stdout --inputbox 'Please choose the directory you want to backup from your client (also known as tree)' 0 0 /home)"
103 }
104
105 create_client_conf() {
106   mkdir -p "${BACKUP_DIR}/${CLIENT}/dirvish"
107   if [ -f "${BACKUP_DIR}/${CLIENT}/dirvish/default.conf" ] ; then
108      ewarn "Warning: ${BACKUP_DIR}/${CLIENT}/dirvish/default.conf exists already, ignoring creation." ; eend 0
109   else
110      cat > "${BACKUP_DIR}/${CLIENT}/dirvish/default.conf" << EOF
111 # Configuration file of client-side for dirvish created by $PN on $(date)
112 client: $CLIENTNAME
113 tree: $TREE
114 xdev: true
115 index: gzip
116 image-default: %Y-%m-%d
117 exclude:
118         var/cache/apt/archives/*
119         var/cache/man/*
120         tmp/*
121         var/tmp/*
122 rsh: ssh -i $HOME/.ssh/id_rsa_dirvish_${CLIENT}
123 EOF
124   fi
125 }
126
127 sshkey_setup() {
128   CLIENTNAME="$(dialog --stdout --inputbox 'Please choose user login and hostname for the client you want to backup. Syntax: user@host' 0 0 root@"$(hostname)")"
129
130   dialog --stdout --title "${PN}" --yesno "Do you want me to create ssh setup for client ${CLIENTNAME} using ssh-keygen and ssh-copy-id?" 0 0
131   RC=$?
132
133   if [ ${RC} -eq 0 ]; then
134      [ -d "$HOME/.ssh" ] || mkdir "$HOME/.ssh"
135      einfo "Creating $HOME/.ssh/id_rsa_dirvish_${CLIENT} using ssh-keygen:"
136      ssh-keygen -t rsa -f "$HOME/.ssh/id_rsa_dirvish_${CLIENT}" ; eend $?
137      einfo "Running ssh-copy-id to copy ssh key to $CLIENTNAME:"
138      ssh-copy-id -i "$HOME/.ssh/id_rsa_dirvish_${CLIENT}.pub" "$CLIENTNAME" ; eend $?
139   fi
140 }
141
142 client_setup() {
143   dialog --stdout --title "${PN}" --yesno "Do you want to backup $CLIENT via network? Answering with no will use localhost [$(hostname)] as client." 0 0
144   RC=$?
145
146   if [ ${RC} -eq 0 ]; then
147      sshkey_setup
148   else
149      CLIENTNAME=$(hostname) # use localhost only
150   fi
151 }
152
153 display_info() {
154   einfo "Running $PN was successful. Enjoy using dirvish!" ; eend 0
155   echo
156   einfo "Please adjust ${BACKUP_DIR}/${CLIENT}/dirvish/default.conf according to your needs.
157 $MASTERINFO
158 Then run the following command to create an initial backup:
159
160   dirvish --summary long --vault $CLIENT --init
161
162 Find the backup inside $BACKUP_DIR/$CLIENT/$(date +%Y-%m-%d)/tree/ then.
163
164 Documentation available at:
165
166     man dirvish-locate.1 dirvish.conf.5 dirvish.8 dirvish-runall.8 dirvish-expire.8
167     /usr/share/doc/dirvish/HOWTO.upstream /usr/share/doc/dirvish/FAQ.html
168     /usr/share/doc/dirvish/HOWTO.Debian.gz
169
170     http://www.dirvish.org/
171     http://wiki.dirvish.org/
172
173 Please report bugs regarding ${PN}: http://grml.org/bugs/
174 " ; eend 0
175 }
176
177 case "$1" in
178     -h | --help | --h* )
179       echo "Usage: $PN" 1>&2
180       exit 1
181      ;;
182 esac
183
184 # now run the functions:
185 get_backup_dir     && \
186 client_name        && \
187 tree_name          && \
188 client_setup       && \
189 create_client_conf && \
190 write_conffile     && \
191 display_info
192
193 ## END OF FILE #################################################################
194 # vim: ft=sh ai tw=80 expandtab