-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave_script
More file actions
148 lines (123 loc) · 3.73 KB
/
save_script
File metadata and controls
148 lines (123 loc) · 3.73 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
#!/bin/bash
# Variables
## Dossier à sauvegarder
BACKUPFOLDER=$1
## Dossier de destination pour le fichier de sauvegarde 'save.tar'
DESTFOLDER=$2
## Options du script
OPTION=$3
## Time, Date and name Variable
FRIDAY="5"
TODAY=`date +%u`
DATE=`date +%Y-%m-%d`
DATETIME=`date '+%Y-%m-%d_%H:%M:%S'`
DESTFILENAME=""
CRONSCRIPT=~/autosave.sh
CRONLINE="00 03 * * 5 $CRONSCRIPT"
# Fonction aide
usages() {
echo -e "Utilisation du scripts de sauvegarde :\n
./save_script FOLDER_TO_BACKUP DESTINATION_FOLER/ OPTION\n
AVANT TOUT VERIFIEZ QUE VOUS AVEZ LES BONS DROITS SUR LES DOSSIER SINON UTILISEZ sudo !\n
Les options disponibles sont :\n
ponctual : Sauvegarde immédiate\n
enable : Activer le service de sauvegarde tous les vendredi\n
disable : Désinstaller le service\n
check : Vérifier la présence les tâche à démarrage automatique\n
list : List les fichiers du dossier de destination de la sauvegarde\n
"
echo -e "Utilisez ./save_script --help ou ./save_script -h pour afficher ce message."
}
# Fonction sauvegarde
## Sauvegarde Incrémentable
save() {
# echo 'test save ok'
if [[ -e $DESTFOLDER/increment.list ]]; then
MAX=`ls | cut -c-1 | sort -n | tail -1`
else
touch $DESTFOLDER/increment.list
MAX="0"
fi;
if [ $TODAY -ge $FRIDAY ]; then
MAX=$(( $MAX + 1 ))
DESTFILENAME="$MAX-savefull.tar"
tar --create --file="$DESTFILENAME" --listed-incremental="$DESTFOLDER/increment.list" "$BACKUPFOLDER" 2>/dev/null
else
DESTFILENAME="$MAX-$DATETIME-save.tar"
tar --create --file="$DESTFILENAME" --listed-incremental="$DESTFOLDER/increment.list" "$BACKUPFOLDER" 2>/dev/null
fi;
find $DESTFOLDER -mtime +30 -type f -delete
echo "Sauvegarde effectuée"
}
## Activation sauvegarde automatique
enable_auto_save() {
# echo 'test enable_auto_save ok'
test -f $CRONSCRIPT || touch $CRONSCRIPT
echo "
#!/bin/bash
MAX=""
DESTFILENAME=""
DATETIME=`date '+%Y-%m-%d_%H:%M:%S'`
if [[ -e $DESTFOLDER/increment.list ]]; then
MAX=`ls | cut -c-1 | sort -n | tail -1`
else
touch $DESTFOLDER/increment.list
MAX="0"
fi;
if [ $TODAY -ge $FRIDAY ]; then
MAX=$(( $MAX + 1 ))
DESTFILENAME="$MAX-savefull.tar"
tar --create --file="$DESTFILENAME" --listed-incremental="$DESTFOLDER/increment.list" "$BACKUPFOLDER" 2>/dev/null
else
DESTFILENAME="$MAX-$DATETIME-save.tar"
tar --create --file="$DESTFILENAME" --listed-incremental="$DESTFOLDER/increment.list" "$BACKUPFOLDER" 2>/dev/null
fi;
find $DESTFOLDER -mtime +30 -type f -delete
" >> $CRONSCRIPT
chmod +x $CRONSCRIPT
(crontab -u $USER -l; echo "$CRONLINE";) | crontab -u $USER -
clear
echo "Sauvegarde automatique activée"
}
## Désactivation sauvegarde automatique
disable_auto_save() {
# echo 'test disable_auto_save ok'
crontab -u $USER -l | grep -v "$CRONSCRIPT" | crontab -u $USER -
rm -f $CRONSCRIPT
clear
echo "Sauvegarde automatique désactivée"
}
## Vérification des taches automatiques
check_cron() {
crontab -u $USER -l
}
## List des fichiers du dossier de destination
show_life() {
ls -la $DESTFOLDER
}
[ -d $DESTFOLDER ] || mkdir -p $DESTFOLDER
if [[ ($1 = "--help") || ($1 = "-h") ]]; then
usages
exit 1
elif [[ ($OPTION = "save") ]] ; then
save
exit 1
elif [[ ($OPTION = "enable") ]] ; then
enable_auto_save
exit 1
elif [[ ($OPTION = "disable") ]] ; then
disable_auto_save
exit 1
elif [[ ($OPTION = "check") ]] ; then
check_cron
exit 1
elif [[ ($1 = "check") ]]; then
check_cron
exit 1
elif [[ ($OPTION = "show") ]] ; then
show_life
exit 1
else
echo -e "Commande Invalide\n"
usages
fi;