-
Notifications
You must be signed in to change notification settings - Fork 17
/
nextcloud-rsync-to-remote.sh
117 lines (82 loc) · 3.67 KB
/
nextcloud-rsync-to-remote.sh
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
#!/bin/bash
# By Georgiy Sitnikov.
#
# Will do NC backup and it upload to remote server via SSH with key authentication
#
# AS-IS without any warranty
SSHIdentityFile=/path/to/file/.ssh/id_rsa
SSHUser=user
RemoteAddr=IP_or_host
RemoteBackupFolder=/path/to/backup
NextCloudPath=/var/www/nextcloud
# Folder and files to be excluded from backup.
# - data/updater* exclude updater backups and dowloads
# - *.ocTransferId*.part exclude partly uploaded files
#
# This is reasonable "must have", everything below is just to save place:
#
# - data/appdata*/preview exclude Previews - they could be newle generated
# - data/*/files_trashbin/ exclude users trashbins
# - data/*/files_versions/ exclude users files Versions
excludeFromBackup="--exclude=data/updater*\
--exclude=*.ocTransferId*.part\
--exclude=data/appdata*/preview"
# Compress to needs to have archivemount and sshfs installed.
CompressToArchive=false
WhereToMount=/mnt/remoteSystem # Needs to be set if CompressToArchive is true
RemoteArchiveName=backup.tar.gz # Needs to be set if CompressToArchive is true
##############################################################################
InstallerCheck () {
# Check if all Programms are installed
hash $1 2>/dev/null || { echo >&2 "It requires $1 but it's not installed. Aborting."; exit 1; }
}
# Check if config.php exist
[[ -e $NextCloudPath/config/config.php ]] || { echo >&2 "Error - config.php could not be found under "$NextCloudPath"/config/config.php. Please check the path"; exit 1; }
# Fetch data directory place from the config file
DataDirectory=$(grep datadirectory $NextCloudPath/config/config.php | cut -d "'" -f4)
RsyncOptions="-a --partial --info=progress2 --no-o --no-g --delete"
#RsyncOptions="-aP --info=progress2 --no-o --no-g --delete"
InstallerCheck rsync
if [ "$CompressToArchive" == true ]; then
InstallerCheck sshfs
InstallerCheck archivemount
RsyncOptions="-aP --delete"
mkdir -p $WhereToMount;
mkdir -p $WhereToMount_$(date);
echo "Mount remote system"
sshfs -o allow_other,default_permissions,IdentityFile=$SSHIdentityFile $SSHUser@$RemoteAddr:$RemoteBackupFolder:$RemoteBackupFolder $WhereToMount
sleep 2
if [ -f "$WhereToMount/$RemoteArchiveName" ]; then
echo "Mount remote Archive"
archivemount $WhereToMount/$RemoteArchiveName $WhereToMount_$(date)
echo "Rsync of NC into Archive"
rsync $RsyncOptions $excludeFromBackup $NextCloudPath $WhereToMount_$(date)
echo "Wait to finish sync"
sleep 5
echo "Unmount Archive"
umount $WhereToMount_$(date)
else
InstallerCheck tar
echo "Put NC into Archive"
echo "Will create Archive under $WhereToMount"
echo "With name nextcloudBackup-$(date +"%Y-%m-%d_%T")_$(md5sum <<< $(ip route get 8.8.8.8 | awk '{print $NF; exit}')$(hostname) | cut -c1-5 ).tar.gz"
tar -cvpf $excludeFromBackup --one-file-system $NextCloudPath $WhereToMount/nextcloudBackup-$(date +"%Y-%m-%d_%T")_$(md5sum <<< $(ip route get 8.8.8.8 | awk '{print $NF; exit}')$(hostname) | cut -c1-5 ).tar.gz
fi
echo "Wait to finish sync"
sleep 5
echo "Unmount Remote FS"
umount $WhereToMount
else
echo "Run Rsync of NC root folder."
echo "From Source: $NextCloudPath"
echo "To: Destination: $RemoteAddr"
echo " Destination Folder: $RemoteBackupFolder"
rsync $RsyncOptions --exclude=data --exclude=$DataDirectory -e "ssh -i $SSHIdentityFile" $NextCloudPath $SSHUser@$RemoteAddr:$RemoteBackupFolder
echo "Run Rsync of NC Data folder (found under $DataDirectory)."
echo "From Source: $DataDirectory"
echo "To: Destination: $RemoteAddr"
echo " Destination Folder: $RemoteBackupFolder"
rsync $RsyncOptions $excludeFromBackup -e "ssh -i $SSHIdentityFile" $DataDirectory $SSHUser@$RemoteAddr:$RemoteBackupFolder
fi
echo Ready.
exit 0