-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup-dot-envs.sh
executable file
·105 lines (85 loc) · 2.59 KB
/
backup-dot-envs.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
102
103
104
105
#!/bin/bash
if [ -z $DEBUG ]; then
DEBUG=0
fi
# Figure out the directory the current script lives in. We'll use it later to determine default file paths for other stuff.
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
# Set up defaults
target_dir="${HOME}/Archive/backups/dot-env"
base_source_dir="${HOME}/src"
verbose=0
# Process Options
function usage () {
echo "Usage:"
echo " $( basename "${0}" ) [options]"
echo
echo " Options include: "
echo " -h Display this help message."
echo " -s source_dir Base source dir under which to search for .env files. Defaults to `${base_source_dir}`."
echo " -t target_dir Place generated file in the specified directory. Defaults to `${target_dir}`."
echo " -v Be verbose."
}
while getopts ":hvt:" opt; do
case ${opt} in
h)
usage
exit 0
;;
s)
base_source_dir="$( cd "$OPTARG" && pwd )"
;;
t)
target_dir="$( cd "$OPTARG" && pwd )"
;;
v)
echo "Increasing verbosity"
verbose=1
;;
\? )
echo "Invalid Option: -$OPTARG" 1>&2
exit 1
;;
esac
done
shift $((OPTIND -1))
#
if [ $verbose -gt 0 -o $DEBUG -ne 0 ]; then
echo "What am I?: ${0}"
echo "Verbose: ${verbose}"
echo "DIR: ${DIR}"
echo "Target dir: ${target_dir}"
echo "Base Source dir: ${base_source_dir}"
fi
echo "Backing up .env files in ${base_source_dir} ... "
## Get it done
files=`find ${base_source_dir} -name .env`
date=`date +%Y%m%d%H%M%S`
dest_dir_bydate="${target_dir}/by_date/${date}"
if [ ! -d "${dest_dir_bydate}" ]; then
if [ $verbose -gt 0 -o $DEBUG -ne 0 ]; then
echo "Date-based destination directory missing. Creating ${dest_dir_bydate} ..."
fi
mkdir -p "${dest_dir_bydate}"
fi
for file in $files; do
dest_file="${file/${base_source_dir}/${target_dir}}"
dest_dir="$( dirname "$dest_file" )"
dest_file="${dest_dir}/env.${date}"
dest_file_bydate="${dest_dir##${target_dir}}.env"
dest_file_bydate="${dest_file_bydate//\//-}"
dest_file_bydate="${dest_dir_bydate}/${dest_file_bydate#-}"
if [ ! -d "${dest_dir}" ]; then
if [ $verbose -gt 0 -o $DEBUG -ne 0 ]; then
echo "Destination directory missing. Creating ${dest_dir} ..."
fi
mkdir -p "${dest_dir}"
fi
cp -v "${file}" "${dest_file}"
ln -s "${dest_file}" "${dest_file_bydate}"
done