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