-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddzfs
executable file
·1810 lines (1574 loc) · 54.8 KB
/
ddzfs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# remove swift_cpanel
# 1.07.31.2020
# ddlvm does not support freebsd yet
# ddlvm needs email support and exit code checking
# we can replace old manifest code with create_manifest
#-f, --fail (HTTP) Fail silently (no output at all) on server errors. This is mostly done to better enable scripts etc to better deal with failed attempts. In normal cases when an HTTP server fails to deliver a document, it returns an HTML document stating so (which often also describes why and more). This flag will pre‐ vent curl from outputting that and return error 22.
###################################################
#
# Swift openstack wrapper for bash
# John Quaglieri <[email protected]>
#
# requires curl / file / md5 / python / bc / sqlite
#
###################################################
#-
# Copyright (c) 2019 InterServer, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of InterServer, Inc nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY INTERSERVER, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COMPANY OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#/
# to do
# add --delete to rsync
# support fake "folders" in containers
# test all binaries exist
#
# more freebsd support, update sleep
#
# cat may not be needed in split (however tests were not working initially)
#
###################################################
running_dir=$(dirname $0);
if [ ! -f ${running_dir}/swift.include ]; then
echo 'Missing swift.include file';
exit;
fi
. ${running_dir}/swift.include
if [ ! -f ${my_path}/swift.kludge ]; then
echo 'Missing swift.kludge file';
exit;
fi
. ${my_path}/swift.kludge
# auth key from each curl command
function getauthkey()
{
auth_key=`${curl} -v $CURLOPTS -H "X-Storage-User: ${username}" -H "X-Storage-Pass: ${pass}" $AUTH_URL 2>&1 | grep "^< X-" | grep -v "X-Storage-Token:" | cut -d " " -f2- > /$file`;
}
function create_config()
{
if [ "$HOME" = "" ]; then
echo "$HOME is not defined";
exit;
fi
mkdir -p $HOME/.swift
chmod 700 $HOME/.swift
echo -n "Enter Username (user:name): "
read config_uname
echo -n "Enter APIkey/password: "
read config_pass
echo -n "Enter auth url (enter for default https://storage-nj2.interserver.net/auth/v1.0) "
read config_authurl
if [ "$config_uname" = "" ]; then
echo "username was blank"
exit;
elif [ "$config_pass" = "" ]; then
echo "password was blank"
exit;
elif [ "$config_authurl" = "" ]; then
config_authurl="https://storage-nj2.interserver.net/auth/v1.0"
fi
if [ ! -e $HOME/.swift/config ]; then
touch $HOME/.swift/config
chmod 600 $HOME/.swift/config
echo "username=\"$config_uname\"" >> $HOME/.swift/config
echo "pass=\"$config_pass\"" >> $HOME/.swift/config
echo "AUTH_URL=\"$config_authurl\"" >> $HOME/.swift/config
exit;
else
echo "$HOME/.swift/config already exists";
exit;
fi
}
function rundebug() {
echo "curl: ${curl}";
echo "curl options: $CURLOPTS "
echo "Storage url: $STORAGE_URL";
echo
echo "Example url: ${curl} $CURLOPTS -o/dev/null -X PUT -H \"X-Object-Manifest: container/filename/\" -H \"X-Auth-Token: ${APIKEY}\" \"$STORAGE_URL/container/filename\" --data-binary ''";
echo
$my_path/isstat
}
# backup lvm and backup zfs use this function, however due to different partitions partclone has its own manifest
function create_manifest()
{
sleep 10s;
echo "Creating dd manifest file at line $LINENO"
sleep 10s;
echo "${curl} $CURLOPTS -o/dev/null -X PUT -H \"X-Object-Manifest: ${CONTAINER}/${REMOTEFILENAME}/\" -H \"X-Auth-Token: ${APIKEY}\" \"$STORAGE_URL/${CONTAINER}/${REMOTEFILENAME}\" --data-binary ''"
echo
sleep 2s;
# get new key
/bin/rm $file
getauthkey
APIKEY=`cat $file | grep ^X-Auth-Token: | cut -d: -f2 | tr -d '\r'`;
sleep 10s;
#echo "X-Object-Manifest: ${CONTAINER}/${SAVENAME}/"
#echo "X-Auth-Token: ${APIKEY}"
#echo "$STORAGE_URL";
#echo
${curl} $CURLOPTS -o/dev/null -X PUT -H "X-Object-Manifest: ${CONTAINER}/${REMOTEFILENAME}/" -H "X-Auth-Token: ${APIKEY}" "$STORAGE_URL/${CONTAINER}/${REMOTEFILENAME}" --data-binary ''
echo "done ddlvm at line $LINENO"
#echo
#sleep 2s;
#echo 'Fileinfo';
#$my_path/fileinfo ${CONTAINER} ${REMOTEFILENAME}
echo
}
#headers can verify creation
#HTTP/1.1 201 Created will show if created
function makedir()
{
if [ "$1" = "" ]; then
echo 'Usage ./ismkdir container';
else
CONTAINER="${1}";
URL=`urlencode "${CONTAINER}"`;
${curl} $CURLOPTS -X PUT -D - -H "X-Auth-Token: ${APIKEY}" "$STORAGE_URL/""${URL}"
fi
}
function delete_after()
{
if [ "$1" = "" -o "$2" = "" -o "$3" = "" ]; then
echo 'Usage ./deleteafter container file XX (number of days)';
else
CONTAINER="${1}";
FILE="${2}";
TIME=`echo "${3} * 86400" | $bc -l`;
if [ "$TIME" = "" ]; then
echo "Did not get a proper value for time (non integer used) at $LINENO";
echo "Defaulting to 45 days";
TIME=3888000;
sleep 5s;
fi
listsplits=`${curl} $CURLOPTS -s -H "X-Auth-Token: ${APIKEY}" "$STORAGE_URL"/"${CONTAINER}" 2>&1 | grep "${FILE}/" | grep "/split-"`
if [ "$listsplits" = "" ]; then
URL=`urlencode "${CONTAINER}/${FILE}"`;
delete_after_check=`${curl} $CURLOPTS -X POST -H "X-Delete-After: $TIME" -H "X-Auth-Token: ${APIKEY}" "$STORAGE_URL/$URL" | grep -i accepted`;
sleep 1s;
if [ ! "${delete_after_check}" = "" ]; then
echo "Appears we got an error, will retry";
sleep 1s;
${curl} $CURLOPTS -X POST -H "X-Delete-After: $TIME" -H "X-Auth-Token: ${APIKEY}" "$STORAGE_URL/$URL"
else
echo 'Accepted command';
fi
echo
else
for SPLITNAME in $listsplits; do
URL=`urlencode "${CONTAINER}/${SPLITNAME}"`;
${curl} $CURLOPTS -X POST -H "X-Delete-After: $TIME" -H "X-Auth-Token: ${APIKEY}" "$STORAGE_URL/$URL"
done
# delete the filemanifest too
URL=`urlencode "${CONTAINER}/${FILE}"`
# need to do delete after and object manifest at the same time
${curl} $CURLOPTS -X POST -H "X-Object-Manifest: ${URL}/" -H "X-Delete-After: $TIME" -H "X-Auth-Token: ${APIKEY}" "$STORAGE_URL/$URL"
fi
fi
}
function backupzfs()
{
#/admin/swift/ddzfs vz/windows153389@82ea43947e597607e6b7ae885a44e9be94b6aefb82ccdcfb705ff604b691fee4 vps153389 windows153389-2018-09-13-28738.tgz
if [ "$1" = "" -o "$2" = "" -o "$3" = "" ]; then
echo 'Usage ./ddzfs vz/windows153389@82ea43947e597607e6b7ae885a44e9be94b6aefb82ccdcfb705ff604b691fee4 (dataset) vps153389 (container) windows153389-2018-09-13-28738.tgz (filename) optional email';
exit;
fi
if [ ! -e /sbin/zfs ]; then
echo '/sbin/zfs does not exist';
exit;
fi
DATASET=$1;
REMOTEFILENAME=$3;
CONTAINER=$2;
# we pass dataset to shell
if [[ ! $DATASET =~ ^[a-zA-Z0-9@/.]+$ ]]; then
echo "$DATASET contains invalid characters";
exit;
fi
check_container_exists ${CONTAINER}
# split size
bytes=4096;
if [ "$(zfs list -t snapshot -o name -H | grep ^${DATASET}$)" = "${DATASET}" ]; then
REDO_BACKUP=1;
if [ "$SET_FAST" = "1" ]; then
#echo "Using fast filter";
# etag and content type are used for md5 checksums and since we are getting this from stdin we do not know these
# we need to get the auth key
zfs send ${DATASET} | ${splitprog} --suffix-length=3 --verbose -d --bytes=${bytes}M --filter="export KEY='$APIKEY'; export STORAGE_URL=$STORAGE_URL; export WRCONTAINER=${CONTAINER}; export WRREALFILE=${REMOTEFILENAME}; $my_path/include/curlwrapper"
# check exit code
splitexit=`echo $?`;
if [ ! "$splitexit" = "0" ]; then
echo 'Bad exit from backup';
sleep 10s;
# we will want to delete the uploads here then retry
# for now
create_manifest
send_email_error
else
echo 'Exit status ok';
create_manifest
REDO_BACKUP=0;
fi
fi
# set fast if backup is ok we skip this. still testing.
if [ "$REDO_BACKUP" = "1" ]; then
zfs send ${DATASET} | ${splitprog} --suffix-length=3 --verbose -d --bytes=${bytes}M --filter="export WRCONTAINER=${CONTAINER}; export WRREALFILE=${REMOTEFILENAME}; $my_path/include/splitwrapper"
create_manifest
fi
else
echo 'ERROR SNAPSHOT DOES NOT EXIST';
fi
}
function backuplvm()
{
# designed for interserver cloud use
# it should be done with a snapshot
if [ "$1" = "" -o "$2" = "" -o "$3" = "" ]; then
echo 'Usage ./ddlvm windows49159 (lvm) vps49159 (container) windows49159-april6-2015.img.gz (remote filename) partclone|email (optional with out it is dd) email@address (optional)';
exit;
else
if [ ! -x $DDPROG ]; then
echo "$DDPROG does not exist";
exit;
fi
if [ -e /etc/master.passwd ]; then
echo 'No FreeBSD support';
exit;
fi
NAME=$1;
REMOTEFILENAME=$3;
CONTAINER=$2;
if [[ ! $NAME =~ ^[a-zA-Z0-9-]+$ ]]; then
echo "$NAME contains invalid characters";
exit;
fi
#SOLUSVM backup
if [ -d /usr/local/solusvm ]; then
solus=1;
echo 'Found SolusVM';
SOLUSVM=`vgdisplay | grep "VG Name" | awk '{print $3}' | head -n 1`;
echo "VG name is $SOLUSVM";
if [ "$SOLUSVM" = "" ]; then
echo "VG path is blank at $LINENO";
exit;
fi
VZPATH="/dev/${SOLUSVM}";
if [ -d /dev/${SOLUSVM} ]; then
echo "Using /dev/${SOLUSVM}";
else
echo "/dev/${SOLUSVM} is not a directory";
exit;
fi
else
VZPATH=/dev/vz;
fi
SRIPLVM=`echo "${VZPATH}/${NAME}" | tr -d '\r'`;
# Check that we exist
if [ ! -L ${SRIPLVM} ]; then
echo "${SRIPLVM} does not exist";
exit;
fi
check_container_exists ${CONTAINER}
# split size
bytes=4096;
if [ ! "$5" = "" ]; then
if [[ "$5" =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$ ]]; then
echo "Valid email passed";
email_to_send=$5;
HOSTNAME=$(hostname);
export RUN_BY_CRON=1;
else
echo "Email is not valid no email will be sent";
fi
fi
if [ "$4" = "partclone" ]; then
echo "Using partclone";
partclone_errors=0;
if [ ! -e "${partclone_path}.info" ]; then
echo "Partclone is not installed can not be run";
elif [ "$solus" = "1" ]; then
echo "Partclone is not supported on solusvm";
else
sleep 5;
kpartx -pp -av ${SRIPLVM}
sleep 2;
if [ ! -L /dev/mapper/vz-${NAME}p1 ]; then
echo "Did not find /dev/mapper/vz-${NAME}p1";
kpartx -pp -dv ${SRIPLVM}
else
#here we are actually running backups
for backup_partitions in $(ls /dev/mapper/ | grep ^vz-${NAME}p); do
partition_name=/dev/mapper/${backup_partitions};
if [ -L ${partition_name} ]; then
partition_check=$(lsblk --output FSTYPE,NAME ${partition_name} | grep -v FST | awk '{print $1}');
if [[ ${partition_check} =~ ^[a-z]+ ]]; then
case "${partition_check}" in
swap)
echo "skipping swap";
continue;
;;
ext3)
if [ -x "${partclone_patch}.ext3" ]; then
pc_call=${partclone_path}.ext3;
else
echo "${partclone_path}.ext3 does not exist";
fi
;;
ext4)
if [ -x "${partclone_path}.ext4" ]; then
pc_call=${partclone_path}.ext4;
else
echo "${partclone_path}.ext3 does not exist";
fi
;;
xfs)
if [ -x "${partclone_path}.xfs" ]; then
pc_call=${partclone_path}.xfs;
else
echo "${partclone_path}.xfs does not exist";
fi
;;
ntfs)
if [ -x "${partclone_path}.ntfs" ]; then
pc_call=${partclone_path}.ntfs;
else
echo "${partclone_path}.ntfs does not exist";
fi
;;
*)
echo 'Unknown call partition';
continue;
;;
esac
if [ "$pc_call" = "" ]; then
echo "$pc_call is blank at $LINENO";
else
echo "Running backup $pc_call ${partition_name} ${backup_partitions} which is ${partition_check} calling it ${REMOTEFILENAME}";
partclone=1;
$pc_call -L $TMP_PATH/${NAME}.log -c -s ${partition_name} | ${splitprog} --suffix-length=3 --verbose -d --bytes=${bytes}M --filter="export NOGZIP=1; export WRCONTAINER=${CONTAINER}; export WRREALFILE=${REMOTEFILENAME}_${backup_partitions}; $my_path/include/splitwrapper"
# test exit code
# if not zero backup failed
if [ ! "${PIPESTATUS[0]}" = "0" ]; then
partclone_errors=1;
echo "Partclone ended with an error ${PIPESTATUS[0]}";
sleep 2s;
$my_path/isrm ${CONTAINER} ${REMOTEFILENAME}_${backup_partitions}
else
sleep 10s;
${curl} $CURLOPTS -o/dev/null -X PUT -H "X-Object-Manifest: ${CONTAINER}/${REMOTEFILENAME}_${backup_partitions}/" -H "X-Auth-Token: ${APIKEY}" "$STORAGE_URL/${CONTAINER}/${REMOTEFILENAME}_${backup_partitions}" --data-binary ''
echo "Creating dd manifest file at line $LINENO"
sleep 2s;
# on occasion this command fails
/bin/rm $file
getauthkey
APIKEY=`cat $file | grep ^X-Auth-Token: | cut -d: -f2 | tr -d '\r'`;
${curl} $CURLOPTS -o/dev/null -X PUT -H "X-Object-Manifest: ${CONTAINER}/${REMOTEFILENAME}_${backup_partitions}/" -H "X-Auth-Token: ${APIKEY}" "$STORAGE_URL/${CONTAINER}/${REMOTEFILENAME}_${backup_partitions}" --data-binary ''
echo "done ddlvm at line $LINENO"
echo
sleep 2s;
#echo 'Fileinfo';
#$my_path/fileinfo ${CONTAINER} ${REMOTEFILENAME}_${backup_partitions}
fi
fi
else
echo "${partition_check} failed regex";
fi
else
echo "/dev/mapper/${backup_partitions} is not a device";
fi
done
kpartx -pp -dv ${SRIPLVM}
fi
fi
if [ "$partclone_errors" = "1" ]; then
echo "Partclone errored out running dd";
export ERROR_NOTICE="Partclone error on ${CONTAINER}";
export ERROR_SUBJECT='Partclone error';
export HOSTNAME=`hostname`;
send_email_error
else
echo "Partclone completed, exiting";
exit;
fi
fi
if [ -f /dev/shm/${CONTAINER}.log ]; then
/bin/rm /dev/shm/${CONTAINER}.log
fi
# log
if [ ! "$email_to_send" = "" ]; then
exec > >(tee -i $TMP_PATH/${CONTAINER}.log)
fi
# older slower method but more verification
REDO_BACKUP=1;
if [ "$SET_FAST" = "1" ]; then
#echo "Using fast filter";
# etag and content type are used for md5 checksums and since we are getting this from stdin we do not know these
${DDPROG} bs=4096 conv=noerror if=${SRIPLVM} | ${splitprog} --suffix-length=3 --verbose -d --bytes=${bytes}M --filter="export KEY='$APIKEY'; export STORAGE_URL=$STORAGE_URL; export WRCONTAINER=${CONTAINER}; export WRREALFILE=${REMOTEFILENAME}; $my_path/include/curlwrapper"
# check exit code
splitexit=`echo $?`;
if [ ! "$splitexit" = "0" ]; then
echo 'Bad exit from backup';
sleep 10s;
send_email_error
else
echo 'Exit status ok';
REDO_BACKUP=0;
fi
fi
if [ "$REDO_BACKUP" = "1" ]; then
# older slower method but more verification
${DDPROG} bs=4096 conv=noerror if=${SRIPLVM} | ${splitprog} --suffix-length=3 --verbose -d --bytes=${bytes}M --filter="export WRCONTAINER=${CONTAINER}; export WRREALFILE=${REMOTEFILENAME}; $my_path/include/splitwrapper"
fi
if [ ! "${PIPESTATUS[0]}" = "0" ]; then
echo 'DD RETURNED ERRORS CHECK THE LOG';
export ERROR_NOTICE="DD ERROR ${CONTAINER}";
export ERROR_SUBJECT='DD error Detected';
export HOSTNAME=`hostname`;
send_email_error
else
export ERROR_SUBJECT='No errors Detected';
fi
# move to own mail function
if [ ! "$email_to_send" = "" ]; then
${MAILX_PATH} -s "${ERROR_SUBJECT} InterServer Backup ${CONTAINER} filename ${REMOTEFILENAME}_${backup_partitions} on ${HOSTNAME}" -S smtp=${smtpserver} -S smtp-auth=login -S smtp-auth-user=${smtplogin} -S smtp-auth-password=${smtppassword} -S from="${smtpfrom}" -S ssl-verify=ignore -S nss-config-dir=/etc/pki/nssdb $email_to_send < $TMP_PATH/${CONTAINER}.log
fi
create_manifest
fi
}
function backupmysql()
{
if [ "$1" = "" -o "$2" = "" ]; then
echo 'Usage ./backupmysql container filename';
exit;
fi
if [ ! -f /root/.my.cnf ]; then
echo 'No /root/.my.cnf, skipping';
exit;
fi
if [ ! -x /usr/bin/mysqldump ]; then
echo '/usr/bin/mysqldump not executable or does not exist';
exit;
fi
REMOTEFILENAME=$2;
CONTAINER=$1;
check_container_exists ${CONTAINER}
# split size
bytes=4096;
/usr/bin/mysqldump -f --single-transaction --quick --skip-extended-insert --all-databases --add-drop-table --allow-keywords -u root | ${splitprog} --suffix-length=3 --verbose -d --bytes=${bytes}M --filter="export WRCONTAINER=${CONTAINER}; export WRREALFILE=${REMOTEFILENAME}; $my_path/include/splitwrapper"
sleep 10s;
echo "Creating manifest at line $LINENO";
${curl} $CURLOPTS -o/dev/null -X PUT -H "X-Object-Manifest: ${CONTAINER}/${REMOTEFILENAME}/" -H "X-Auth-Token: ${APIKEY}" "$STORAGE_URL/${CONTAINER}/${REMOTEFILENAME}" --data-binary ''
sleep 5s;
${curl} $CURLOPTS -o/dev/null -X PUT -H "X-Object-Manifest: ${CONTAINER}/${REMOTEFILENAME}/" -H "X-Auth-Token: ${APIKEY}" "$STORAGE_URL/${CONTAINER}/${REMOTEFILENAME}" --data-binary ''
}
function runabackup()
{
# single file backup or directory which is tarred on the fly
# delete after 30 days by default
# auto create dir if needed, based on hostname
# append the date
if [ "$1" = "" ]; then
echo 'Usage ./backup filename';
echo 'Notes: container will be the server hostname and the file will be called filename-year-month-day';
exit;
fi
#get date
year=$(date +%Y)
month=$(date +%m)
day=$(date +%d)
hostname=`$hostprog`;
if [ "$hostname" = "" ]; then
echo "hostname returned a blank value in function runabackup at line $LINENO";
exit;
else
check_container_exists $hostname --force
fi
JUSTFILE=`basename ${1}`;
echo "Backup up file ${1} as ${JUSTFILE}-${year}-${month}-${day} to container ${hostname}";
if [ -f "$1" ]; then
upload ${hostname} ${1} ${JUSTFILE}-${year}-${month}-${day}
elif [ -d "$1" ]; then
onthefly ${hostname} ${1} frombackup ${JUSTFILE}-${year}-${month}-${day}
# delete the fly files after 30 days
onthefly ${hostname} ${JUSTFILE}-${year}-${month}-${day} deleteafter
else
echo "runabackup can only do files or directories";
exit;
fi
echo 'Sleeping 5 seconds to settle upload';
sleep 5s;
delete_after ${hostname} ${JUSTFILE}-${year}-${month}-${day} 30
}
# test size, if we are 5GB or over display error to use split
function check_size()
{
# 5368709120 is 5gb in bytes, we are under by 1 byte for swift
size=5368709119;
#size=100;
FILE="${1}";
if [ "$FILE" = "" ]; then
echo "File name missing in function check_size at $LINENO";
exit;
fi
if [ ! -f $FILE ]; then
echo "File $FILE does not exist in function check_size at $LINENO";
exit;
fi
# ls is quicker
if [ -x /bin/ls ]; then
file_size=`/bin/ls -l $FILE | awk '{print $5}'`;
elif [ -x /usr/bin/du ]; then
file_size=`/usr/bin/du $FILE | awk '{print $1}'`;
else
echo "No programs to check disk space in check_size at $LINENO";
exit;
fi
if [ "$file_size" -gt "$size" ]; then
echo "$FILE size is $file_size which is over 5GB. Must use the split function";
echo "sending split split $2 $FILE $3 $4 $5";
split $2 $FILE $3 $4 $5
else
if [ "$debug" = "1" ]; then
echo "Found file size $file_size for file $FILE in function check_size";
fi
fi
}
function display_storage_url()
{
echo
echo "Storage URL (pub/private): ${blue}$STORAGE_URL${normal}"
echo
}
function makepublic()
{
if [ "$1" = "" ]; then
echo 'Usage ./mkpub container [optional --remove (to make private)]';
echo '[optional --dirlist (to allow directory listings)]';
exit;
fi
CONTAINER="${1}";
if [ "$2" = "--remove" ]; then
${curl} $CURLOPTS -X PUT -H 'X-Container-Read: \n*' -H "X-Auth-Token: ${APIKEY}" "$STORAGE_URL/""${CONTAINER}"
display_storage_url
exit;
elif [ "$2" = "--dirlist" ]; then
${curl} $CURLOPTS -X PUT -H 'X-Container-Read: .r:*,.rlistings' -H "X-Auth-Token: ${APIKEY}" "$STORAGE_URL/""${CONTAINER}"
display_storage_url
exit;
# default call
elif [ "$2" = "" ]; then
${curl} $CURLOPTS -X PUT -H 'X-Container-Read: .r:*' -H "X-Auth-Token: ${APIKEY}" "$STORAGE_URL/""${CONTAINER}"
display_storage_url
else
echo 'Invalid call to mkpub';
display_storage_url
exit;
fi
}
function getstats()
{
if [ "$1" = "" ]; then
${curl} -v $CURLOPTS -X HEAD -H "X-Auth-Token: ${APIKEY}" "$STORAGE_URL" 2>&1 | grep X-Account | cut -d" " -f2-
# display api key info
cat $file
elif [ "$1" = "/all" ]; then
bytes=`${curl} -v $CURLOPTS -X HEAD -H "X-Auth-Token: ${APIKEY}" "${STORAGE_URL}" 2>&1 | grep X-Account-Bytes-Used: | cut -d: -f2 | tr -d '\r'`;
echo "${bytes} / 1000000000" | $bc -l
else
CONTAINER=$1;
check_container_exists ${CONTAINER}
# next three are code reuse, fix on any updates
# in mb
if [ "$2" = "mb" ]; then
bytes=`${curl} -v $CURLOPTS -X HEAD -H "X-Auth-Token: ${APIKEY}" "${STORAGE_URL}/${CONTAINER}" 2>&1 | grep X-Container-Bytes-Used: | cut -d: -f2 | tr -d '\r'`;
# in the future awk 'BEGIN{print int(($bytes / 1000000 )+0.5)}'
echo "${bytes} / 1000000" | $bc -l
# in gb
elif [ "$2" = "gb" ]; then
bytes=`${curl} -v $CURLOPTS -X HEAD -H "X-Auth-Token: ${APIKEY}" "${STORAGE_URL}/${CONTAINER}" 2>&1 | grep X-Container-Bytes-Used: | cut -d: -f2 | tr -d '\r'`;
# in the future awk 'BEGIN{print int(($bytes / 1000000 )+0.5)}
echo "${bytes} / 1000000000" | $bc -l
else
# normal display
${curl} -v $CURLOPTS -X HEAD -H "X-Auth-Token: ${APIKEY}" "${STORAGE_URL}/${CONTAINER}" 2>&1 | grep X-Container | cut -d" " -f2-
fi
fi
}
function showfileinfo()
{
if [ "$1" = "" -o "$2" = "" ]; then
echo 'Usage ./fileinfo container file';
else
CONTAINER=$1;
check_container_exists ${CONTAINER}
FILE=$2;
${curl} -v $CURLOPTS -X HEAD -H "X-Auth-Token: ${APIKEY}" "${STORAGE_URL}/${CONTAINER}/${FILE}" || exit 0
fi
}
function rcopy
{
if [ "$1" = "" ]; then
echo 'Usage ./iscp container file newfile';
else
CONTAINER=$1;
FILE=$2;
NEWFILE=$3;
${curl} $CURLOPTS -X COPY -H "Destination: ${CONTAINER}/${NEWFILE}" -H "X-Auth-Token: ${APIKEY}" "$STORAGE_URL/${CONTAINER}/${FILE}"
fi
}
function rmove
{
if [ "$1" = "" ]; then
echo 'Usage ./ismv container newcontainer file';
else
CONTAINER=$1;
NEWCONTAINER=$2;
FILE=$3;
${curl} $CURLOPTS -X COPY -H "Destination: ${NEWCONTAINER}/${FILE}" -H "X-Auth-Token: ${APIKEY}" "$STORAGE_URL/${CONTAINER}/${FILE}"
sleep 1s;
echo
echo -n 'Delete old file? [y/n]';
read check
if [ "$check" = "y" ]; then
delete ${CONTAINER} ${FILE}
else
echo 'Skipping deletion';
fi
fi
}
checkforexit18()
{
if [ "$1" = "" -o "$2" = "" ]; then
echo "Did not pass container or filename in function checkforexit18 at LINE $LINENO";
exit;
fi
if [ "$curlexit" = "18" ]; then
if [ "$debug" = "1" ]; then
echo "Curl exiting with code 18 retrying";
fi
download $1 $2 -c
else
if [ "$debug" = "1" ]; then
echo "Curl exiting with code $curlexit";
fi
fi
}
function download()
{
if [ $# -lt 2 ]; then
echo 'Usage ./isget <container> <file> [-f] [-out]';
echo ' -f Optional, force overwrite existing file'
echo ' -out Optional, output to stdout instead of file'
return;
fi
force=0
out=0
continue=0;
CONTAINER=$1
if [ "$CONTAINER" = "x" ]; then
if [ -f /root/.swift/hostname ]; then
CONTAINER=`cat /root/.swift/hostname`;
else
CONTAINER=`/bin/hostname`;
fi
fi
shift
FILE=$1
shift
while [ $# -gt 0 ]; do
if [ "$1" = "-f" ]; then
force=1;
elif [ "$1" = "-out" ]; then
out=1;
elif [ "$1" = "-c" ]; then
continue=1;
fi
shift
done
if [ -d "${FILE}" ]; then
echo "Directory with same name exists, can not download";
return;
fi
if [ $out = 0 ]; then
if [ -f "${FILE}" ]; then
if [ $force = 1 ]; then
if [ "$debug" = "1" ]; then
echo "Downloading file ${FILE} with force";
fi
${curl} $CURLOPTS -o ${FILE} -H "X-Auth-Token: ${APIKEY}" "$STORAGE_URL/${CONTAINER}/${FILE}"
curlexit=`echo $?`;
checkforexit18 ${CONTAINER} ${FILE}
elif [ $continue = 1 ]; then
if [ "$debug" = "1" ]; then
echo "Downloading file ${FILE} with continue";
fi
${curl} $CURLOPTS -C - -o ${FILE} -H "X-Auth-Token: ${APIKEY}" "$STORAGE_URL/${CONTAINER}/${FILE}"
curlexit=`echo $?`;
checkforexit18 ${CONTAINER} ${FILE}
else
echo "File with same name exists, can not download with out -f or -c";
return;
fi
else
${curl} $CURLOPTS -C - -o ${FILE} -H "X-Auth-Token: ${APIKEY}" "$STORAGE_URL/${CONTAINER}/${FILE}"
curlexit=`echo $?`;
checkforexit18 ${CONTAINER} ${FILE}
fi
else
${curl} $CURLOPTS -H "X-Auth-Token: ${APIKEY}" "$STORAGE_URL/${CONTAINER}/${FILE}"
curlexit=`echo $?`;
checkforexit18 ${CONTAINER} ${FILE}
fi
}
#delete file, supports email
function delete()
{
# curl -D . -X DELETE -H "X-Auth-Token: yourAuthToken" -H "X-Purge-Email: [email protected]" https://cdn1.clouddrive.com/v1/yourAccountHash/bar/foo.txt
if [ "$1" = "" -o "$2" = "" ]; then
echo 'Usage ./isrm container file bulk [opt force] (if bulk is sent then file is the filename to call)';
else
FILE=$2;
CONTAINER=$1;
# this passes bulk delete which we list the files to remove in a file separated by a \n
if [ "$3" = "bulk" ]; then
echo 'Using bulk remove';
# lets ensure we use a simple file name
if [[ ${FILE} =~ ^[a-zA-Z0-9]*$ ]]; then
if [ ! -f ${FILE} ]; then
echo "Filname ${FILE} does not exist";
else
# don't do anything if the file is empty
if [[ ! -s ${FILE} ]]; then
echo 'FILE is empty and will not continue';
exit;
fi
# allow force to have no verification for scripting
if [ ! "$4" = "force" ]; then
echo "We will remove in container ${CONTAINER} the below files";
sleep 1s;
cat ${FILE}
echo -n "continue? y or n: "
read waitbulk
if [ ! "$waitbulk" = "y" ]; then
echo 'bye';
exit 1;
fi
fi
# actually delete
echo "running delete";
${curl} $CURLOPTS -i -H "X-Auth-Token: ${APIKEY}" -H "Accept: text/plain" -H "Content-Type: text/plain" -X DELETE "$STORAGE_URL/${CONTAINER}/?bulk-delete" --data-binary @${FILE}
# generally we will remove the file but we output back possible errors
if [ "$4" = "force" ]; then
/bin/rm -v ${FILE}
else
/bin/rm -i -v ${FILE}
fi
fi
else
echo 'Keep filenames simple a-z 0-9 A_Z';
fi
else
# non bulk delete
${curl} $CURLOPTS -o/dev/null -X DELETE -H "X-Auth-Token: ${APIKEY}" "$STORAGE_URL/${CONTAINER}/${FILE}"
fi
fi
}
function rrmdir()
{
if [ "$1" = "" ]; then
echo 'Usage ./isdir container [ force ] for none empty containers';
else
CONTAINER=$1;
check_container_exists ${CONTAINER}
# remove all data
if [ "$2" = "force" ]; then
echo -n "DO you really want to remove all data in ${CONTAINER} and remove the container? ";
read nogoingback
if [ "$nogoingback" = "y" ]; then
for filetodelete in `$my_path/isls ${CONTAINER}`; do $my_path/isrm ${CONTAINER} $filetodelete; done
else
echo "Cancelling";
exit;
fi
fi
${curl} $CURLOPTS -o/dev/null -X DELETE -H "X-Auth-Token: ${APIKEY}" "$STORAGE_URL/${CONTAINER}"
fi
}
function getcontenttype()
{
FILE=$1;
if [ ! -e "${FILE}" ]; then
echo "Failed on getting content type of file ${FILE} at line $LINENO file does not exist";
exit;
fi
${FILECOMMAND} -bi ${FILE}
}
function getmd5()
{
FILE=$1;
if [ ! -f "${FILE}" ]; then
echo "Ran into a problem in getmd5 function, file not found or is a directory";
exit;
fi
if [ -e /etc/master.passwd ]; then
$md5prog "${FILE}" | awk '{print $4}'
else
$md5prog "${FILE}" | awk '{print $1}'
fi
}
function urlencode()
{
python -c "import urllib; print urllib.quote('$*')"
}
function getremotemd5()
{
if [ "$1" = "" -o "$2" = "" ]; then
echo "Usage ./getmd5 container filename";
exit;
fi
FILE=$2;
CONTAINER=$1;
check_container_exists ${CONTAINER}
# remove final /r
#encoded_value=$(python -c "import urllib; print urllib.quote('''$value''')")
URL=`urlencode "${CONTAINER}/${FILE}"`;
${curl} -s $CURLOPTS -I -H "X-Auth-Token: ${APIKEY}" "${STORAGE_URL}/${URL}" | grep ^Etag: | awk '{print $2}' | tr -d '\r'
}
function rsyncfile()
{
if [ "$1" = "" -o "$2" = "" ]; then
echo 'Usage ./rsync container file [args]';
echo 'optional args: --put (upload file) add new file name after --put for rename';
echo 'optional args: --get (download file)';
echo 'optional args: --check (checks md5sum for remote / localfile) add new file name after --check for a renamed file [add 24 to just select todays entries in sqlite db]';