Commit | Line | Data |
---|---|---|
e6002cfa | 1 | #!/bin/bash |
bf301371 | 2 | |
3 | # | |
4 | # This script backs up every database to its own file, gzipped. | |
5 | # docelic@hcoop.net, Tue Aug 28 13:38:26 EDT 2007 | |
6 | # | |
7 | ||
109a1b34 CE |
8 | BACKUP_ROOT=/srv/backup/database |
9 | BACKUP_DAYS=7 | |
bf301371 | 10 | |
11 | if ! test -d "$BACKUP_ROOT"; then | |
12 | echo "Backup root directory '$BACKUP_ROOT' inaccessible." | |
13 | exit 1 | |
14 | fi | |
15 | ||
109a1b34 | 16 | MYSQL_DBS=`sudo -H mysql -Bse 'show databases' | xargs` |
bf301371 | 17 | echo "Mysql Databases are: $MYSQL_DBS" |
109a1b34 | 18 | PGSQL_DBS=`sudo -u postgres psql template1 -tAc 'SELECT datname FROM pg_database WHERE datistemplate = false;' | xargs` |
bf301371 | 19 | echo "Postgres Databases are: $PGSQL_DBS" |
20 | ||
21 | # Delete oldest | |
22 | rm -rf "$BACKUP_ROOT/mysql.$BACKUP_DAYS" | |
109a1b34 | 23 | rm -rf "$BACKUP_ROOT/postgres-9.$BACKUP_DAYS" |
bf301371 | 24 | |
109a1b34 | 25 | # Rotate backup dirs |
bf301371 | 26 | for day in `seq $BACKUP_DAYS -1 1`; do |
27 | to=".$day" | |
28 | let day_before=" $day - 1" | |
29 | if test "$day" = "1"; then | |
30 | from='' | |
31 | else | |
32 | from=".$day_before" | |
33 | fi | |
34 | ||
35 | mv -f "$BACKUP_ROOT/mysql$from" "$BACKUP_ROOT/mysql$to" 2>/dev/null | |
109a1b34 | 36 | mv -f "$BACKUP_ROOT/postgres-9$from" "$BACKUP_ROOT/postgres-9$to" 2>/dev/null |
bf301371 | 37 | done |
38 | ||
109a1b34 CE |
39 | # backups should not be world-readable, mask all o permissions |
40 | umask 0027 | |
41 | ||
bf301371 | 42 | # Create current dump dirs |
109a1b34 | 43 | mkdir --mode=770 "$BACKUP_ROOT/mysql" "$BACKUP_ROOT/postgres-9" |
bf301371 | 44 | |
45 | # Perform MYSQL backup | |
109a1b34 CE |
46 | for db in $MYSQL_DBS; do |
47 | sudo -H mysqldump --single-transaction "$db" | pigz - > "$BACKUP_ROOT/mysql/$db.gz" | |
bf301371 | 48 | done |
49 | ||
50 | # Perform PGSQL backup | |
109a1b34 CE |
51 | for db in $PGSQL_DBS; do |
52 | sudo -H -u postgres pg_dump -Fc "$db" > "$BACKUP_ROOT/postgres-9/$db.pg" | |
bf301371 | 53 | done |