This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
executable file
·85 lines (71 loc) · 2.47 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash
# Set the directory where backups will be stored
BACKUP_DIR=/home/backup
# Set the database name, username, and password
DATABASE_NAME=duwit
DATABASE_USER=duwit
DATABASE_PASSWORD=g3Nd3LJckbYgH8MZ
# Set the hostname and port of the remote server
REMOTE_SERVER=172.30.0.19
REMOTE_PORT=22
# Set the host and port of the PostgreSQL server
DB_HOST=localhost
DB_PORT=5432
# Set the username and directory on the remote server
REMOTE_USER=clouduser
REMOTE_DIR=/home/backup
# Get the current date and time
DATE=$(date +%Y-%m-%d_%H-%M-%S)
# create logs folder
if [ ! -d $BACKUP_DIR/log/ ]; then
mkdir -p $BACKUP_DIR/log/
fi
# create logs file
LOG_FILE=$BACKUP_DIR/log/backup-$DATE.log
if [ ! -f "$LOG_FILE" ]; then
touch $LOG_FILE
fi
# create full backup directory
FULL_DIR=$BACKUP_DIR/full
if [ ! -d $FULL_DIR ]; then
mkdir -p $FULL_DIR
fi
# create incremental backup directory
INC_DIR=$BACKUP_DIR/inc
if [ ! -d $INC_DIR ]; then
mkdir -p $INC_DIR
fi
# Create a full backup of the database
pg_dump --dbname="postgresql://$DATABASE_USER:$DATABASE_PASSWORD@$DB_HOST:$DB_PORT/$DATABASE_NAME" -F t -f $FULL_DIR/full_$DATE.tar 2>> $LOG_FILE
# check if backup success
if [ $? -eq 0 ]; then
echo "Full Backup Success at $DATE" >> $LOG_FILE
# send the backup to remote server
scp -P $REMOTE_PORT $FULL_DIR/full_$DATE.tar $REMOTE_USER@$REMOTE_SERVER:$REMOTE_DIR/full/ 2>> $LOG_FILE
# check if file transfer success
if [ $? -eq 0 ]; then
echo "Full Backup file transfer success at $DATE" >> $LOG_FILE
# rm $FULL_DIR/full_$DATE.tar
else
echo "Full Backup file transfer failed at $DATE" >> $LOG_FILE
fi
else
echo "Full Backup failed at $DATE" >> $LOG_FILE
fi
# Create an incremental backup of the database
pg_dump --dbname="postgresql://$DATABASE_USER:$DATABASE_PASSWORD@$DB_HOST:$DB_PORT/$DATABASE_NAME" -F t -f $INC_DIR/incremental_$DATE.tar 2>> $LOG_FILE
# check if backup success
if [ $? -eq 0 ]; then
echo "Incremental Backup Success at $DATE" >> $LOG_FILE
# send the backup to remote server
scp -P $REMOTE_PORT $INC_DIR/incremental_$DATE.tar $REMOTE_USER@$REMOTE_SERVER:$REMOTE_DIR/inc/ 2>> $LOG_FILE
# check if file transfer success
if [ $? -eq 0 ]; then
echo "Incremental Backup file transfer success at $DATE" >> $LOG_FILE
# rm $INC_DIR/incremental_$DATE.tar
else
echo "Incremental Backup file transfer failed at $DATE" >> $LOG_FILE
fi
else
echo "Incremental Backup failed at $DATE" >> $LOG_FILE
fi