home · contact · privacy
759779564452f9b80a82ba9df9cfbe72ed16a586
[config] / all_new_2018 / borg.sh
1 #!/bin/sh
2 set -e
3 set -x
4
5 standard_repo="borg"
6 config_file="${HOME}/.borgrepos"
7
8 usage() {
9     echo "Need operation as argument, one of:"
10     echo "init"
11     echo "store"
12     echo "check"
13     echo "export_keyfiles"
14     false
15 }
16
17 read_pw() {
18     stty -echo
19     printf "Passphrase: "
20     read password
21     stty echo
22     printf "\n"
23     export BORG_PASSPHRASE="${password}"
24 }
25
26 if [ ! -f "${config_file}" ]; then
27     echo '# file read ends at last newline' >> "${config_file}"
28 fi
29 if [ "$#" -lt 1 ]; then
30     usage
31 fi
32 first_arg="$1"
33 shift
34 if [ "${first_arg}" = "init" ]; then
35     if [ ! "$#" -eq 1 ]; then
36         echo "Need exactly one argument: target of form user@server"
37         false
38     fi
39     target="$1"
40     echo "Initializing: ${target}"
41     borg init --verbose --encryption=keyfile "${target}:${standard_repo}"
42     tmp_file="/tmp/new_borgrepos"
43     echo "${target}" > "${tmp_file}"
44     cat "${config_file}" >> "${tmp_file}"
45     cp "${tmp_file}" "${config_file}"
46 elif [ "${first_arg}" = "store" ]; then
47     if [ "$#" -lt 2 ]; then
48         echo "Need archive name and paths to archive."
49         false
50     fi
51     archive_name=$1
52     shift
53     to_backup="$@"
54     read_pw
55     cat "${config_file}" | while read line; do
56         first_char=$(echo "${line}" | cut -c1)
57         if [ "${first_char}" = "#" ]; then
58             continue
59         fi
60         repo="${line}:${standard_repo}"
61         archive="${repo}::${archive_name}"
62         echo "Creating archive: ${archive}"
63         borg create --verbose --list "${archive}" ${to_backup}
64     done
65 elif [ "${first_arg}" = "check" ]; then
66     if [ ! "$#" -eq 0 ]; then
67         echo "Need no arguments"
68         false
69     fi
70     read_pw
71     cat "${config_file}" | while read line; do
72         first_char=$(echo "${line}" | cut -c1)
73         if [ "${first_char}" = "#" ]; then
74             continue
75         fi
76         repo="${line}:${standard_repo}"
77         echo "Checking repo: ${repo}"
78         borg check --verbose "${repo}"
79     done
80 elif [ "${first_arg}" = "export_keyfiles" ]; then
81     if [ ! "$#" -eq 1 ]; then
82         echo "Need output tar file name."
83         false
84     fi
85     tar_target="${1}"
86     tmp_dir="${HOME}/.borgtmp"
87     keyfiles_dir="${tmp_dir}/borg_keyfiles"
88     mkdir -p "${keyfiles_dir}"
89     cat "${config_file}" | while read line; do
90         first_char=$(echo "${line}" | cut -c1)
91         if [ "${first_char}" = "#" ]; then
92             continue
93         fi
94         repo="${line}:${standard_repo}"
95         borg key export "${repo}" "${keyfiles_dir}/${line}"
96     done
97     cur_dir="$(pwd)"
98     cd "${tmp_dir}"
99     target=$(basename "${keyfiles_dir}")
100     tar cf "${tar_target}" "${target}"
101     mv "${tar_target}" "${cur_dir}"
102     cd
103     rm -rf "${tmp_dir}"
104 else
105     usage
106 fi