├── README.md
└── yd-api-backuper.sh
/README.md:
--------------------------------------------------------------------------------
1 | Simpe bash script for backup files to Yandex.Disk via API
2 | For more details you can visit [article at my blog](http://zharenkov.ru/post/skript-kopirovaniya-faila-na-yandeks.disk-cherez-api)
3 |
4 | Features
5 | --------
6 | * OAuth authorization
7 | * Needs only curl (no webdav or yandex client)
8 | * GPG encryption
9 | * email notification
10 |
11 | Settings
12 | --------
13 |
14 | token -- token for Yandex.Disk application
15 | backupDir -- name of backup directory
16 | logFile -- name of logfile
17 | GPGENCRYPTUID -- GPG UID
18 | mailLog -- email address for sending logs
19 | mailLogErrorOnly -- send email only if error occured
20 |
21 | Options
22 | -------
23 |
24 |
25 | -h Show help
26 | -f /path/to/file Specify filename for upload
27 | -g user@gpgid Specify GPG UID
28 | -m user@localhost Specify email for logging
29 | -e Send email on error only
30 |
31 |
32 | Usage
33 | -----
34 |
35 | Copy file to Yandex Disk:
36 | ```bash
37 | ./yd-api-backuper.sh -f /path/to/file/file_name
38 | ```
39 | Encrypt and copy file to Yandex Disk:
40 | ```bash
41 | ./yd-api-backuper.sh -f /path/to/file/file_name -g username@server
42 | ```
43 | Copy file to Yandex Disk and notify user about errors
44 | ```bash
45 | ./yd-api-backuper.sh -f /path/to/file/file_name -m user@localhost -e
46 | ```
--------------------------------------------------------------------------------
/yd-api-backuper.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | #
4 | # Author : Zharenkov I.V.
5 | # License : GNU/GPL2
6 | # Date : 2014-07-17
7 | # Update : 2015-05-25
8 | # Requirements : curl
9 | # Version : 0.4
10 | #
11 |
12 |
13 | # ---------- SETTINGS -------------
14 |
15 | # Yandex.Disk token
16 | token=''
17 |
18 | # Target directory. Be sure, that the directory exists.
19 | backupDir='backup'
20 |
21 | # Logfile name
22 | logFile=yd-api-backuper.log
23 |
24 | # GPG encryption UID
25 | GPGENCRYPTUID=''
26 |
27 | # Send log to email
28 | mailLog=''
29 |
30 | # Send email error only
31 | mailLogErrorOnly=false
32 |
33 | # ---------- FUNCTIONS ------------
34 |
35 | function usage
36 | {
37 | cat < Specify filename for upload
45 | -g Specify GPG UID
46 | -m Specify email for logging
47 | -e Send mail on error only
48 |
49 | EOF
50 | }
51 |
52 | function gpgEncrypt()
53 | {
54 | gpg -e -r $GPGENCRYPTUID $FILENAME
55 | }
56 |
57 | function mailing()
58 | {
59 | # Function's arguments:
60 | # $1 -- email subject
61 | # $2 -- email body
62 | if [ ! $mailLog = '' ];then
63 | if [ "$mailLogErrorOnly" == true ];
64 | then
65 | if echo "$1" | grep -q 'error'
66 | then
67 | echo "$2" | mail -s "$1" $mailLog > /dev/null
68 | fi
69 | else
70 | echo "$2" | mail -s "$1" $mailLog > /dev/null
71 | fi
72 | fi
73 | }
74 |
75 |
76 | function logger()
77 | {
78 | echo "["`date "+%Y-%m-%d %H:%M:%S"`"] File $FILENAME: $1" >> $logFile
79 | }
80 |
81 | function parseJson()
82 | {
83 | local output
84 | regex="(\"$1\":[\"]?)([^\",\}]+)([\"]?)"
85 | [[ $2 =~ $regex ]] && output=${BASH_REMATCH[2]}
86 | echo $output
87 | }
88 |
89 | function checkError()
90 | {
91 | echo $(parseJson 'error' "$1")
92 | }
93 |
94 | function getUploadUrl()
95 | {
96 | local output
97 | local json_out
98 | local json_error
99 | json_out=`curl -s -H "Authorization: OAuth $token" https://cloud-api.yandex.net:443/v1/disk/resources/upload/?path=$backupDir/$backupName&overwrite=true`
100 | json_error=$(checkError "$json_out")
101 | if [[ $json_error != '' ]];
102 | then
103 | logger "Yandex Disk error: $json_error"
104 | mailing "Yandex Disk backup error" "ERROR copy file $FILENAME. Yandex Disk error: $json_error"
105 | exit 1
106 | else
107 | output=$(parseJson 'href' $json_out)
108 | echo $output
109 | fi
110 | }
111 |
112 | function uploadFile
113 | {
114 | local json_out
115 | local uploadUrl
116 | local json_error
117 | uploadUrl=$(getUploadUrl)
118 | if [[ $uploadUrl != '' ]];
119 | then
120 | json_out=`curl -s -T $1 -H "Authorization: OAuth $token" $uploadUrl`
121 | json_error=$(checkError "$json_out")
122 | if [[ $json_error != '' ]];
123 | then
124 | logger "Yandex Disk error: $json_error"
125 | mailing "Yandex Disk backup error" "ERROR copy file $FILENAME. Yandex Disk error: $json_error"
126 |
127 | else
128 | logger "Copying file to Yandex Disk success"
129 | mailing "Yandex Disk backup success" "SUCCESS copy file $FILENAME"
130 |
131 | fi
132 | else
133 | echo 'Some errors occured. Check log file for detail'
134 | exit 1
135 | fi
136 | }
137 |
138 | function preUpload()
139 | {
140 | if [ ! $GPGENCRYPTUID = '' ];
141 | then
142 | gpgEncrypt
143 | FILENAME=$FILENAME.gpg
144 | fi
145 |
146 | backupName=`date "+%Y%m%d-%H%M"`_$(basename $FILENAME)
147 | }
148 |
149 | function postUpload()
150 | {
151 | if [ ! $GPGENCRYPTUID = '' ];
152 | then
153 | rm $FILENAME
154 | fi
155 | }
156 |
157 | # --------------- OPTIONS -------------
158 |
159 | while getopts ":f:g:m:he" opt; do
160 | case $opt in
161 | h)
162 | usage
163 | exit 1
164 | ;;
165 | f)
166 | FILENAME=$OPTARG
167 |
168 | if [ ! -f $FILENAME ];
169 | then
170 | echo "File not found: $FILENAME"
171 | logger "File not found"
172 | mailing "Yandex Disk backup error" "ERROR copy file $FILENAME. File not found."
173 | exit 1
174 | fi
175 | ;;
176 | g)
177 | GPGENCRYPTUID=$OPTARG
178 | ;;
179 | e)
180 | mailLogErrorOnly=true
181 | ;;
182 | m)
183 | mailLog=$OPTARG
184 | ;;
185 | \?)
186 | echo "Invalid option: -$OPTARG. $0 -h for help" >&2
187 | exit 1
188 | ;;
189 | :)
190 | echo "Option -$OPTARG requires an argument." >&2
191 | exit 1
192 | ;;
193 | esac
194 | done
195 |
196 | # --------------- MAIN ----------------
197 |
198 | if [[ -z $FILENAME ]]
199 | then
200 | usage
201 | exit 1
202 | fi
203 |
204 | preUpload
205 |
206 | uploadFile $FILENAME
207 |
208 | postUpload
209 |
210 |
--------------------------------------------------------------------------------