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