-
Notifications
You must be signed in to change notification settings - Fork 0
/
backupscript.sh
executable file
·144 lines (126 loc) · 3.73 KB
/
backupscript.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
ROOT_DIR='.'
SRC_DIR=''
DEST_DIR=''
DEPTH=2
setroot=0
setsrc=0
setdest=0
setdepth=0
for i in "$@"; do
case $i in
--help)
echo "USAGE: backupscript -r <ROOT_DIR> -s <SRC_DIR> -d <DEST_DIR> [-x <DEPTH>]
Where:
ROOT_DIR = the start folder in which to scan for docker-compose.yml files
SRC_DIR = The source location to copy FROM
DEST_DIR = The destination to copy TO
DEPTH = How many levels down from the ROOT_DIR to scan for docker-compose files"
;;
-r | --root)
setroot=1
;;
-s | -src)
setsrc=1
;;
-d | -dest)
setdest=1
;;
-x | -depth)
setdepth=1
;;
*)
if [ -n "$i" ]; then
if [ "$setroot" = "1" ]; then
ROOT_DIR=$i
setroot=0
elif [ "$setsrc" = "1" ]; then
SRC_DIR=$i
setsrc=0
elif [ "$setdest" == "1" ]; then
DEST_DIR=$i
setdest=0
elif [ "$setdepth" == "1" ]; then
DEPTH=$i
setdest=0
else
echo "Unknown command line: $i"
exit 1
fi
fi
;;
esac
done
function start-compose-cmd() {
echo "Starting $1"
composecmd=$(which docker-compose)
if [ -z $composecmd ]; then
composecmd="$(which docker) compose"
fi
composecmd="${composecmd:-/usr/local/bin/docker compose}" # deal with which not returning a value
currentdir=$(pwd)
workingdir=$(dirname "$1")
cd "$workingdir" || return
$composecmd up -d
cd "$currentdir" || exit 1
}
export -f start-compose-cmd
function stop-compose-cmd() {
echo "Stopping $1"
composecmd=$(which docker-compose)
if [ -z $composecmd ]; then
composecmd="$(which docker) compose"
fi
composecmd="${composecmd:-/usr/local/bin/docker compose}" # deal with which not returning a value
currentdir=$(pwd)
workingdir=$(dirname "$1")
cd "$workingdir" || return
$composecmd stop
cd "$currentdir" || exit 1
}
export -f stop-compose-cmd
echo "Starting backup at $(date) from DIR $PWD...
ROOT_DIR=$ROOT_DIR
SRC_DIR=$SRC_DIR
DEST_DIR=$DEST_DIR
DEPTH=$DEPTH"
# Verify we are running as root
if [[ $EUID -ne 0 ]]; then
echo "Failed to start backup at $(date). Must be run as root user..."
exit 1
fi
# Stop any autoheal monitor containers (as these can cause other containers to restart!
echo looking for autoheal containers
autoheal_id=$(sudo docker ps -aqf "name=autoheal")
if [ -n "$autoheal_id" ]; then
echo "Found autoheal container id(s): $autoheal_id"
echo "Stoppping autoheal containers..."
sudo docker stop "$autoheal_id"
echo "...Assuming this will be restarted by docker-compose later"
fi
# Cleanly stop all running containers using compose
echo "Running compose stop..."
find "${ROOT_DIR}" -maxdepth "${DEPTH}" -name "docker-compose.yml" -exec echo Stop {} ... \; -exec bash -c 'stop-compose-cmd "$0"' {} \;
# Stop docker to take down any other non-compose containers
# echo "Stopping Docker Service..."
# systemctl stop docker
## rsync command - add further switches for src and destination
echo "Starting rsync..."
if ! rsync -axHhv --exclude 'swapfile' --exclude '*.swp' --exclude '*.tmp' --exclude ';' --inplace --delete "${SRC_DIR}" "${DEST_DIR}";then
echo "
*******************************************
*******************************************
************ RSYNC FAILED *****************
*******************************************
*******************************************
"
fi
# restart docker (will also bring up any containers set to restart: always)
# echo "Restarting Docker Service..."
# systemctl start docker
#sudo reboot
# restart all stopped containers using compose
echo "Running compose up..."
find "${ROOT_DIR}" -maxdepth "${DEPTH}" -name "docker-compose.yml" -exec echo up {} ... \; -exec bash -c 'start-compose-cmd "$0"' {} \;
echo ...Backup completed at "$(date)"
exit 0