-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_util_file-deploy
More file actions
executable file
·134 lines (107 loc) · 5.07 KB
/
ft_util_file-deploy
File metadata and controls
executable file
·134 lines (107 loc) · 5.07 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
#!/bin/bash
source "$(dirname "$0"})/ft_util_inc_var"
if [ -z "$1" ] || [ -z "$2" ]; then
$S_LOG -s crit -d $S_NAME "Needed variable path_src and path_tgt are missing. Cannot do file deploy! path_src=$1 path_tgt=$2 file_deploy_opts=$3"
exit 1
fi
#############################
## Functions
#############################
function backup_file() {
if [ -f "$*.${bkp_suffix}" ]; then
$S_LOG -s debug -d "${S_NAME}|${path_src_nice}" "Backup skipped of $* > $*.${bkp_suffix}"
elif [ ! -f "$*" ]; then
$S_LOG -s debug -d "${S_NAME}|${path_src_nice}" "Backup skipped of $* (new file)"
else
cp --preserve=all "$*" "$*.${bkp_suffix}"
$S_LOG -s debug -d "${S_NAME}|${path_src_nice}" "Backup done of $* > $*.${bkp_suffix}"
fi
}
export -f backup_file
function compare_file() {
if [ -f "$1" ]; then
diff --unified=0 "$1" "$2"
else
echo "no diff on new file: ${2}"
fi
}
export -f compare_file
#############################
## Variables
#############################
path_src=$1
path_src_nice="${path_src/#$(dirname "${S_DIR_PATH}")\//}" # this will shorten the path_src string in order to create less text in logs (note, it will only shorten if the path is in the same ../ dir as this script)
path_tgt=$2
# Remove trailing slash
path_src=${path_src%/}
path_tgt=${path_tgt%/}
# Grab options
if [[ "${3}" == *"NO-BACKUP"* ]]; then opt_backup=false; else opt_backup=true; fi
if [[ "${3}" == *"NO-COMPARE"* ]]; then opt_compare=false; else opt_compare=true; fi
# Backup suffix is the start date of the parent PID... which is gonna be the same if you use this script several time, hence you can do compare...
# Exemple: in a script you deploy the folder bin then a file 123.sh inside of bin... on the next run 123.sh will be deleted when you redeploy bin files... but will be redeployed AND compared because the backup prefix will be the same
ppid_age_date=$(ps -olstart= $PPID) # Get start date of parent PID
ppid_age_epoch=$(date +%s -d "${ppid_age_date}") # Convert start date to epoch date
bkp_suffix=$(date +"%Y%m%d-%H%M%S" -d@${ppid_age_epoch}).bak
$S_LOG -s debug -d "${S_NAME}|${path_src_nice}" "bkp_suffix=${bkp_suffix}"
#############################
## Deploy file(s)
#############################
returncode=255
if [ -d "${path_src}" ]; then
# Create destination dir
if [ -d "${path_tgt}" ]; then
mkdir -p "$path_tgt"
$S_LOG -s $? -d "${S_NAME}|${path_src_nice}" "Creating $path_tgt returned EXIT_CODE=$?"
fi
# Backup files
if [ "$opt_backup" = true ]; then
find "$path_tgt/" -type f ! -name '*.bak' | while read file; do backup_file "$file"; done
fi
# Delete all the files inside the destination dir (excluding bak)
find "$path_tgt/" -type f ! -name '*.bak' -delete
$S_LOG -s debug -d "${S_NAME}|${path_src_nice}" "Delete existing files returned EXIT_CODE=$?"
# Copy dir
cp -R "$path_src/." "$path_tgt/"
returncode=$?
$S_LOG -s debug -d "${S_NAME}|${path_src_nice}" "Copy files returned EXIT_CODE=$returncode"
# Apply pre-existing folder owner and group to files in folder (because cp does not respect it)
saved_owner="$(stat -c '%u:%g' "$path_tgt")"
chown -R "$saved_owner" "$path_tgt"
$S_LOG -s debug -d "${S_NAME}|${path_src_nice}" "Chown files [$saved_owner] returned EXIT_CODE=$?"
# Compare
if [ "$opt_backup" = true ]; then
if [ "$opt_compare" = true ]; then
find "$path_src/" -type f ! -name '*.bak' -exec bash -c "p=\"{}\" ; p=\"$path_tgt\${p#$path_src}\" ; compare_file \"\${p}.${bkp_suffix}\" \"\${p}\"" \; | $S_LOG -s warn -d "${S_NAME}|${path_src_nice}|diff" -i
fi
$S_DIR_PATH/ft_util_bak-cleaner "$path_tgt/" # Cleanup backup files
fi
else
# Create parent dir if missing
path_tgt_d=$(dirname "$path_tgt")
[ -d "${path_tgt_d}" ] || mkdir -p "$path_tgt_d"
if [ "$opt_backup" = true ]; then backup_file ${path_tgt}; fi # Backup file
# Save ACL to variable (empty if no ACL)
saved_acl="$(getfacl -p "$path_tgt" 2>/dev/null)"
# Copy file
cp "${path_src}" "${path_tgt}"
returncode=$? # store the exit code of the command
# Restore ACL only if variable is non-empty
if [ -n "$saved_acl" ]; then
printf '%s\n' "$saved_acl" | setfacl --restore=-
$S_LOG -s debug -d "${S_NAME}|${path_src_nice}" "ACL restore returned EXIT_CODE=$?"
else
$S_LOG -s debug -d "${S_NAME}|${path_src_nice}" "No ACL to restore. Applying Ownership from parent folder."
saved_owner="$(stat -c '%u:%g' "$path_tgt_d")"
chown "$saved_owner" "$path_tgt"
$S_LOG -s debug -d "${S_NAME}|${path_src_nice}" "Chown file [$saved_owner] returned EXIT_CODE=$?"
fi
# Compare
if [ "$opt_backup" = true ]; then
if [ "$opt_compare" = true ]; then
compare_file "${path_tgt}.${bkp_suffix}" "${path_tgt}" | $S_LOG -s warn -d "${S_NAME}|${path_src_nice}|diff" -i
fi
$S_DIR_PATH/ft_util_bak-cleaner "${path_tgt}" # Cleanup backup files
fi
fi
$S_LOG -s $returncode -d "${S_NAME}|${path_src_nice}" "Transfer returned EXIT_CODE=$returncode"