forked from seaster1970/migbin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmailsync
More file actions
executable file
·67 lines (57 loc) · 1.66 KB
/
Copy pathmailsync
File metadata and controls
executable file
·67 lines (57 loc) · 1.66 KB
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
#!/bin/bash
# make sure destination user is defined
if [[ -z "$USER" ]]
then
user="$USER"
else
user=$(whoami)
fi
# check to make sure user is not root.
if [[ "$user" == 'root' || -z "$user" ]]
then
echo "Cannot run as root. Please dj in before running mailsync."
exit 1
fi
# Get directory paths for destination
homedir=$(pwd | cut -d '/' -f 1,2,3)
migdir="${homedir}/migration/migstuff"
# check to see if ssh has already been setup and directories are present
if [[ ! -d ${migdir} || ! -s ${migdir}/source.txt || ! -s ${migdir}/id_rsa ]]
then
setupssh
mkdir -p "${migdir}"/logs/ 2>/dev/null
fi
# Get SSH credentials for connection
creds=$(cat "${migdir}"/source.txt)
keyFile="${migdir}/id_rsa"
sshConn="ssh -q -i ${keyFile} ${creds}"
# Test SSH connection
function testConn() {
${sshConn} exit
echo $?
}
echo -n "Checking SSH connection to source. "
connStatusCode=$(testConn)
if [[ "$connStatusCode" == '255' ]]
then
connStatus='fail'
echo "Connection $connStatus"
exit 1
elif [[ "$connStatusCode" == '0' ]]
then
connStatus='success'
echo "Connection $connStatus"
else
echo 'unexpected output, check ssh connection files and credentials'
fi
# Get the source user mail directory path and contents
srcHome=`${sshConn} "pwd && exit"`
# Write the rsync lines
if [[ -z "$1" ]]
then
rsync -avP -e "ssh -i ${keyFile}" ${creds}:${srcHome}/mail/ ${homedir}/mail/
rsync --ignore-existing -avP -e "ssh -i ${keyFile}" ${creds}:${srcHome}/etc/ ${homedir}/etc/
else
rsync -avP -e "ssh -i ${keyFile}" ${creds}:${srcHome}/mail/${1}/ ${homedir}/mail/${1}/
rsync --ignore-existing -avP -e "ssh -i ${keyFile}" ${creds}:${srcHome}/etc/${1}/ ${homedir}/etc/${1}/
fi