1
+ #! /usr/bin/env bash
2
+
3
+ { # this ensures the entire script is downloaded #
4
+
5
+ dbsync_install_dir () {
6
+ printf %s " ${HOME} /.dbsync"
7
+ }
8
+
9
+ dbsync_latest_version () {
10
+ echo " v1.0.0"
11
+ }
12
+
13
+ #
14
+ # Outputs the location to DB Sync depending on:
15
+ # * The availability of $GIT_SOURCE
16
+ # * The method used ("script" or "git" in the script, defaults to "git")
17
+ #
18
+ dbsync_source () {
19
+ local DBSYNC_METHOD
20
+ DBSYNC_METHOD=" $1 "
21
+ local DBSYNC_SOURCE_URL
22
+ DBSYNC_SOURCE_URL=" https://github.com/clivewalkden/bash-magento2-db-sync.git"
23
+ echo " $DBSYNC_SOURCE_URL "
24
+ }
25
+
26
+ do_install () {
27
+ local INSTALL_DIR
28
+ INSTALL_DIR=" $( dbsync_install_dir) "
29
+
30
+ # Downloading to $INSTALL_DIR
31
+ mkdir -p " $INSTALL_DIR "
32
+ if [ -f " $INSTALL_DIR /db-sync.sh" ]; then
33
+ echo " => db-sync is already installed in $INSTALL_DIR , trying to update the script"
34
+ else
35
+ echo " => Downloading db-sync as script to '$INSTALL_DIR '"
36
+ fi
37
+
38
+ if [ -d " $INSTALL_DIR /.git" ]; then
39
+ echo " => db-sync is already installed in $INSTALL_DIR , trying to update using git"
40
+ command printf ' \r=> '
41
+ command git --git-dir=" $INSTALL_DIR " /.git --work-tree=" $INSTALL_DIR " fetch origin tag " $( dbsync_latest_version) " --depth=1 2> /dev/null || {
42
+ echo >&2 " Failed to update db-sync, run 'git fetch' in $INSTALL_DIR yourself."
43
+ exit 1
44
+ }
45
+ else
46
+ # Cloning to $INSTALL_DIR
47
+ echo " => Downloading db0sync from git to '$INSTALL_DIR '"
48
+ command printf ' \r=> '
49
+ mkdir -p " ${INSTALL_DIR} "
50
+ if [ " $( ls -A " ${INSTALL_DIR} " ) " ]; then
51
+ command git init " ${INSTALL_DIR} " || {
52
+ echo >&2 ' Failed to initialize db-sync repo. Please report this!'
53
+ exit 2
54
+ }
55
+ command git --git-dir=" ${INSTALL_DIR} /.git" remote add origin " $( dbsync_source) " 2> /dev/null \
56
+ || command git --git-dir=" ${INSTALL_DIR} /.git" remote set-url origin " $( dbsync_source) " || {
57
+ echo >&2 ' Failed to add remote "origin" (or set the URL). Please report this!'
58
+ exit 2
59
+ }
60
+ command git --git-dir=" ${INSTALL_DIR} /.git" fetch origin tag " $( dbsync_latest_version) " --depth=1 || {
61
+ echo >&2 ' Failed to fetch origin with tags. Please report this!'
62
+ exit 2
63
+ }
64
+ else
65
+ command git -c advice.detachedHead=false clone " $( dbsync_source) " -b " $( dbsync_latest_version) " --depth=1 " ${INSTALL_DIR} " || {
66
+ echo >&2 ' Failed to clone dbsync repo. Please report this!'
67
+ exit 2
68
+ }
69
+ fi
70
+ fi
71
+ command git -c advice.detachedHead=false --git-dir=" $INSTALL_DIR " /.git --work-tree=" $INSTALL_DIR " checkout -f --quiet " $( dbsync_latest_version) "
72
+ if [ -n " $( command git --git-dir=" $INSTALL_DIR " /.git --work-tree=" $INSTALL_DIR " show-ref refs/heads/master) " ]; then
73
+ if command git --git-dir=" $INSTALL_DIR " /.git --work-tree=" $INSTALL_DIR " branch --quiet 2> /dev/null; then
74
+ command git --git-dir=" $INSTALL_DIR " /.git --work-tree=" $INSTALL_DIR " branch --quiet -D master > /dev/null 2>&1
75
+ else
76
+ echo >&2 " Your version of git is out of date. Please update it!"
77
+ command git --git-dir=" $INSTALL_DIR " /.git --work-tree=" $INSTALL_DIR " branch -D master > /dev/null 2>&1
78
+ fi
79
+ fi
80
+
81
+ echo " => Compressing and cleaning up git repository"
82
+ if ! command git --git-dir=" $INSTALL_DIR " /.git --work-tree=" $INSTALL_DIR " reflog expire --expire=now --all; then
83
+ echo >&2 " Your version of git is out of date. Please update it!"
84
+ fi
85
+ if ! command git --git-dir=" $INSTALL_DIR " /.git --work-tree=" $INSTALL_DIR " gc --auto --aggressive --prune=now ; then
86
+ echo >&2 " Your version of git is out of date. Please update it!"
87
+ fi
88
+ return
89
+ }
90
+
91
+ do_install
92
+
93
+ } # this ensures the entire script is downloaded #
0 commit comments