-
Notifications
You must be signed in to change notification settings - Fork 3
/
backup.sh
executable file
·44 lines (32 loc) · 1.54 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
#!/usr/bin/env bash
set -e
APP_CONTAINER_NAME="lanager"
NETWORK_NAME="lanager-docker-compose_lanager-network"
if [ "$( docker container inspect -f '{{.State.Status}}' $APP_CONTAINER_NAME )" != "running" ]; then
echo "Error: Container \"$APP_CONTAINER_NAME\" is not running"
exit 1;
fi
DATE=$(date +"%Y-%m-%d")
BACKUP_NAME="lanager-backup-$DATE-$HOSTNAME"
BACKUP_FILE="$BACKUP_NAME.tar.gz"
TEMP_DIR="/tmp"
DB_BACKUP_FILE="$TEMP_DIR/$BACKUP_NAME/lanager-database.sql"
STORAGE_BACKUP_FILE="lanager-storage.tar"
ENV_BACKUP_FILE="$TEMP_DIR/$BACKUP_NAME/lanager-environment.env"
echo "Creating temporary directory for backup data: $TEMP_DIR/$BACKUP_NAME"
mkdir -p "$TEMP_DIR/$BACKUP_NAME"
echo "Loading database credentials from the .env file into environment variables"
source .env
echo "Dumping database data into $DB_BACKUP_FILE"
docker run -t -e MYSQL_PWD="$DB_ROOT_PASSWORD" --network $NETWORK_NAME --rm mysql:8 \
mysqldump -hDB -uroot --add-drop-database --databases lanager > "$DB_BACKUP_FILE"
echo "Backing up the storage/ directory stored in the lanager_laravel-storage volume"
docker run --rm --volumes-from $APP_CONTAINER_NAME -v "$TEMP_DIR":/backup mysql:8 tar cf "/backup/$BACKUP_NAME/$STORAGE_BACKUP_FILE" \
/var/www/lanager/storage
echo "Backing up the .env file into $ENV_BACKUP_FILE"
cp .env "$ENV_BACKUP_FILE"
echo "Compressing all backup files into $BACKUP_NAME.tar.gz"
tar czf "$BACKUP_FILE" -C "$TEMP_DIR" "$BACKUP_NAME"
echo "Removing temporary directory"
rm -rf "${TEMP_DIR:?}/$BACKUP_NAME"
echo "Successfully created backup archive"