-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsncr.sh
More file actions
executable file
·152 lines (150 loc) · 5.05 KB
/
Copy pathsncr.sh
File metadata and controls
executable file
·152 lines (150 loc) · 5.05 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
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
145
146
147
148
149
150
151
152
#!/bin/bash
##################################### USAGE v1.0.0 ################################
usage="Usage:\n
sncr (Will run the sync with the provided configuration, if no config is provided it will prompt you for the inital configuration.) \n
sncr -config (Will bring the configuration prompt where you can change parameters) \n
\n\t Changeable parameters:
\n\t server address (ex. 192.168.X.X)
\n\t server username (the host machine username)
\n\t how often the sync happens (in minutes, 0 for no automaatic syncing)
\n\t local directory (where the files will be synced into, requires absoulute path, default is $HOME/sncr_files)
\n\n
sncr -log (outputs the content of the last logfile) \n
sncr -log -n <number> (outpots only the last <number> lines of the last logfile) \n
\n
sncr -last (outputs when the last successfull sync happened) \n
./sncr.sh -install (installs sncr by moving the sncr.sh script to /usr/bin and deletes the sncr folder cloned from git) \n
sncr -update (updates /usr/bin/sncr to the latest version from git) \n
sncr -help (outputs this usage screen)"
##################################################################################
hiddenfolder=$HOME/.sncr
config () {
read -p "Server IP or DNS: " serverip
while [ -z "${serverip}" ]
do
echo "You must provide a server IP, please try again!"
read -p "Server IP or DNS: " serverip
done
read -p "Server user: " serverusername
while [ -z "${serverusername}" ]
do
echo "You must provide a server name, please try again!"
read -p "Server user: " serverusername
done
read -p "Sync frequency [in minutes , default (0) means no automatic sync]: " minutes
read -p "Path to local directory where the content is to be copied (default is $HOME/sncr_files): " local_dir
mkdir -p $hiddenfolder
touch $hiddenfolder/sncr.conf
if [ -z "${local_dir}" ]
then
local_dir=$HOME/sncr_files
fi
if [ ! -d "${local_dir}" ]
then
mkdir $local_dir
fi
logdir=$hiddenfolder/rsynclogs
if [ ! -d "${logdir}" ] #if logdir already created dont create again
then
mkdir $logdir
fi
if [ -z "${minutes}" ]
then
minutes=0
fi
echo "SERVER_IP=$serverip" > $hiddenfolder/sncr.conf
echo "SERVER_USERNAME=$serverusername" >> $hiddenfolder/sncr.conf
echo "SYNC_MINUTES=$minutes" >> $hiddenfolder/sncr.conf
echo "LOCAL_DIR=$local_dir" >> $hiddenfolder/sncr.conf
echo "New configuration saved!"
}
if [ "$1" = "-config" ]
then
config
exit 1
elif [ "$1" = "-log" ]
then
last_modified_file=`find $hiddenfolder/rsynclogs/ -type f -printf "%T@\0%p\0" | awk '
{
if ($0>max) {
max=$0;
getline mostrecent
} else
getline
}
END{print mostrecent}' RS='\0'`
if [ "$2" = "-n" ]
then
regex='^[0-9]+$' #is a number regex
if [[ "$3" =~ $regex ]]
then
tail $last_modified_file -n $3
else
echo "sncr: Invalid usage, argument after -n shoud be an number, run -h!"
fi
elif [ "$2" = "-last" ]
then
cat $hiddenfolder/last_sync_success.log
else
cat $last_modified_file
fi
elif [ "$1" = "-last" ]
then
cat $hiddenfolder/last_sync_success.log
elif [ "$1" = "-h" ] || [ "$1" = "-help" ]
then
echo -e $usage
elif [ "$1" = "-install" ]
then
echo "Checking if sncr is already installed.."
which sncr
if [ "$?" -ne "0" ]
then
sudo cp sncr.sh /usr/bin/sncr
cd ..
rm -rf sncr
echo "Successfully installed sncr to /usr/bin/sncr"
else
echo "sncr is already installed"
fi
elif [ "$1" = "-update" ]
then
echo "Checking if sncr is installed.."
which sncr
if [ "$?" -eq "0" ]
then
git clone https://gitlab.com/semiismaili/sncr.git
cd sncr
sudo cp sncr.sh /usr/bin/sncr
cd ..
rm -rf sncr
echo "Successfully updated sncr at /usr/bin/sncr"
else
echo "No sncr installation found at /usr/bin/, please run ./sncr.sh -install first or -help for more info."
fi
elif [ $# -eq 0 ]
then #actual work
if [ ! -f $hiddenfolder/sncr.conf ] || [ ! -s $hiddenfolder/sncr.conf ] #if conf file doesnt exist or is empty
then
config
fi
. $hiddenfolder/sncr.conf
address="$SERVER_USERNAME@$SERVER_IP"
minutes=$SYNC_MINUTES
local_dir=$LOCAL_DIR
while true
do
rsync -avzP --log-file=$hiddenfolder/rsynclogs/rsync-backup-log-$(date +"%Y-%m-%d").log --delete rsync://$address:12000/files $local_dir
if [ "$?" -eq "0" ]
then
echo 'Last successfull sync on' $(date) > $hiddenfolder/last_sync_success.log
fi
if [[ $minutes -eq 0 ]]
then
break
fi
sleep $minutes
done
else
echo "sncr: Invalid usage, run -h for usage info!"
fi