-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore-remote.sh
executable file
·83 lines (61 loc) · 2.25 KB
/
restore-remote.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
#!/bin/bash
## Shared resources
scriptdir="$(dirname "$0")"
source "$scriptdir/.shared.sh"
# Verify available SSH key
SSH_KEY="$(verifySSH)"
remote_address="$REMOTE_SSH_USER@$REMOTE_SSH_IP"
remote_ssh="-t -p $REMOTE_SSH_PORT $remote_address $SSH_KEY"
## Run the script
# List all available snapshots in some pretty format
available=$(ssh $remote_ssh "ls $REMOTE_BACKUPS_FOLDER_PATH")
IFS=$'\n'
array=($available)
len=${#array[@]}
IFS=$' '
printf "${Underline_White}Date Time | ID | Database | Size | File etx. (user)${Styling_Off}\n"
for (( j=1; j<"$len"; j++ ))
do
echo ${array[$j]} $j \
| awk -F' ' '{ printf "%s_%s_%s\n", $10,$5,$9 }' \
| awk -F'_' '{ printf "%s %s\n", $0,$6; }' \
| sed 's/_/ /g' \
| awk -F' ' '{ split($6,a,"."); $6=a[1]; sub(a[1], "", $7); sub("-",":",$5); sub("-",":",$5); printf "%s\n", $0; }' \
| awk -F' ' '{ printf "%s %s |%4s| %-28s | %8s | %-14s (%s)\n", $4,$5,$1,$3,$2,$7,$6 }'
done
selected=""
printf "Enter file ID: "
read index
for (( j=0; j<"${#array[@]}"; j++ ))
do
if [ $j == $index ]
then
selected=${array[$j]}
selected=$( echo "$selected" | awk -F' ' '{ printf "%s",$9 }' )
fi
done
echo ":: ${selected}"
selectedDB=${selected%%[_.]*}
selected="${REMOTE_BACKUPS_FOLDER_PATH}/${selected}"
printf "${Yellow_On}:: Do you really wish to restore to this snapshot??${Styling_Off}\n:: [${Bold_On}y${Styling_Off}es/${Bold_On}n${Styling_Off}o] "
read affi
if [ $(saysYes "$affi") == "1" ]; then
# pass
echo ""
else
echoAlert "Command prevented"
# echoTitle "DONE"
exit 0
fi
# Create remote backup
echoTitle "Backup remote database" &&
up="--username=\"$REMOTE_MONGO_DB_USER\" --password=\"$REMOTE_MONGO_DB_PASS\"" &&
echoCmd "mongodump $up --archive --uri\"$REMOTE_MONGO_URI\" >> \"$REMOTE_BACKUP\"" &&
ssh $remote_ssh "mongodump $up --archive --uri=\"$REMOTE_MONGO_URI\" >> \"$REMOTE_BACKUP\"" &&
# Apply changes
echoTitle "Apply archived data to remote" &&
ns="--nsInclude=\"$selectedDB.*\" --nsFrom=\"$selectedDB.*\" --nsTo=\"$REMOTE_MONGO_DB_NAME.*\"" &&
echoCmd "mongorestore $up $ns --noIndexRestore --drop --archive=\"$selected\"" &&
ssh $remote_ssh "mongorestore $up $ns --noIndexRestore --drop --archive=\"$selected\"" &&
echoTitle "DONE"
exit 0