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