-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup_db.sh
executable file
·58 lines (49 loc) · 1.29 KB
/
backup_db.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
50
51
52
53
54
55
56
57
58
#!/bin/bash
usage() {
echo "Usage: $0 --database-url <DATABASE_URL> [--backup-dir <BACKUP_DIR>]"
echo
echo "Options:"
echo " --database-url PostgreSQL connection URL (required)"
echo " --backup-dir Backup directory (optional, defaults to ./backups)"
echo
echo "Example:"
echo " $0 --database-url 'postgres://user:pass@host/dbname' --backup-dir '/path/to/backups'"
exit 1
}
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export TZ='Asia/Taipei'
while [[ $# -gt 0 ]]; do
case $1 in
--database-url)
DATABASE_URL="$2"
shift 2
;;
--backup-dir)
BACKUP_DIR="$2"
shift 2
;;
-h|--help)
usage
;;
*)
echo "Unknown parameter: $1"
usage
;;
esac
done
if [ -z "$DATABASE_URL" ]; then
echo "Error: DATABASE_URL is required"
usage
fi
backup_dir="${BACKUP_DIR:-$SCRIPT_DIR/backups}"
mkdir -p "$backup_dir"
timestamp=$(date +%Y%m%d_%H%M%S_%Z%z)
backup_file="${backup_dir}/backup_${timestamp}.sql"
pg_dump "$DATABASE_URL" > "$backup_file"
# Check if backup was successful
if [ $? -eq 0 ]; then
echo "Backup completed successfully: $backup_file"
else
echo "Backup failed!"
exit 1
fi