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