forked from bcgov/embc-ess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
50 lines (38 loc) · 1.3 KB
/
backup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/bash
# mssql automated backup script
# See README.md for documentation on this script
FINAL_BACKUP_DIR=$BACKUP_DIR"`date +\%Y-\%m-\%d`/"
TARGET_SQL="DATABASE"
if [ "$BACKUP_LOG_ONLY" == "true" ]; then
echo "LOG only mode"
TARGET_SQL="LOG"
DBFILE=$FINAL_BACKUP_DIR"$MSSQL_DATABASE-LOG-`date +\%Y-\%m-\%d-%H-%M`.bak"
else
echo "FULL BACKUP mode"
DBFILE=$FINAL_BACKUP_DIR"$MSSQL_DATABASE-FULL-DATABASE`date +\%Y-\%m-\%d-%H-%M`.bak"
fi;
echo "Making backup directory in $FINAL_BACKUP_DIR"
if ! mkdir -p $FINAL_BACKUP_DIR; then
echo "Cannot create backup directory in $FINAL_BACKUP_DIR." 1>&2
exit 1;
fi;
DB_ADMIN_PASSWORD=$DB_ADMIN_PASSWORD
export DB_ADMIN_PASSWORD
echo "Backing up $MSSQL_DATABASE on $DATABASE_SERVICE_URI"
sqlcmd -S $DATABASE_SERVICE_URI -U sa -P $DB_ADMIN_PASSWORD -d master -Q "BACKUP $TARGET_SQL $MSSQL_DATABASE TO DISK='$DBFILE'"
if test -f "$DBFILE" ; then
echo "Database backup written to $DBFILE"
echo "Will now gzip $DBFILE"
gzip $DBFILE
echo "Database backup written compressed to $DBFILE.gz"
if test -f "$DBFILE"; then
rm -d $DBFILE
fi;
# first cull backups older.
echo "Culling backups older than $DAILY_BACKUPS"
find $BACKUP_DIR* -type d -ctime +$DAILY_BACKUPS | xargs rm -rf
exit 0;
else
echo "[!!ERROR!!] Failed to backup database $MSSQL_DATABASE"
exit 1;
fi;