-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup
executable file
·89 lines (69 loc) · 2.43 KB
/
backup
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
#!/usr/bin/env bash
### Configuration
# Path to the folder which should be saved
BACKUP_PATH="/root"
# Name of the folder which should be saved
BACKUP_FOLDER="testfolder"
# Name of the backup (datum will be added automatically)
BACKUP_NAME="backup-of-testfolder"
# Name of the s3 bucket where the backups will be stored
S3_BUCKET_NAME="my-backup-bucket"
# Path in the s3 bucket where the backups will be stored
S3_BUCKET_PATH="/backups"
# Compression with zip or tar
COMPRESSION="zip"
# Temporary storage
TMP_PATH="/tmp"
###############################################################################
###############################################################################
############################ STOP EDITING HERE ############################
###############################################################################
###############################################################################
check_if_requirements_are_available()
{
if ! hash $1 2>/dev/null
then
echo "$1 command not found. You have to install $1 first."
exit 1
fi
}
case "$1" in
create|start|backup)
echo "Create Backup..."
full_backup_name="$BACKUP_NAME"_"$(date +%Y-%m-%d_%H-%M-%S)"
cd "$BACKUP_PATH"
if [ $COMPRESSION = "zip" ]; then
check_if_requirements_are_available zip
full_backup_name="$full_backup_name".zip
zip -r "$TMP_PATH/$full_backup_name" "$BACKUP_FOLDER"
elif [ $COMPRESSION = "tar" ]; then
check_if_requirements_are_available tar
full_backup_name="$full_backup_name".tar.gz
tar cfz "$TMP_PATH/$full_backup_name" "$BACKUP_FOLDER"
else
echo "Only zip or tar are valid."
exit 1
fi
echo "Upload Backup..."
s3cmd put "$TMP_PATH/$full_backup_name" "s3://$S3_BUCKET_NAME$S3_BUCKET_PATH/$full_backup_name"
echo "Clear tmp..."
rm -f "$TMP_PATH/$full_backup_name"
;;
list)
s3cmd ls "s3://$S3_BUCKET_NAME$S3_BUCKET_PATH/"
;;
download)
if [ -n "$2" ]; then
s3cmd get "s3://$S3_BUCKET_NAME$S3_BUCKET_PATH/${2}"
else
echo "Specify a file you want to download."
echo "Example: ${0} ${1} test.zip"
echo "Or list your backups with: ${0} list"
fi
;;
*)
echo "Use: ${0} {create|list|download}"
exit 2
;;
esac
exit 0