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