-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_database.sh
More file actions
61 lines (48 loc) · 1.2 KB
/
Copy pathbackup_database.sh
File metadata and controls
61 lines (48 loc) · 1.2 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
source db_config.sh
BACKUP_DIR="backups"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="${BACKUP_DIR}/inventory_db_${TIMESTAMP}.sql"
mkdir -p "$BACKUP_DIR"
create_backup() {
pg_dump \
-h "$PGHOST" \
-p "$PGPORT" \
-U "$PGUSER" \
--no-owner \
--no-privileges \
--format=plain \
--file "$BACKUP_FILE" \
"$PGDATABASE"
if [ $? -ne 0 ]; then
echo "Backup failed during pg_dump."
return 1
fi
if [ ! -s "$BACKUP_FILE" ]; then
echo "Backup file was not created or is empty."
return 1
fi
return 0
}
validate_backup() {
local backup_file="$1"
if [ ! -f "$backup_file" ]; then
echo "Backup validation failed: file does not exist."
return 1
fi
if [ ! -s "$backup_file" ]; then
echo "Backup validation failed: file is empty."
return 1
fi
echo "Backup file exists and is not empty."
echo "Preview of backup file:"
head -n 5 "$backup_file"
return 0
}
echo "Starting backup at $(date)"
if create_backup && validate_backup "$BACKUP_FILE"; then
echo "Backup completed: $BACKUP_FILE"
else
echo "Backup failed."
exit 1
fi