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