Backup site files on Yandex.Disk

Bash script for archiving and copying site backup files to Yandex.Disk.

Continuation of the series of articles on backing up to Yandex disk using the WebDAV protocol. There is an article on how to install webdav to your server.

Examples of backup scripts. Everything there is relevant and quite working.

In those scripts, the principle was as follows: Yandex.Disk was mounted via WebDAV and a copy was immediately created there.

In principle, everything should work. However, on a new hosting, I was faced with the fact that with a large archive somewhere the file is lost along the way.

In this article, the principle is slightly different: we create a copy on our server. Then we mount the disk and after that we send this copy to Yandex.Disk.

But that is not all. We leave the last file on our server. And the previous one is deleted. For this reason, create some empty file that will be the starting point of the removal.

Below is the entire bash script with detailed descriptions and explanations:

#! / bin / sh
#
# Backup sites and copy to Yandex.Disk

###################
# Create archive
###################

date=`date +“% Y_% m_% d_% H:% M”`
filename=‘backup-sites_’$ date‘.tar.gz’

server_path=‘/ home / admin /’

backups_dir=$ server_path‘backups /’

dir_for_backup=‘web’

# Create archive gzip
# c – creates a new file
# z – create gzip from .tar archive
# v – show the progress of creating the .tar file.
# f – tells the command that the next argument is the name of the archive file
# C – go to the directory
/usr/bin/tar -czvf $ backups_dir$ filename -C $ server_path $ dir_for_backup

###################
# Copy to Yandex
###################

# Mount Yandex.Disk
mount -t davfs https://webdav.yandex.ru /mnt/yandex/

# Path on Yandex.Disk
yandex_disk=‘/ mnt / yandex / backups / sites’

# Copy to yandex server
# t – sort by date modified, first at the beginning
# r – reverse order when sorting
# 1 – display one file per line
cp `ls -tr1 $ backups_dir/ * | tail1` $ yandex_disk

###################
# Clean
###################

# Remove oldest file in the backup directory on our server
rm -f `ls -t $ backups_dir* | tail1`

# Delete backup file older than 10 days on yandex server
/usr/bin/find $ yandex_disk -type f -mtime +10 -exec rm {} ;

# UnMount Yandex.Disk
umount /mnt/yandex

Similar Posts

Leave a Reply Cancel reply