home · contact · privacy
Better passphrase management.
[config] / all_new_2018 / borg.sh
1 #!/bin/sh
2 set -e
3
4 standard_repo="borg"
5 config_file="${HOME}/.borgrepos"
6
7 usage() {
8     echo "Need operation as argument, one of:"
9     echo "init"
10     echo "store"
11     echo "check"
12     echo "export_keyfiles"
13     echo "orgpush"
14     echo "orgpull"
15     false
16 }
17
18 read_pw() {
19     eval $(ssh-agent)
20     echo "ssh-add"
21     ssh-add
22     if [ "${#BORG_PASSPHRASE}" -eq 0 ]; then
23         stty -echo
24         printf "Borg passphrase: "
25         read password
26         stty echo
27         printf "\n"
28         export BORG_PASSPHRASE="${password}"
29     fi
30 }
31
32 if [ ! -f "${config_file}" ]; then
33     echo '# file read ends at last newline' >> "${config_file}"
34 fi
35 if [ "$#" -lt 1 ]; then
36     usage
37 fi
38 first_arg="$1"
39 shift
40 if [ "${first_arg}" = "init" ]; then
41     if [ ! "$#" -eq 1 ]; then
42         echo "Need exactly one argument: target of form user@server"
43         false
44     fi
45     target="$1"
46     echo "Initializing: ${target}"
47     borg init --verbose --encryption=keyfile "${target}:${standard_repo}"
48     tmp_file="/tmp/new_borgrepos"
49     echo "${target}" > "${tmp_file}"
50     cat "${config_file}" >> "${tmp_file}"
51     cp "${tmp_file}" "${config_file}"
52 elif [ "${first_arg}" = "store" ]; then
53     if [ ! "$#" -eq 2 ]; then
54         echo "Need precisely two arguments: archive name and path to archive."
55         false
56     fi
57     archive_name=$1
58     shift
59     to_backup="$@"
60     read_pw
61     cat "${config_file}" | while read line; do
62         first_char=$(echo "${line}" | cut -c1)
63         if [ "${first_char}" = "#" ]; then
64             continue
65         fi
66         repo="${line}:${standard_repo}"
67         archive="${repo}::${archive_name}-{utcnow:%Y-%m-%dT%H:%M}"
68         echo "Creating archive: ${archive}"
69         borg create --verbose --list "${archive}" "${to_backup}"
70     done
71 elif [ "${first_arg}" = "check" ]; then
72     if [ ! "$#" -eq 0 ]; then
73         echo "Need no arguments"
74         false
75     fi
76     read_pw
77     cat "${config_file}" | while read line; do
78         first_char=$(echo "${line}" | cut -c1)
79         if [ "${first_char}" = "#" ]; then
80             continue
81         fi
82         repo="${line}:${standard_repo}"
83         echo "Checking repo: ${repo}"
84         borg check --verbose "${repo}"
85     done
86 elif [ "${first_arg}" = "export_keyfiles" ]; then
87     if [ ! "$#" -eq 1 ]; then
88         echo "Need output tar file name."
89         false
90     fi
91     tar_target="${1}"
92     tmp_dir="${HOME}/.borgtmp"
93     keyfiles_dir="${tmp_dir}/borg_keyfiles"
94     mkdir -p "${keyfiles_dir}"
95     cat "${config_file}" | while read line; do
96         first_char=$(echo "${line}" | cut -c1)
97         if [ "${first_char}" = "#" ]; then
98             continue
99         fi
100         repo="${line}:${standard_repo}"
101         borg key export "${repo}" "${keyfiles_dir}/${line}"
102     done
103     cur_dir="$(pwd)"
104     cd "${tmp_dir}"
105     target=$(basename "${keyfiles_dir}")
106     tar cf "${tar_target}" "${target}"
107     mv "${tar_target}" "${cur_dir}"
108     cd
109     rm -rf "${tmp_dir}"
110 elif [ "${first_arg}" = "orgpush" ]; then
111     archive_name="orgdir"
112     to_backup=~/org
113     read_pw
114     cat "${config_file}" | while read line; do
115         first_char=$(echo "${line}" | cut -c1)
116         if [ "${first_char}" = "#" ]; then
117             continue
118         fi
119         repo="${line}:${standard_repo}"
120         archive="${repo}::${archive_name}-{utcnow:%Y-%m-%dT%H:%M}"
121         echo "Creating archive: ${archive}"
122         borg create --verbose --list "${archive}" "${to_backup}" --exclude ~/org/.git
123     done
124 elif [ "${first_arg}" = "orgpull" ]; then
125     archive_name="orgdir"
126     read_pw
127     cd /
128     cat "${config_file}" | while read line; do
129         first_char=$(echo "${line}" | cut -c1)
130         if [ "${first_char}" = "#" ]; then
131             continue
132         fi
133         repo="${line}:${standard_repo}"
134         archive=$(borg list "${repo}" | grep "${orgdir}" | tail -1 | cut -f1 -d' ')
135         echo "Pulling archive: ${archive}"
136         borg extract --verbose "${repo}::${archive}"
137         break
138     done
139 else
140     usage
141 fi