Backup to Yandex disk

I will give an example of a script for creating backup copies to Yandex drive.

On the test, I use the Ubuntu operating system and execute commands from the root user through sudo.

Curl must be installed on the system or install it with the command:

sudo apt-get install curl

First, create an empty file for the script:

sudo touch

We’ll specify the minimum access rights to the file so that other users can’t open it, because the Yandex password will be stored in the file:

sudo chmod 0700

Open the file in some kind of text editor, for example nano (Ctrl+X to exit, y/n to save or cancel changes):

sudo nano

And add the following contents to the file (if you work through putty, you can paste the contents of the clipboard by right-clicking):

# We assign the path to the files to variables so that in the future it will be easier to specify them several times
$FILE1=/tmp/etc_`date +%Y-%b`.tar.gz
$FILE2=/tmp/sql_`date +%Y-%b`.sql.gz
# For example, we first archive the /etc directory in the archive
tar -czf $FILE1 /etc
# Let's dump all mysql databases
mysqldump -u USERNAME --password=PASSWORD --all-databases | gzip > $FILE2
# Now send these two files to the Yandex drive (specify the files separated by commas)
curl --user USER:PASSWORD -T "{$FILE1,$FILE2}"
# Now delete them, as they are already on Yandex drive
unlink $FILE1
unlink $FILE2

You can also set an archive password when archiving, see my more detailed articles on this topic:
RAR and ZIP archiving on Linux
RAR архивы в Linux Ubuntu

The script can be added to the Cron scheduler to run automatically – Using and configuring CRON

Leave a comment

Leave a Reply