home · contact · privacy
Ensure we don't get asked for SSH passphrase repeatedly in borg script.
[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     false
14 }
15
16 read_pw() {
17     eval $(ssh-agent)
18     ssh-add
19     stty -echo
20     printf "Passphrase: "
21     read password
22     stty echo
23     printf "\n"
24     export BORG_PASSPHRASE="${password}"
25 }
26
27 if [ ! -f "${config_file}" ]; then
28     echo '# file read ends at last newline' >> "${config_file}"
29 fi
30 if [ "$#" -lt 1 ]; then
31     usage
32 fi
33 first_arg="$1"
34 shift
35 if [ "${first_arg}" = "init" ]; then
36     if [ ! "$#" -eq 1 ]; then
37         echo "Need exactly one argument: target of form user@server"
38         false
39     fi
40     target="$1"
41     echo "Initializing: ${target}"
42     borg init --verbose --encryption=keyfile "${target}:${standard_repo}"
43     tmp_file="/tmp/new_borgrepos"
44     echo "${target}" > "${tmp_file}"
45     cat "${config_file}" >> "${tmp_file}"
46     cp "${tmp_file}" "${config_file}"
47 elif [ "${first_arg}" = "store" ]; then
48     if [ ! "$#" -eq 2 ]; then
49         echo "Need precisely two arguments: archive name and path to archive."
50         false
51     fi
52     archive_name=$1
53     shift
54     to_backup="$@"
55     read_pw
56     cat "${config_file}" | while read line; do
57         first_char=$(echo "${line}" | cut -c1)
58         if [ "${first_char}" = "#" ]; then
59             continue
60         fi
61         repo="${line}:${standard_repo}"
62         archive="${repo}::${archive_name}-{utcnow:%Y-%m-%dT%H:%M}"
63         echo "Creating archive: ${archive}"
64         borg create --verbose --list "${archive}" "${to_backup}"
65     done
66 elif [ "${first_arg}" = "check" ]; then
67     if [ ! "$#" -eq 0 ]; then
68         echo "Need no arguments"
69         false
70     fi
71     read_pw
72     cat "${config_file}" | while read line; do
73         first_char=$(echo "${line}" | cut -c1)
74         if [ "${first_char}" = "#" ]; then
75             continue
76         fi
77         repo="${line}:${standard_repo}"
78         echo "Checking repo: ${repo}"
79         borg check --verbose "${repo}"
80     done
81 elif [ "${first_arg}" = "export_keyfiles" ]; then
82     if [ ! "$#" -eq 1 ]; then
83         echo "Need output tar file name."
84         false
85     fi
86     tar_target="${1}"
87     tmp_dir="${HOME}/.borgtmp"
88     keyfiles_dir="${tmp_dir}/borg_keyfiles"
89     mkdir -p "${keyfiles_dir}"
90     cat "${config_file}" | while read line; do
91         first_char=$(echo "${line}" | cut -c1)
92         if [ "${first_char}" = "#" ]; then
93             continue
94         fi
95         repo="${line}:${standard_repo}"
96         borg key export "${repo}" "${keyfiles_dir}/${line}"
97     done
98     cur_dir="$(pwd)"
99     cd "${tmp_dir}"
100     target=$(basename "${keyfiles_dir}")
101     tar cf "${tar_target}" "${target}"
102     mv "${tar_target}" "${cur_dir}"
103     cd
104     rm -rf "${tmp_dir}"
105 else
106     usage
107 fi