-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore
executable file
·109 lines (91 loc) · 3.49 KB
/
restore
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
#!/bin/bash
################################
# UNIXycle - A UNIX recycle bin
################################
# Author: Elliott Steer
# Docs: https://github.com/essteer/unixycle/blob/main/README.md
#
# `restore` retrieves files from a recyclebin directory in a
# user's $HOME that were previously recycled via the `recycle`
# script, and recreates parent directories that no longer exist.
#
# File usage:
# "$ bash restore file_to_restore.txt_64135"
#
# Data for recycled files is stored in $HOME/.restore.info.
# See the accompanying `recycle` script for details.
#################################
log_filepath="logs/restore.log"
recyclebin_abs_path="$HOME/recyclebin"
restore_info_abs_path="$HOME/.restore.info"
function timestamp() {
date +"%Y-%m-%d %H:%M:%S"
}
if [[ ! -f "${log_filepath}" ]] ; then
mkdir -p logs
touch "${log_filepath}"
echo "$(timestamp) - INFO - restore - Log file created at ${log_filepath}" >> "${log_filepath}"
fi
if [ "$#" -eq 0 ] ; then
echo "$(timestamp) - WARNING - restore - Missing operand: no file specified" | tee -a "${log_filepath}"
echo "Usage: bash $(basename $0) [FILE]"
exit 1 # exit if no args provided
fi
function show_help() {
echo "Usage: bash restore [FILE]"
echo
echo "Options:"
echo " -h Display help"
}
OPTIND=1
while getopts ":h" opt
do
case $opt in
h|H)
show_help
exit 0
;;
esac
done
recycle_state_name="$(basename "$1")"
target_abs_path_in_recyclebin="${recyclebin_abs_path}/${recycle_state_name}"
if ! [ -e "$target_abs_path_in_recyclebin" ] ; then
echo "$(timestamp) - WARNING - restore - Cannot restore '$1': no such file" | tee -a "${log_filepath}"
exit 2 # exit if file does not exist
fi
data_in_restore_info=$(cat "$restore_info_abs_path" | grep "$recycle_state_name")
target_original_abs_path=$(echo -n "$data_in_restore_info" | cut -d: -f2)
if [ -e "$target_original_abs_path" ] ; then
echo "$target_original_abs_path already exists"
read -p "Do you want to overwrite? y/n " overwriteDecision
if [ "$overwriteDecision" != "y" ] && [ "$overwriteDecision" != "Y" ] ; then
exit 3 # user declined file overwrite
fi
fi
# create missing parent dirs for target file original location
target_original_dir=$(dirname "$target_original_abs_path")
mkdir -p "$target_original_dir"
if ! [ -e "${target_original_dir}" ] ; then
echo "$(timestamp) - ERROR - restore - Failed to restore missing parent directories for ${recycle_state_name}" | tee -a "${log_filepath}"
exit 4
fi
# restore target file from recyclebin to original location
mv "$target_abs_path_in_recyclebin" "$target_original_abs_path"
if [ $? -eq 0 ] ; then
echo "$(timestamp) - INFO - restore - File restored to ${target_original_abs_path}" >> "${log_filepath}"
echo "File restored to ${target_original_abs_path}"
else
echo "$(timestamp) - ERROR - restore - Failed to restore ${restore_state_name}" | tee -a "${log_filepath}"
exit 5
fi
# delete data on target file from .restore.info
grep -v "$data_in_restore_info" "$restore_info_abs_path" > tmp
mv tmp "$restore_info_abs_path" # overwrite .restore.info with tmp file from grep -v
if [ $? -eq 0 ] ; then
echo "$(timestamp) - INFO - restore - Entry removed from .restore.info: ${data_in_restore_info}" >> "${log_filepath}"
echo "Entry removed from .restore.info"
else
echo "$(timestamp) - ERROR - restore - Failed to remove entry from .restore.info: ${data_in_restore_info}" | tee -a "${log_filepath}"
exit 6
fi
exit 0