-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynchro_rep.sh
executable file
·101 lines (87 loc) · 3.06 KB
/
synchro_rep.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
#!/bin/sh
#===============================================================================
# CREATION
# Date de creation : 17/08/2017
# Auteur : jdesesquelles
# Version : V1.0
#===============================================================================
#
# Script Synchro home => USB backup
#
#===============================================================================
# ------------------------------------------------------------------------------
# VARIABLES: globales
# ------------------------------------------------------------------------------
DATE=$(date +"%Y%m%d-%H%M")
DIR_MOUNT="/mnt/filer"
DIR_BACKUP="$DIR_MOUNT/backup"
LOG_FILE="/var/log/synchroUSB-${DATE}.log"
# ------------------------------------------------------------------------------
# FONCTION: Message et log
# ------------------------------------------------------------------------------
displayMessage() {
echo "$*"
}
displayError() {
echo -e "\r\e[0;31m* $* *\e[0m"
exit 1
}
displayTitle() {
displayMessage "------------------------------------------------------------------------------"
displayMessage "$*"
displayMessage "------------------------------------------------------------------------------"
}
# Parametres: MESSAGE COMMAND
displayAndExec() {
local message=$1
echo -n " [ Veuillez patienter... ] $message"
shift
echo -e "\n\n>>> $*" >> $LOG_FILE 2>&1
sh -c "$*" >> $LOG_FILE 2>&1
local ret=$?
if [ $ret -ne 0 ]; then
echo -e "\r\e[0;31m [ ERROR ]\e[0m $message"
else
echo -e "\r\e[0;32m [ OK ]\e[0m $message"
fi
return $ret
}
# ------------------------------------------------------------------------------
# FONCTION: Globales
# ------------------------------------------------------------------------------
function testMount {
## a remplacer par une connection mount
##### /bin/ls $DIR_MOUNT > /dev/null
/bin/mount |grep $DIR_MOUNT > /dev/null
if [ ! $? -eq 0 ]; then
displayError "Impossible de monter le repertoire distant."
exit 1
fi
}
function checkProcRsync {
if /usr/bin/pgrep rsync 2>&1 > /dev/null; then
displayError "Un processus rsync est deja en cours."
exit 1
fi
}
# ------------------------------------------------------------------------------
# MAIN
# ------------------------------------------------------------------------------
#clear
echo
displayTitle "Script synchro de sa HOME"
testMount
checkProcRsync
# Repertoire de backup
[ -d $DIR_BACKUP ]|| mkdir -p $DIR_BACKUP
echo -e "\n\t [DEBUT] -- $(date)" > $LOG_FILE
## repertoire et fichier a rsync
displayAndExec "Synchro du repertoire Documents" "rsync -aP ${HOME}/Documents/ ${DIR_BACKUP}/Documents"
displayAndExec "Synchro du repertoire Images" "rsync -aP ${HOME}/Pictures/ ${DIR_BACKUP}/Pictures"
displayAndExec "Synchro du repertoire Musiques" "rsync -aP ${HOME}/Music/ ${DIR_BACKUP}/Music"
displayAndExec "Synchro du repertoire Videos" "rsync -aP ${HOME}/Videos/ ${DIR_BACKUP}/Videos"
echo -e "\n\t [ FIN ] -- $(date)" >> $LOG_FILE
## retention LOG
find /var/log -name "synchroUSB*.log" -mtime +5 -exec rm -f {} \;
echo
exit 0