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