-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdump-databases.sh
executable file
·45 lines (40 loc) · 1.65 KB
/
dump-databases.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
#!/bin/bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
source $CURRENT_DIR/config.ini
# This script dump all databases to the user's database borg repo
# Assign arguments
TIME=$1
DB_COUNT=0
if [ -z $TIME ]; then
TIME=$(date +'%F')
fi
echo "$(date +'%F %T') #################### DUMP MYSQL DATABASES TO CORRESPONDING USER BORG REPO ####################"
# Get user list
while read USER ; do
USER_REPO=$REPO_DB_DIR/$USER
# Check if repo was initialized, if its not we perform borg init
if ! [ -d "$USER_REPO/data" ]; then
echo "-- No repo found. Initializing new borg repository $USER_REPO"
mkdir -p $USER_REPO
borg init $OPTIONS_INIT $USER_REPO
fi
# Get MySQL databases
while read DATABASE ; do
ARCHIVE="$DATABASE-$TIME"
echo "-- Creating new backup archive $USER_REPO::$ARCHIVE"
mysqldump $DATABASE --opt --routines --skip-comments | borg create $OPTIONS_CREATE $USER_REPO::$ARCHIVE -
borg prune $OPTIONS_PRUNE $USER_REPO --prefix ${DATABASE}'-'
let DB_COUNT++
done < <(v-list-databases $USER | grep -w mysql | cut -d " " -f1)
# Get PostgreSQL databases
while read DATABASE ; do
ARCHIVE="$DATABASE-$TIME"
echo "-- Creating new backup archive $USER_REPO::$ARCHIVE"
$CURRENT_DIR/inc/pg-pgdump.sh $DATABASE | borg create $OPTIONS_CREATE $USER_REPO::$ARCHIVE -
borg prune $OPTIONS_PRUNE $USER_REPO --prefix ${DATABASE}'-'
let DB_COUNT++
done < <(v-list-databases $USER | grep -w pgsql | cut -d " " -f1)
echo "-- Cleaning old backup archives"
done < <(v-list-users | cut -d " " -f1 | awk '{if(NR>2)print}')
echo "$(date +'%F %T') ########## $DB_COUNT DATABASES SAVED ##########"
echo