-
Notifications
You must be signed in to change notification settings - Fork 12
/
sync.sh
executable file
·116 lines (102 loc) · 5.76 KB
/
sync.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
#!/bin/bash
# Reset
Color_Off='\033[0m' # Text Reset
# Regular Colors
Black='\033[0;30m' # Black
Red='\033[0;31m' # Red
Green='\033[0;32m' # Green
Yellow='\033[0;33m' # Yellow
Blue='\033[0;34m' # Blue
Purple='\033[0;35m' # Purple
Cyan='\033[0;36m' # Cyan
White='\033[0;37m' # White
# Background
On_Black='\033[40m' # Black
On_Red='\033[41m' # Red
On_Green='\033[42m' # Green
On_Yellow='\033[43m' # Yellow
On_Blue='\033[44m' # Blue
On_Purple='\033[45m' # Purple
On_Cyan='\033[46m' # Cyan
On_White='\033[47m' # White
BACKUP_NAME=backup-`date "+%Y-%m-%d_%H-%M-%S"`.sql
DOCKER_NAME=koe_app
SOURCE=$1
TARGET=$2
if [[ -z "$SOURCE" ]]; then
echo "Usage: sync.sh source_cfg_file target_cfg_file"
exit 0
fi
if [[ "$SOURCE" != "this" && ! -f ${SOURCE} ]]; then
echo "File $SOURCE not found!"
exit 1
fi
if [[ -z "$TARGET" ]]; then
echo "Usage: sync.sh source_cfg_file target_cfg_file"
exit 0
fi
if [[ "$TARGET" != "this" ]]; then
if [[ ! -f ${TARGET} ]]; then
echo "File $TARGET not found!"
exit 1
fi
fi
if [[ "$SOURCE" = "$TARGET" ]]; then
echo "Source and Target cannot be the same"
exit 1
fi
if [[ "$SOURCE" != "this" ]]; then
source $SOURCE
echo -e "${Yellow}${On_Purple}Create a backup on the remote machine${Color_Off}"
echo -e "${Green}${On_Black}docker exec -it koe_app python maintenance.py --backup-database --file=backups/mysql/$BACKUP_NAME${Color_Off}"
ssh $SSH_EXTRA_CREDENTIAL $REMOTE_USER@$REMOTE_ADDRESS "cd $WORKSPACE; docker exec koe_app python maintenance.py --backup-database --file=backups/mysql/$BACKUP_NAME"
echo -e "${Yellow}${On_Purple}Copy it over here${Color_Off}"
echo -e "${Green}${On_Black}scp $SSH_EXTRA_CREDENTIAL $REMOTE_USER@$REMOTE_ADDRESS:$WORKSPACE/backups/mysql/$BACKUP_NAME $BACKUP_NAME${Color_Off}"
scp $SSH_EXTRA_CREDENTIAL $REMOTE_USER@$REMOTE_ADDRESS:$WORKSPACE/backups/mysql/$BACKUP_NAME backups/mysql/$BACKUP_NAME
ssh $SSH_EXTRA_CREDENTIAL $REMOTE_USER@$REMOTE_ADDRESS "cd $WORKSPACE; rm -f backups/mysql/$BACKUP_NAME"
else
source .venv/bin/activate
# echo -e "${Yellow}${On_Purple}Create a backup on the local machine${Color_Off}"
# echo -e "${Green}${On_Black}python maintenance.py --backup-database --file=backups/mysql/$BACKUP_NAME${Color_Off}"
# python maintenance.py --backup-database --file=backups/mysql/$BACKUP_NAME
fi
# if the target is this, copy remote file back here
# Otherwise, if the source is this, copy local files to the remote server
if [[ "$TARGET" = "this" ]]; then
source ./.venv/bin/activate
echo -e "${Yellow}${On_Purple}Restore the backup in the local machine${Color_Off}"
echo -e "${Green}${On_Black}python maintenance.py --restore-database --file=backups/mysql/$BACKUP_NAME${Color_Off}"
python maintenance.py --restore-database --file=backups/mysql/$BACKUP_NAME
echo -e "${Green}${On_Black}rsync -aP $REMOTE_USER@$REMOTE_ADDRESS:$WORKSPACE/user_data/ordination user_data/${Color_Off}"
rsync -aP $REMOTE_USER@$REMOTE_ADDRESS:$WORKSPACE/user_data/ordination user_data/
echo -e "${Green}${On_Black}rsync -aP $REMOTE_USER@$REMOTE_ADDRESS:$WORKSPACE/user_data/similarity user_data/${Color_Off}"
rsync -aP $REMOTE_USER@$REMOTE_ADDRESS:$WORKSPACE/user_data/similarity user_data/
echo -e "${Green}${On_Black}rsync -aP $REMOTE_USER@$REMOTE_ADDRESS:$WORKSPACE/user_data/measurement user_data/${Color_Off}"
rsync -aP $REMOTE_USER@$REMOTE_ADDRESS:$WORKSPACE/user_data/measurement user_data/
echo -e "${Green}${On_Black}rsync -aP $REMOTE_USER@$REMOTE_ADDRESS:$WORKSPACE/user_data/audio user_data/${Color_Off}"
rsync -aP $REMOTE_USER@$REMOTE_ADDRESS:$WORKSPACE/user_data/audio user_data/
echo -e "${Green}${On_Black}rsync -aP $REMOTE_USER@$REMOTE_ADDRESS:$WORKSPACE/user_data/pickle user_data/${Color_Off}"
rsync -aP $REMOTE_USER@$REMOTE_ADDRESS:$WORKSPACE/user_data/pickle user_data/
else
source ${TARGET}
# echo -e "${Yellow}${On_Purple}Copy it to the target${Color_Off}"
# echo -e "cp $SSH_EXTRA_CREDENTIAL backups/mysql/$BACKUP_NAME $REMOTE_USER@$REMOTE_ADDRESS:$WORKSPACE/backups/mysql/$BACKUP_NAME"
# scp ${SSH_EXTRA_CREDENTIAL} backups/mysql/${BACKUP_NAME} ${REMOTE_USER}@${REMOTE_ADDRESS}:${WORKSPACE}/backups/mysql/$BACKUP_NAME
#
# echo -e "${Yellow}${On_Purple}Restore the backup in remote machine${Color_Off}"
# echo -e "${Green}${On_Black}docker exec -it koe_app python maintenance.py --restore-database --file=backups/mysql/$BACKUP_NAME${Color_Off}"
# ssh $SSH_EXTRA_CREDENTIAL $REMOTE_USER@$REMOTE_ADDRESS "cd $WORKSPACE; docker exec koe_app python maintenance.py --restore-database --file=backups/mysql/$BACKUP_NAME"
fi
if [[ "$SOURCE" = "this" ]]; then
source ${TARGET}
echo -e "${Green}${On_Black}rsync -aP $REMOTE_USER@$REMOTE_ADDRESS:$WORKSPACE/user_data/ordination user_data/${Color_Off}"
rsync -aP user_data/ordination $REMOTE_USER@$REMOTE_ADDRESS:$WORKSPACE/user_data/
echo -e "${Green}${On_Black}rsync -aP $REMOTE_USER@$REMOTE_ADDRESS:$WORKSPACE/user_data/similarity user_data/${Color_Off}"
rsync -aP user_data/similarity $REMOTE_USER@$REMOTE_ADDRESS:$WORKSPACE/user_data/
echo -e "${Green}${On_Black}rsync -aP $REMOTE_USER@$REMOTE_ADDRESS:$WORKSPACE/user_data/measurement user_data/${Color_Off}"
rsync -aP user_data/measurement $REMOTE_USER@$REMOTE_ADDRESS:$WORKSPACE/user_data/
echo -e "${Green}${On_Black}rsync -aP $REMOTE_USER@$REMOTE_ADDRESS:$WORKSPACE/user_data/audio user_data/${Color_Off}"
rsync -aP user_data/audio $REMOTE_USER@$REMOTE_ADDRESS:$WORKSPACE/user_data/
echo -e "${Green}${On_Black}rsync -aP $REMOTE_USER@$REMOTE_ADDRESS:$WORKSPACE/user_data/pickle user_data/${Color_Off}"
rsync -aP user_data/pickle $REMOTE_USER@$REMOTE_ADDRESS:$WORKSPACE/user_data/
fi