This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathv-sync-backups-to-mega
80 lines (73 loc) · 4.12 KB
/
v-sync-backups-to-mega
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
#!/bin/bash
# info: Script loading all backups users in the MEGA cloud
# expansion: Functional additions for Vesta Control Panel
# options: BACKUP_COUNT LOG_MAX_ROWS_COUNT
# version: v1.1.3
#
# Copyright 2016 Vasilyuk Vasiliy <vasilyukvasiliy@gmail.com>
# Fix: Centos error starting CRON task / megals: command not found #1
export PATH=$PATH:/usr/local/bin
#---------------------------------------------------------#
# Configuration definition #
#---------------------------------------------------------#
CLOUD_BACKUPS_DIR="DEFAULT_DIR"; #Name the backup directory in the MEGA cloud
RUN_USER=$(who | head -n 1 | awk '{print $1}'); #The user running the script
RUN_SCRIPT=${0##*/}; #The name of the current script at startup
VESTA_USERS=$(ls "/home/"); #A simple method to obtain a list of users
LOCAL_BACKUPS_DIR="/backup/"; #Universal path to local backups VestaCP
LOG="/var/log/$RUN_SCRIPT.log"; #Log file the script
BACKUP_COUNT=21; #The default value for the number of backups
LOG_MAX_ROWS_COUNT=200000; #The maximum number of rows in a log file. In excess of the log is cleared. 200000 lines ~ 50 MiB
#---------------------------------------------------------#
# Reading options #
#---------------------------------------------------------#
#Backup count
if(echo "$1" | grep -E -q "^?[0-9]+$"); then BACKUP_COUNT=${1}; fi;
#The maximum number of rows in a log file
if(echo "$2" | grep -E -q "^?[0-9]+$"); then LOG_MAX_ROWS_COUNT=${2}; fi;
#---------------------------------------------------------#
#Checking the number of log file lines
if [ $(cat ${LOG} | wc -l) -gt ${LOG_MAX_ROWS_COUNT} ]; then echo "[$(date +%F" "%T" "%z)] -- Log file cleared." > ${LOG}; fi;
echo " " >> ${LOG};
echo " " >> ${LOG};
echo "#------------------------#-------------------------#------------------------#" >> ${LOG};
echo "#------------------------|$(date +%F" "%T" "%z)|------------------------#" >> ${LOG};
echo "#------------------------#-------------------------#------------------------#" >> ${LOG};
echo "[$(date +%F" "%T" "%z)] -- Start script: #$RUN_SCRIPT# backups upload to mega." >> ${LOG};
echo "[$(date +%F" "%T" "%z)] -- Run user: $RUN_USER - Params: $@" >> ${LOG};
echo "[$(date +%F" "%T" "%z)] -- Print users list: "${VESTA_USERS} >> ${LOG};
echo "[$(date +%F" "%T" "%z)] -- Count backups upload: $BACKUP_COUNT" >> ${LOG};
echo "[$(date +%F" "%T" "%z)] -- Maximum number of rows in a log file: $LOG_MAX_ROWS_COUNT" >> ${LOG};
#Create base backups directory in the MEGA cloud
[ -z "$(megals --reload /Root/${CLOUD_BACKUPS_DIR})" ] && megamkdir /Root/${CLOUD_BACKUPS_DIR} >> ${LOG} 2>> ${LOG}
echo "[$(date +%F" "%T" "%z)]" >> ${LOG};
#Upload backups
for USER in ${VESTA_USERS}; do
echo "[$(date +%F" "%T" "%z)] -- User log block: $USER" >> ${LOG};
#Remove old backups
echo "[$(date +%F" "%T" "%z)] -- Start remove $USER cloud backups:" >> ${LOG};
while [ $(megals --reload /Root/${CLOUD_BACKUPS_DIR} | grep -E "/Root/${CLOUD_BACKUPS_DIR}/${USER}.*" | wc -l) -gt ${BACKUP_COUNT} ]
do
TO_REMOVE=$(megals --reload /Root/${CLOUD_BACKUPS_DIR} | grep -E "/Root/${CLOUD_BACKUPS_DIR}/${USER}.*" | sort | head -n 1)
echo "[$(date +%F" "%T" "%z)] -- Remove file: mega:$TO_REMOVE" >> ${LOG};
megarm ${TO_REMOVE} >> ${LOG} 2>> ${LOG}
done
echo "[$(date +%F" "%T" "%z)] -- Stop remove $USER cloud backups" >> ${LOG};
echo "[$(date +%F" "%T" "%z)] -- Start upload $USER backups:" >> ${LOG};
FILES=$(/usr/bin/find ${LOCAL_BACKUPS_DIR} -type f -name "$USER.*");
for FILE in ${FILES}; do
FILENAME=${FILE##*/}
echo "[$(date +%F" "%T" "%z)] -- Upload: $FILE" >> ${LOG};
megaput --no-progress --reload --path "/Root/${CLOUD_BACKUPS_DIR}/${FILENAME}" "${FILE}" >> ${LOG} 2>> ${LOG}
done
echo "[$(date +%F" "%T" "%z)] -- Stop upload $USER backups" >> ${LOG};
echo "[$(date +%F" "%T" "%z)]" >> ${LOG};
done
#Remove VestaCP TMP
echo "[$(date +%F" "%T" "%z)] -- Remove VestaCP TMP" >> ${LOG};
rm -rf "${LOCAL_BACKUPS_DIR}/tmp.*/*" >> ${LOG} 2>&1
rm -rf "${LOCAL_BACKUPS_DIR}/tmp.*" >> ${LOG} 2>&1
echo "#---------------------------------------------------------------------------#" >> ${LOG};
echo " " >> ${LOG};
echo " " >> ${LOG};
exit