backup.sh 2.12 KB
Newer Older
1 2
#!/bin/sh

3
COMPLETE=0
4
INCREMENTAL=0
5
DIR=
6
DATABASE=
7

8 9
usage() {
    echo "$0: [-c] [-i] dbname [path]
10 11

-c:   Backup everything
12
-i:   Incremental backup, since the first of the month
13 14
path: Where to save the file" >&2
    exit 1
15 16 17 18
}

if [ $# -ge 1 -a "$1" = "-h" ]; then
    usage
19 20
fi

21 22 23 24 25
if [ $# -ge 1 -a "$1" = "-i" ]; then
    INCREMENTAL=1
    shift
fi

26 27 28 29 30
if [ $# -ge 1 -a "$1" = "-c" ]; then
    COMPLETE=1
    shift
fi

31 32 33 34 35 36 37
if [ $# -eq 0 ]; then
    usage
fi

DATABASE="$1"
shift

38
if [ $# -ge 1 ]; then
39
    DIR="$1/"
40 41
fi

42
now=$(date +"%Y-%m-%d_%H:%M:%S")
43 44
FIRST_OF_THE_MONTH=$(date --date="$(date +%Y-%m-01)" +"%Y-%m-%d")

45 46
vidjil_path=web2py/applications/vidjil
db_backup_file=/tmp/db-backup-$now.csv
47
sql_backup_file=/tmp/db-backup-$now.sql
48

49 50 51 52 53 54 55 56 57 58
defs_py=$vidjil_path/modules/defs.py

if [ ! -f "$defs_py" ]; then
    echo "$defs_py doesn't exist. Is the Vidjil server fully installed?" >&2
    exit 2
fi

DIR_SEQUENCES=$(sed -rn "s/^DIR_SEQUENCES.*['\"](.*)['\"].*$/\1/p" $defs_py)
DIR_RESULTS=$(sed -rn "s/^DIR_RESULTS.*['\"](.*)['\"].*$/\1/p" $defs_py)

59
python web2py/web2py.py -S vidjil -M -R "applications/vidjil/scripts/backup-db.py" -A "$db_backup_file"
60
mysqldump  --no-create-info --complete-insert "$DATABASE" > $sql_backup_file
61

62
if [ $COMPLETE -eq 1 ]; then
63 64
        filename_raw="${DIR}backup_"$now
        filename=$filename_raw.zip
65
        zip -r $filename_raw web2py/applications/vidjil/databases/  "$DIR_SEQUENCES" "$DIR_RESULTS" $db_backup_file $sql_backup_file
66
else
67
    if [ $INCREMENTAL -eq 1 ]; then
68 69
        filename_raw="${DIR}backup_incremental_${FIRST_OF_THE_MONTH}__${now}.tar"
        filename=$filename_raw.gz
70
	tar cvf $filename_raw --force-local web2py/applications/vidjil/databases/ $db_backup_file $sql_backup_file
71 72
	tar rvf $filename_raw --force-local --after-date "$FIRST_OF_THE_MONTH" "$DIR_RESULTS" 2>&1 | grep -v "file is unchanged"
	gzip $filename_raw
73
    else
74 75
        filename_raw="${DIR}backup_essentials_"$now
        filename=$filename_raw.zip
76
        zip -r $filename_raw web2py/applications/vidjil/databases/  "$DIR_RESULTS" $db_backup_file $sql_backup_file
77
    fi
78
fi
79
rm -f "$sql_backup_file"
80
rm -f "$db_backup_file"
81
ls -lh $filename
82
echo $filename