dirvish-setup: support setting of tree on the client
[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 Mär 09 19:45:57 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 tree_name() {
95   TREE="$(dialog --stdout --inputbox 'Please choose the directory you want to backup from your client (also known as tree)' 0 0 /home)"
96 }
97
98 create_client_conf() {
99   mkdir -p "${BACKUP_DIR}/${CLIENT}/dirvish"
100   if [ -f "${BACKUP_DIR}/${CLIENT}/dirvish/default.conf" ] ; then
101      ewarn "Warning: ${BACKUP_DIR}/${CLIENT}/dirvish/default.conf exists already, ignoring creation." ; eend 0
102   else
103      cat > "${BACKUP_DIR}/${CLIENT}/dirvish/default.conf" << EOF
104 # Configuration file of client-side for dirvish created by $PN on $(date)
105 client: $CLIENTNAME
106 tree: $TREE
107 xdev: true
108 index: gzip
109 image-default: %Y-%m-%d
110 exclude:
111         var/cache/apt/archives/*
112         var/cache/man/*
113         tmp/*
114         var/tmp/*
115 rsh: ssh -i $HOME/.ssh/id_rsa_dirvish_${CLIENT}
116 EOF
117   fi
118 }
119
120 sshkey_setup() {
121   CLIENTNAME="$(dialog --stdout --inputbox 'Please choose user login and hostname for the client you want to backup. Syntax: user@host' 0 0 root@$(hostname))"
122
123   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
124
125   if [ $? -eq 0 ]; then
126      [ -d "$HOME/.ssh" ] || mkdir "$HOME/.ssh"
127      einfo "Creating $HOME/.ssh/id_rsa_dirvish_${CLIENT} using ssh-keygen:"
128      ssh-keygen -t rsa -f "$HOME/.ssh/id_rsa_dirvish_${CLIENT}" ; eend $?
129      einfo "Running ssh-copy-id to copy ssh key to $CLIENTNAME:"
130      ssh-copy-id -i "$HOME/.ssh/id_rsa_dirvish_${CLIENT}.pub" $CLIENTNAME ; eend $?
131   fi
132 }
133
134 client_setup() {
135   dialog --stdout --title "${PN}" --yesno "Do you want to backup $CLIENT via network? Answering with no will use localhost [$(hostname)] as client." 0 0
136
137   if [ $? -eq 0 ]; then
138      sshkey_setup
139   else
140      CLIENTNAME=$(hostname) # use localhost only
141   fi
142 }
143
144 display_info() {
145   einfo "Running $PN was successful. Enjoy using dirvish!" ; eend 0
146   echo
147   einfo "Please adjust ${BACKUP_DIR}/${CLIENT}/dirvish/default.conf according to your needs.
148 Then run the following command to create an initial backup:
149
150   dirvish --summary long --vault $CLIENT --init
151
152 Find the backup inside $BACKUP_DIR/$CLIENT/$(date +%Y-%m-%d)/tree/ then.
153
154 Documentation available at:
155
156     man dirvish-locate.1 dirvish.conf.5 dirvish.8 dirvish-runall.8 dirvish-expire.8
157     /usr/share/doc/dirvish/HOWTO.upstream /usr/share/doc/dirvish/FAQ.html
158     /usr/share/doc/dirvish/HOWTO.Debian.gz
159
160     http://www.dirvish.org/
161     http://wiki.dirvish.org/
162
163 Please report bugs regarding ${PN}: http://grml.org/bugs/
164 " ; eend 0
165 }
166
167 case "$1" in
168     -h | --help | --h* )
169       echo "Usage: $PN" 1>&2
170       exit 1
171      ;;
172 esac
173
174 # now run the funtions:
175 get_backup_dir     && \
176 client_name        && \
177 client_setup       && \
178 tree_name          && \
179 create_client_conf && \
180 write_conffile     && \
181 display_info
182
183 ## END OF FILE #################################################################
184 # vim: ft=sh ai tw=80 expandtab