-
Notifications
You must be signed in to change notification settings - Fork 0
/
fullBackup.sh
executable file
·42 lines (34 loc) · 955 Bytes
/
fullBackup.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
#/!bin/bash
# Set variables
DB_NAME="testHibernate"
CRON_USER="cron_user"
CRON_PASS="someStrongPassword"
FULLDATE=$(date +"%Y-%d-%m %H:%M")
NOW=$(date +"%Y-%m-%d-%H-%M")
MYSQL_DUMP=`which mysqldump`
GIT=`which git`
TEMP_BACKUP="latest_backup.sql"
BACKUP_DIR=$(date +"%Y/%m")
GIT_REPO_URL="https://github.com/smzerehpoush/myDbBackup.git"
GIT_REPO_DIR="myDbBackup"
#make backup
cd "../$GIT_REPO_DIR"
# Check current Git status and update
${GIT} status
${GIT} pull origin HEAD
touch "$TEMP_BACKUP"
# Dump database
${MYSQL_DUMP} -u "$CRON_USER" -p"$CRON_PASS" $DB_NAME > $TEMP_BACKUP &
wait
# Make backup directory if not exists (format: {year}/{month}/)
if [ ! -d "$BACKUP_DIR" ]; then
mkdir -p $BACKUP_DIR
fi
# Compress SQL dump
tar -cvzf $BACKUP_DIR/$DB_NAME-$NOW-sql.tar.gz $TEMP_BACKUP
# Remove original SQL dump
#rm -f $TEMP_BACKUP
# Add to Git and commit
${GIT} add -A
${GIT} commit -m "Automatic backup - $FULLDATE"
${GIT} push origin HEAD