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