Removing here() antipattern, fixing virtualenv deployment, and ensuring prereqs are...
[oweals/karmaworld.git] / bin / bupdb.sh
1 #!/bin/bash
2
3 # Bup-DB (alpha v0.10a)
4 # Copyright (C) 2012  FinalsClub Foundation
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License as published by
8 #    the Free Software Foundation, either version 3 of the License, or
9 #    (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU General Public License for more details.
15 #
16 #    You should have received a copy of the GNU General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 DATE=`date +%Y-%m-%d`
20
21 # Create a database dump based upon input.
22 function dump_db() {
23         
24         pg_dumpall > $1
25 }
26
27 # Generate MD5 sum of file
28 function create_hash() {
29
30         md5sum $1 > $1.md5
31 }
32
33 # Verify if file hashes match for verification.
34 function verify_hash() {
35
36         if [ "`cat $1.md5`" == "`md5sum $1`" ]; then
37                 echo "Pass!"
38         else
39                 echo "Fail : Backup has not been completed!"
40                 # Add measures to ensure that backup is removed and re-attempted.
41         fi
42 }
43
44 # Make a commit to bup
45 function bup_commit() {
46
47         bup index -vux $BUP_DIR
48         bup save -vn $USE_CASE $BUP_DIR
49 }
50
51 # Set up our local bup using the backup user.
52 function init_run() {
53
54         # Create Our Backup Environment on the Server
55         mkdir $BUP_DIR
56         cp -r $WEB_ROOT $BUP_DIR
57         chown -R backup:backup $BUP_DIR
58         bup init
59         bup_commit      
60 }
61
62 # Used if pulling backups from remote server
63 function bup_remote_backup(){
64
65         echo "Pulling backup from : $BACKUP_SRV"        
66         bup on $BACKUP_SRV index -u $BUP_DIR
67         bup on $BACKUP_SRV save -n $USE_CASE $BUP_DIR
68
69 }
70
71 # General Database backups
72 function backup() {
73         
74         rsync -r $WEB_ROOT $BUP_DIR/uploads
75         pg_dumpall > $BUP_DIR/$USE_CASE.sql
76         create_hash $BUP_DIR/$USE_CASE.sql
77         bup_commit      
78
79 }
80
81 #Push snapshot to the S3 bucket
82 function s3_push() {
83                 tar fcjv /tmp/$USE_CASE-$DATE.tar.bz2 ~/.bup
84                 s3cmd put /tmp/$USE_CASE-$DATE.tar.bz2 s3://$BUCKET
85 }
86
87 # Removes old BUP snapshot, still a WIP.
88 function old_bkup_rm() {
89         s3cmd la s3://$BUCKET | cut -d / -f4 | cut -d - -f4 | cut -d . -f1
90
91 }
92
93 # Display a basic help page / script info.
94 function usage() {
95         echo "You seem to be lost. This is a pre-alpha of bup-db. Next release"
96         echo "Will be a Python patch for bup"
97         echo "---------------------------------------------------------------"
98         echo " bupdb.sh [-Flags ] "
99         echo "---------------------------------------------------------------"
100         echo " -b bucket_name"
101         echo " -h user@remote_host"
102         echo " -n app_name "
103         echo " -r repo_dir"
104         echo " -s hostname "
105         echo 
106         exit
107 }
108
109 function env_check() {
110     
111 }
112 while getopts b:h:n:r:w: option
113 do
114         case "${option}"
115         in
116                 b) BUCKET=${OPTARG};;
117                 h) BACKUP_SRV=${OPTARG};;
118                 r) BUP_DIR=${OPTARG};;
119                 n) USE_CASE=${OPTARG};;
120                 s) WEB_ROOT=${OPTARG};;
121                 *) usage;;
122         esac
123 done
124
125 backup
126 s3_push
127
128
129