-
Notifications
You must be signed in to change notification settings - Fork 2
/
cya
executable file
·1677 lines (1398 loc) · 43.7 KB
/
cya
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
: <<HEADER
Cover Your Ass(ets) v2.4 on 14 August 2018
Copyright (c) 2018 by Jeremy O'Connell - Cyberweb Solutions, LLC @ www.cyberws.com
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
HEADER
######################################################################
# Variables
######################################################################
# Version number
VERSION="2.4"
# Title variable
TITLE="Cover Your Ass(ets)"
# Set ACTION_BANNER to off
ACTION_BANNER="off"
# Load action from commandline
action=$1
# Core backup directories
BACKUP_DIRECTORIES=(/bin/ /boot/ /etc/ /var/ /lib/ /lib32/ /lib64/ /opt/ /root/ /sbin/ /snap/ /usr/)
# Core backup files
CORE_BACKUP_FILES=(/initrd.img /initrd.img.old /vmlinuz /vmlinuz.old)
####################################################################
# Load configuration file
####################################################################
# Recover Mode
RECOVER_MODE="off"
# Max Saves
MAX_SAVES="3"
# Base dir - use for recovery mode root
BASE_DIR=""
# Get home directory
HOME_DIR="/home/cya"
# Check for restore mode
if [ ! -d "${HOME_DIR}" ]
then
RECOVER_DIR="/mnt/cya/home/cya"
# If recovery directory is detected setup environment
if [ -d "${RECOVER_DIR}" ]
then
# Reassign home directory to recovery directory, then set base dir
HOME_DIR=${RECOVER_DIR}
BASE_DIR="/mnt/cya"
# Turn on recover mode
RECOVER_MODE="on"
# Prevent certain functions
DISABLED_ACTIONS=(keep remove save script version)
for DISABLED_ACTION in "${DISABLED_ACTIONS[@]}"
do
if test "$DISABLED_ACTION" == "$action"
then
echo "☀ $TITLE v$VERSION ☀"$'\n'
echo "-> $action <- command is disabled in recovery mode!"
exit
fi
done
fi
fi
# Restore directory
RESTORE_DIR="$HOME_DIR/points"
# Archive directory
ARCHIVES_DIR="$HOME_DIR/archives"
# Load configuration file
CONFIG_FILE="$HOME_DIR/cya.conf"
# Last run file
LAST_RUN_FILE="$HOME_DIR/LAST_RUN"
# Backup files restore directory
CYA_BACKUP_FILES_DIR="cya-backup-files"
# Version timestamp file
VERSION_TS_FILE="$HOME_DIR/versionts"
# Declare CONFIG_ARRAY
declare -A CONFIG_ARRAY
# Turn disclaimer on - may be set off in config file
CONFIG_ARRAY[DISCLAIMER]="on"
# Load configuration variables - if file doesn't exist create it
if [ -f "$CONFIG_FILE" ]
then
CONFIG_FILE_CONTENTS=`cat $CONFIG_FILE`
IFS=$'\n' read -rd '' -a CONFIG_VARS <<< "$CONFIG_FILE_CONTENTS"
for CONFIG_VARS_DATA in "${CONFIG_VARS[@]}"
do
# Split data
IFS='=' read -a CONFIG_DATA <<< "${CONFIG_VARS_DATA}"
# Remove first and last quotes
CONFIG_DATA_VALUE="${CONFIG_DATA[1]//\"/}"
CONFIG_ARRAY[${CONFIG_DATA[0]}]="$CONFIG_DATA_VALUE"
done
# Update BACKUP DIRECTORIES
if [[ CONFIG_ARRAY[BACKUP_DIRECTORIES] ]]
then
IFS=' ' read -a DATA <<< "${CONFIG_ARRAY[BACKUP_DIRECTORIES]}"
BACKUP_DIRECTORIES=("${BACKUP_DIRECTORIES[@]}" "${DATA[@]}")
fi
# Update BACKUP FILES
if [[ CONFIG_ARRAY[BACKUP_FILES] ]]
then
IFS=' ' read -a DATA <<< "${CONFIG_ARRAY[BACKUP_FILES]}"
BACKUP_FILES=("${BACKUP_FILES[@]}" "${DATA[@]}")
fi
# Update MAX SAVES
if [[ CONFIG_ARRAY[MAX_SAVES] ]]
then
# If not blank
if [ -n "${CONFIG_ARRAY[MAX_SAVES]}" ]
then
# Verify number
CHK_MAX_SAVES="${CONFIG_ARRAY[MAX_SAVES]}"
re='^[0-9]+$'
if [[ "$CHK_MAX_SAVES" =~ $re ]]
then
MAX_SAVES=$CHK_MAX_SAVES
fi
fi
fi
fi
######################################################################
# Header
######################################################################
echo "☀ $TITLE v$VERSION ☀ "$'\n'
######################################################################
# Functions
######################################################################
# Footer
######################################################################
DISPLAY_FOOTER() {
echo $'\n'$'\n'"‣ Disclaimer: CYA offers zero guarantees as improper usage can cause undesired results"
echo "‣ Notice: Proper usage can correct unauthorized changes to system from attacks"$'\n'
}
# Display install package notices
######################################################################
DISPLAY_PACKAGE_INSTALL() {
echo "$1 is not on this system please install!"$'\n'
echo "* Debian/Ubuntu based: sudo apt-get install $2"
echo "* Redhat based: sudo yum install $3 OR sudo dnf install $3"
echo "* Other OSes seek instructions for installing $2"$'\n'
command_list_status="fail"
}
# Make sure sudo permissions are enabled, if not ask
######################################################################
SUDO_CHECK() {
# Make sure sudo is on
echo "Checking sudo permissions..."
SUDO_CHECK=`sudo ls`
}
# Command check - MUST BE BELOW DISPLAY_PACKAGE_INSTALL
######################################################################
COMMAND_CHECK() {
command_list_status="pass"
# Check for rsync
command -v rsync >/dev/null 2>&1 || {
DISPLAY_PACKAGE_INSTALL "rsync" "rsync" "rsync"
}
# Check for sudo
command -v sudo >/dev/null 2>&1 || {
DISPLAY_PACKAGE_INSTALL "sudo" "sudo" "sudo"
}
# Check for grep
command -v grep >/dev/null 2>&1 || {
DISPLAY_PACKAGE_INSTALL "grep" "grep" "grep"
}
# If not in recovery mode check additonal commands
if test "$RECOVER_MODE" == "off"
then
# Check for curl
command -v curl >/dev/null 2>&1 || {
DISPLAY_PACKAGE_INSTALL "curl" "curl" "curl"
}
fi
# If command_list_status is fail then exit
if test "$command_list_status" == "fail"
then
echo "PLEASE CORRECT MISSING PACKAGE(S) THEN TRY AGAIN!"$'\n'
exit
fi
# Create directories, if necessary: home, restore, restore point numbers 1-3
CHECK_DIRECTORIES=($HOME_DIR $RESTORE_DIR)
for CHECK_DIRECTORY in "${CHECK_DIRECTORIES[@]}"
do
if [ ! -d "${CHECK_DIRECTORY}" ]
then
SUDO_CHECK
echo -n "We need to create ${CHECK_DIRECTORY} ... "
bash_cli=`sudo mkdir ${CHECK_DIRECTORY}`
echo "done"
fi
done
# Create files, if necessary: config file, last run
CHECK_FILES=($CONFIG_FILE $LAST_RUN_FILE)
for CHECK_FILE in "${CHECK_FILES[@]}"
do
if [ ! -f "${CHECK_FILE}" ]
then
SUDO_CHECK
echo -n "We need to create ${CHECK_FILE} ..."
bash_cli=`sudo touch ${CHECK_FILE}`
echo "done"
fi
done
}
# Display action banner - MUST BE ABOVE INITIALIZE_DB & AFTER COMMAND_CHECK, VERSION_CHECK
######################################################################
ACTION_BANNER() {
COMMAND_CHECK
ACTION_BANNER="on"
echo "ACTION ⯮ $1"$'\n'
}
# Display error banner
######################################################################
ERROR_BANNER() {
echo "{ERROR} $1"
}
# Display success banner
######################################################################
SUCCESS_BANNER() {
echo $'\n'"🗹 SUCCESS: $1"
}
# Display failed banner
######################################################################
FAILED_BANNER() {
echo "🗵 FAILED: $1"
}
# Menu Header display
######################################################################
MENU_HEADER() {
echo $'\n'"$1:"$'\n'"------------------------------------------------------------"
}
# Menu Item display
######################################################################
MENU_ITEM() {
printf "%-18s %-2s %-2s %-2s %s\n" "$1" "" "$2" "" "$3";
}
# List Item display
######################################################################
LIST_ITEM() {
printf "\n%-40s %-5s %s\n" "$1" "" "$2";
for ((c=1; c<=80; c++))
do
echo -n "-"
done
}
# Remove ', " and trim
######################################################################
SANATIZE_VAR() {
clean_var=$1
clean_var=${clean_var//\'/}
clean_var=${clean_var//\"/}
clean_var=${clean_var//\./}
clean_var=${clean_var//\//}
clean_var=`echo $clean_var | xargs`
}
# Get current date
######################################################################
GET_DATE() {
DATE=`date +"%c"`
}
# Restore script write out
######################################################################
RESTORE_SCRIPT_INSERT() {
bash_cli=`sudo bash -c "echo '$1' >> $HOME_DIR/recovery.sh"`
}
# CYA Log write out
######################################################################
CYA_LOG_INSERT() {
if test "$RECOVER_MODE" == "on"
then
CYA_LOG_FILE="/mnt/cya/cya_log"
else
CYA_LOG_FILE="/cya_log"
fi
bash_cli=`sudo bash -c "echo '$1' >> $CYA_LOG_FILE"`
}
# SUDO call
######################################################################
SUDO_RUN () {
bash_cli=`sudo $1`
}
# SUDO Write To File
######################################################################
SUDO_WRITEOUT() {
bash_cli=`sudo bash -c "echo '$1' > $2"`
}
# Run Archive - MUST BE BEFORE RUN_RSYNC
######################################################################
RUN_ARCHIVE() {
# Run archive if backup profile exists
if [ -d "$RESTORE_DIR/$E_BACKUP_NAME" ]
then
# If archive directory doesn't exist create it
if [ ! -d "$ARCHIVES_DIR" ]
then
echo -n "Creating archives directory ... "
SUDO_RUN "mkdir $ARCHIVES_DIR"
echo "complete"
fi
# Verify filename not in use and rename if necessary
UNIXTIME=`date +"%s"`
TARGZ_FILE="$ARCHIVES_DIR/$E_BACKUP_NAME-$UNIXTIME.tar.gz"
if [ -f "$TARGZ_FILE" ]
then
TARGZ_FILE="$ARCHIVES_DIR/$E_BACKUP_NAME-$UNIXTIME.tar.gz"
fi
# Get current path
CURRENT_PATH=`pwd`
# Go to RESTORE_DIR
cd $RESTORE_DIR/
# Tar and gzip
SUDO_RUN "tar -cPvzf $TARGZ_FILE $E_BACKUP_NAME/"
# Remove backup directory
SUDO_RUN "rm -Rf $RESTORE_DIR/$E_BACKUP_NAME"
# Return to current path
cd $CURRENT_PATH
# Print archive complete
echo "$E_BACKUP_NAME has been archived to $TARGZ_FILE"
fi
}
# RSYNC Backup Run - MUST BEFORE RUN_RSYNC
######################################################################
RSYNC_BACKUP_GO() {
# Run actual backup
BACKUP_STATE="off"
# Process Directories
for BACKUP_DIRECTORY in "${BACKUP_DIRECTORIES[@]}"
do
BACKUP_DIR="n"
# Check directory exists
if [ -d "$BASE_DIR${BACKUP_DIRECTORY}" ]
then
BACKUP_DIR="y"
fi
if test "$BACKUP_DIR" == "y"
then
# Process excludes
EXCLUDE_NAME="EXCLUDE_${BACKUP_DIRECTORY}"
EXCLUDE_DIR=${CONFIG_ARRAY[$EXCLUDE_NAME]}
EXCLUDE_DIRS=""
if [[ $EXCLUDE_DIR ]]
then
# Split data
IFS=' ' read -a EXCLUDE_DATA <<< "$EXCLUDE_DIR"
# Loop to build exclude directories
for EXCLUDE_ENTRY in "${EXCLUDE_DATA[@]}"
do
# Verify directory before adding
if [ -d "${BACKUP_DIRECTORY}${EXCLUDE_ENTRY[0]}" ]
then
EXCLUDE_DIRS+="--exclude=${EXCLUDE_ENTRY[0]} "
fi
done
fi
# Set delete tag
RSYNC_DELETE="--delete"
if [ -n "$EXCLUDE_DIRS" ]
then
RSYNC_DELETE="--delete-excluded"
fi
# Run directory backup
echo -n "Backing up ${BACKUP_DIRECTORY} ... "
SUDO_RUN "rsync -a --delete-excluded $EXCLUDE_DIRS$BASE_DIR${BACKUP_DIRECTORY} $RESTORE_DIR/$BACKUP_NAME${BACKUP_DIRECTORY}"
echo " complete"
BACKUP_STATE="on"
fi
done
# Process Core Backup Files
for CORE_BACKUP_FILE in "${CORE_BACKUP_FILES[@]}"
do
BACKUP_FILE="n"
# Check file exists
if [ -f "$BASE_DIR${CORE_BACKUP_FILE}" ]
then
BACKUP_FILE="y"
fi
if test "$BACKUP_FILE" == "y"
then
# Run file backup
echo -n "Backing up ${CORE_BACKUP_FILE} ... "
SUDO_RUN "rsync -a $BASE_DIR${CORE_BACKUP_FILE} $RESTORE_DIR/$BACKUP_NAME${CORE_BACKUP_FILE}"
echo " complete"
BACKUP_STATE="on"
fi
done
# Process Files
# SETPATH must have a dot in recovery mode otherwise files won't sync right
SETPATH=""
# If recovery mode switch to directory
if test "$RECOVER_MODE" == "on"
then
CURRENT_DIR=`pwd`
cd $BASE_DIR
SETPATH="."
fi
for BACKUP_FILE in "${BACKUP_FILES[@]}"
do
BACKUP_FI="n"
# Check files exists
if [ -f "$BASE_DIR${BACKUP_FILE}" ]
then
BACKUP_FI="y"
fi
if test "$BACKUP_FI" == "y"
then
# Run file backup
echo -n "Backing up ${BACKUP_FILE} ... "
SUDO_RUN "rsync -a --relative $SETPATH${BACKUP_FILE} $RESTORE_DIR/$BACKUP_NAME/$CYA_BACKUP_FILES_DIR"
echo " complete"
BACKUP_STATE="on"
fi
done
# If recovery mode switch back to original directory
if test "$RECOVER_MODE" == "on"
then
cd $CURRENT_DIR
fi
# If backup successful write out data file
if test $BACKUP_STATE == "on"
then
GET_DATE
echo -n "Write out date file ... "
SUDO_WRITEOUT "$DATE" "$RESTORE_DIR/$BACKUP_NAME/cya-date"
echo "complete"
fi
}
# Rsync
######################################################################
RUN_RSYNC() {
SYNC_STATE=$1
BACKUP_NAME=$2
if [ -z "$BACKUP_NAME" ]
then
FAILED_BANNER "No backup name provided"
else
SUDO_CHECK
# RSYNC Restore
if test $SYNC_STATE == "restore"
then
BACKUP_NAME="$2"
BACKUP_BEFORE="$3"
DELETE_FILES="$4"
PROMPT_EACH="$5"
# If cya_log exists remove
if [ -d "$RESTORE_DIR/$BACKUP_NAME" ]
then
if test "$RECOVER_MODE" == "on"
then
CYA_LOG_FILE="/mnt/cya/cya_log"
else
CYA_LOG_FILE="/cya_log"
fi
# Remove cya_log if necessary
if [ -f "$CYA_LOG_FILE" ]
then
echo -n "Removing $CYA_LOG_FILE ... "
SUDO_RUN "rm $CYA_LOG_FILE"
echo "complete"
fi
fi
# Set date
GET_DATE
CYA_LOG_INSERT "$DATE"
CYA_LOG_INSERT "--------------------------------------------------------"
CYA_LOG_INSERT "Backup Profile Used: $BACKUP_NAME"
# Check backup directory exists
if [ -d "$RESTORE_DIR/$BACKUP_NAME" ]
then
# Run backup before restore, if set
if test $BACKUP_BEFORE == "y"
then
# Temp hold backup name and generate current state name
BACKUP_NAME_HOLD="$BACKUP_NAME"
UNIXTIME=`date +"%s"`
BACKUP_NAME="RESTORE-$UNIXTIME"
# Create backup directory, if necessary
if [ ! -d "$RESTORE_DIR/$BACKUP_NAME" ]
then
CYA_LOG="Creating current state name $RESTORE_DIR/$BACKUP_NAME ... "
echo -n "$CYA_LOG"
CYA_LOG_INSERT "$CYA_LOG"
SUDO_RUN "mkdir $RESTORE_DIR/$BACKUP_NAME"
CYA_LOG="complete"
echo "$CYA_LOG"
CYA_LOG_INSERT "$CYA_LOG"
fi
CYA_LOG="Backing up current state... "
echo "$CYA_LOG"
CYA_LOG_INSERT "$CYA_LOG"
RSYNC_BACKUP_GO
CYA_LOG="Current state back up complete"
echo "$CYA_LOG"
CYA_LOG_INSERT "$CYA_LOG"
BACKUP_NAME="$BACKUP_NAME_HOLD"
fi
# Set delete flag
DELETE_FLAG=""
if test $DELETE_FILES == "y"
then
DELETE_FLAG=" --delete"
CYA_LOG="Delete files NOT in backup is on"
echo "$CYA_LOG"
CYA_LOG_INSERT "$CYA_LOG"
fi
# Restore Status
RESTORE_STATUS="n"
# Now restore directories
for BACKUP_DIRECTORY in "${BACKUP_DIRECTORIES[@]}"
do
# Check to see if directory exists in backup
if [ -d "$RESTORE_DIR/$BACKUP_NAME${BACKUP_DIRECTORY}" ]
then
# Set restore flag
RESTORE_DO="y"
# If necessary confirm directory restore
if test $PROMPT_EACH == "y"
then
echo -n "Restore ${BACKUP_DIRECTORY}? [y/n] "
read RESTORE_DO
if [ -z "$RESTORE_DO" ]
then
RESTORE_DO="n"
fi
if test "$RESTORE_DO" != "y"
then
echo "Skipping ${BACKUP_DIRECTORY}"
fi
fi
# Run restore
if test $RESTORE_DO == "y"
then
# Process excludes
EXCLUDE_NAME="EXCLUDE_${BACKUP_DIRECTORY}"
EXCLUDE_DIR=${CONFIG_ARRAY[$EXCLUDE_NAME]}
EXCLUDE_DIRS=""
if [[ $EXCLUDE_DIR ]]
then
# Split data
IFS=' ' read -a EXCLUDE_DATA <<< "$EXCLUDE_DIR"
# Loop to build exclude directories
for EXCLUDE_ENTRY in "${EXCLUDE_DATA[@]}"
do
# Verify directory before adding
if [ -d "${BACKUP_DIRECTORY}${EXCLUDE_ENTRY[0]}" ]
then
EXCLUDE_DIRS+="--exclude=${EXCLUDE_ENTRY[0]} "
fi
done
fi
CYA_LOG="Restoring ${BACKUP_DIRECTORY} ... "
echo -n "$CYA_LOG"
# Run directory rsync
SUDO_RUN "rsync -a$DELETE_FLAG $EXCLUDE_DIRS$RESTORE_DIR/$BACKUP_NAME${BACKUP_DIRECTORY} $BASE_DIR${BACKUP_DIRECTORY}"
CYA_LOG="complete"
echo "$CYA_LOG"
CYA_LOG_INSERT "Restoring ${BACKUP_DIRECTORY} ... complete"
# If directory is boot restore core_backup_files
if test "${BACKUP_DIRECTORY}" == "/boot/"
then
# Process Core Backup Files
for CORE_BACKUP_FILE in "${CORE_BACKUP_FILES[@]}"
do
BACKUP_FILE="n"
# Check file exists
if [ -f "$RESTORE_DIR/$BACKUP_NAME${CORE_BACKUP_FILE}" ]
then
BACKUP_FILE="y"
fi
if test "$BACKUP_FILE" == "y"
then
# Run restore file
CYA_LOG="Restoring ${CORE_BACKUP_FILE} ... "
echo -n "$CYA_LOG"
# Run file rsync
SUDO_RUN "rsync -a $RESTORE_DIR/$BACKUP_NAME${CORE_BACKUP_FILE} $BASE_DIR${CORE_BACKUP_FILE}"
echo " complete"
CYA_LOG_INSERT "Restoring ${CORE_BACKUP_FILE} ... complete"
fi
done
fi
# Set status
RESTORE_STATUS="y"
fi
fi
done
# Now restore files
# Check to see if directory exists in backup
if [ -d "$RESTORE_DIR/$BACKUP_NAME/$CYA_BACKUP_FILES_DIR" ]
then
# Set restore flag
RESTORE_DO="y"
# If necessary confirm directory restore
if test $PROMPT_EACH == "y"
then
echo -n "Restore backup files? [y/n] "
read RESTORE_DO
if [ -z "$RESTORE_DO" ]
then
RESTORE_DO="n"
fi
if test "$RESTORE_DO" != "y"
then
echo "Skipping backup files"
fi
fi
# Run restore
if test $RESTORE_DO == "y"
then
CYA_LOG="Restoring backup files ... "
echo -n "$CYA_LOG"
# Run file rsync
SUDO_RUN "rsync -a $RESTORE_DIR/$BACKUP_NAME/$CYA_BACKUP_FILES_DIR/ $BASE_DIR"
CYA_LOG="complete"
echo "$CYA_LOG"
CYA_LOG_INSERT "Restoring backup files ... complete"
# Get file list
for BACKUP_FILE in "${BACKUP_FILES[@]}"
do
if [ -f "$RESTORE_DIR/$BACKUP_NAME/$CYA_BACKUP_FILES_DIR/${BACKUP_FILE}" ]
then
CYA_LOG="Restored ${BACKUP_FILE}"
echo "$CYA_LOG"
CYA_LOG_INSERT "$CYA_LOG"
fi
done
# Set status
RESTORE_STATUS="y"
fi
fi
# Print reboot if restore was done
if test $RESTORE_STATUS == "y"
then
echo "$base_dir/cya_log has been written"
echo $'\n'"🟂 Now restart your machine to load your environment with the restored files! 🟂"
fi
else
FAILED_BANNER "Backup directory doesn't exist!"
fi
# RSYNC Backup
else
# Create backup directory, if necessary
if [ ! -d "$RESTORE_DIR/$BACKUP_NAME" ]
then
echo -n "We need to create $RESTORE_DIR/$BACKUP_NAME ... "
SUDO_RUN "mkdir $RESTORE_DIR/$BACKUP_NAME"
echo "done"
fi
RSYNC_BACKUP_GO
# If standard backup update current number
if test $3 == "standard"
then
echo -n "Update rotation file ... "
SUDO_WRITEOUT "$4" "$LAST_RUN_FILE"
echo "complete"
fi
# If archive is on
if test $3 == "manual"
then
if test $4 == "archive"
then
E_BACKUP_NAME=$BACKUP_NAME
RUN_ARCHIVE "$E_BACKUP_NAME"
fi
fi
fi
# End sync type
fi
}
# List Backups
######################################################################
LIST_BACKUPS() {
LIST_ITEM "Backup Name" "Date & Time"
# Get current LAST_RUN
LAST_RUN_CNT=`cat $LAST_RUN_FILE`
# Load directory list into array
DIRECTORIES=($(ls -1 $RESTORE_DIR))
# Loop through and build list
for DIRECTORY in "${DIRECTORIES[@]}"
do
if [[ "${DIRECTORY}" != "." ]] && [[ "${DIRECTORY}" != ".." ]]
then
# Grab date
if [ -f "$RESTORE_DIR/${DIRECTORY}/cya-date" ]
then
DATESTAMP=`cat $RESTORE_DIR/${DIRECTORY}/cya-date`
else
DATESTAMP='none'
fi
# Check for current save
LAST_RUN_MATCH=""
if test "$LAST_RUN_CNT" == "${DIRECTORY}"
then
LAST_RUN_MATCH=" <-- LAST RUN"
fi
# Display list
LIST_ITEM "${DIRECTORY}$LAST_RUN_MATCH" "$DATESTAMP"
fi
done
}
# Display Version Reminder
######################################################################
VERSION_CHECK() {
# Get current timestamp
curts=`date +%s`
# Calculate 60 days ago in unixtime
olduts=$(($curts-5184000))
# Write version timestamp file, if missing
if [ -f "$VERSION_TS_FILE" ]
then
fileuts=`cat $VERSION_TS_FILE`
else
BASH_RUN=`echo "$curts" | sudo tee $VERSION_TS_FILE`
fileuts="$curts"
fi
# Set fileuts to zero if blank
if [ -z "$fileuts" ]
then
fileuts="0"
fi
# Compare filets with olduts and display message if necessary
if (($olduts > $fileuts))
then
echo "Your version could be out of date! You haven't checked in 60 days."
echo -n "Would you like to check for a new version? [Yn] "
read CHKVERSION
# Update version reminder timestamp file
BASH_RUN=`echo "$curts" | sudo tee $VERSION_TS_FILE`
echo $'\n'"-> Version reminder check updated to remind in 60 days"
if test "$CHKVERSION" == "n"
then
echo "Skipping version check, but highly recommended!"
else
echo "Starting version check..."
action="version"
fi
fi
}
######################################################################
# Version Reminder Check
######################################################################
if test "$RECOVER_MODE" == "off"
then
VERSION_CHECK
fi
######################################################################
# Actions
######################################################################
# List Backups
######################################################################
if test "$action" == "list"
then
ACTION_BANNER "List Backups"
LIST_BACKUPS
fi
# Restore
######################################################################
if test "$action" == "restore"
then
ACTION_BANNER "Restore"
# Only run in restore mode
if test "$RECOVER_MODE" != "on"
then
FAILED_BANNER "restore command doesn't work when your system is booted off the installed OS. Please boot off a live image and try again."
exit
fi
# Check with user
echo "(((WARNING))) You are about to begin the restore process which WILL alter the system's operating files!!! It is best practice to restore a system from a live boot environment using a CD, USB, or network image."$'\n'
echo -n "Are you sure you want to continue this process? [y/N] "
read CONTINUE_STATUS
if [ -z $CONTINUE_STATUS ]
then
CONTINUE_STATUS="n"
fi
# Process answer
if test $CONTINUE_STATUS == "y"
then
echo -n $'\n'"First pick the name of a backup to restore. Press any key to continue... "
read CONTINUE_STATUSB
# Choose the backup to use for restoring
LIST_BACKUPS
echo $'\n'
for ((c=1; c<=20; c++))
do
echo -n "Enter backup name to restore: "
read BACKUP_NAME
SANATIZE_VAR $BACKUP_NAME
BACKUP_NAME="$clean_var"
if [ -d "$RESTORE_DIR/$BACKUP_NAME" ]
then
E_BACKUP_NAME="$BACKUP_NAME"
break
else
FAILED_BANNER "Invalid name!"
fi
done
# If no backup name abort
if [ -z $E_BACKUP_NAME ]
then
echo "Aborting... no valid backup name provided"
else
# Backup current state
echo $'\n'"Do you want to take a backup of current state before restoring?"
echo $'\n'"Enter your choice: [y/N] "
read BACKUP_BEFORE
if [ -z $BACKUP_BEFORE ]
then
BACKUP_BEFORE="n"
fi
SANATIZE_VAR $BACKUP_BEFORE
BACKUP_BEFORE="$clean_var"
# Delete files
echo $'\n'"Should files that do NOT exist in backup be deleted from destination?"
echo $'\n'"This will remove unauthorized files in restored directories. However excluded directories and their files will not be removed, even if you chose yes."
echo $'\n'"Enter your choice: [y/N] "
read DELETE_FILES
if [ -z $DELETE_FILES ]
then
DELETE_FILES="n"
fi
SANATIZE_VAR $DELETE_FILES
DELETE_FILES="$clean_var"
# Verify each directory
echo $'\n'"Do you want to be asked whether to restore each directory individually?"
echo $'\n'"Enter your choice: [y/N] "
read PROMPT_EACH
if [ -z $PROMPT_EACH ]
then
PROMPT_EACH="n"
fi
# Verify restore actions
echo $'\n'"======================================================================"
echo "FILE CHANGES ABOUT TO OCCUR! VERIFY AND CONFIRM:"
echo "======================================================================"$'\n'
echo "Restoring Backup Profile: $E_BACKUP_NAME"
echo -n "Backing Up Current State: "
if test $BACKUP_BEFORE == "y"
then
echo "Yes"
else
echo "No"
BACKUP_BEFORE="n"
fi
echo -n "Delete Files Not Found In Backup: "
if test $DELETE_FILES == "y"
then
echo "Yes"
else