Using scp and rync to move files

FTP is usually the best choice for transferring files to a remote computer but sometimes for a single file, scp is an alternative. Use this command.
scp xertetoolkits_3.8.zip jimmy@redmouse.xyz:/home/jimmy/transfer
Once it is in place we can unzip it:
unzip xertetoolkits_3.8.zip
It will create a directory structure but the tree will be under ~/transfer. We need to move it while preserving the file structure. The easiest way is to use rsync with the r ( recursive function)
First:
sudo mkdir /var/www/html/xerte
rsync -r ~/transfer/ /var/www/html/xerte

The command copies all the files to the new directory.
Becoming familiar with these commands is very useful when working in Linux. Don’t worry about remembering the syntax – I often google and copy. What is important is knowing which command to use.

Using ssh to log in to your remote server

ssh is a quick way to log on and access the file system of your server. From the bash shell on WSL, I would use this command.


$ ssh jimmy@redmouse.xyz

The remote server will prompt you for the password. You can skip the password step by installing key-based SSH. ( Note:you will still be able to log on using a password on devices without a key pair).


jimmy@WSLBASH:~$ ssh-keygen -t rsa

Generating public/private rsa key pair.

Enter file in which to save the key (/home/jimmy/.ssh/id_rsa):

Enter – accept as default


Enter passphrase (empty for no passphrase):
Enter same passphrase again:

Enter – leave the passphrase as blank


Your identification has been saved in /home/jimmy/.ssh/id_rsa.
Your public key has been saved in /home/jimmy/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:j....lgOkE jimmy@WSLBASH
The key's randomart image is:
+---[RSA 2048]----+
| .=++..+ oE |
...................
| + =|
| = |
+----[SHA256]-----+
jimmy@WSLBASH:~$

Your output should look something like this.
Now you have a private and public key saved in ~/.ssh/
Copy the public key to your server.


ssh-copy-id -i ~/.ssh/id_rsa.pub yourserver.com

Now you should be able to log in using ssh without having to enter a password. No password means you can start using other useful bash commands such as scp and rsync.

Backing up Moodle

If you have students on your Moodle site, you have the responsibility to keep their work safe. Some time ago, while using a cheap VPS, I found my site had simply disappeared. The server was never seen again. Luckily I had course backups manually created and downloaded to my desktop. There were a couple of days old so an embarrassing situation but not critical.

So how should you back up? Automatically, at least daily and with a copy stored in an offsite location. Here is how I do it:

First an automatic course back up. I set this up for around midnight, a time when mos students are finished. The backup creates a .mbz file can be used to restore a course on a different Moodle server. Set this up in:

Site administration -> Courses -> Backups -> Automated backup setup.

Set the schedule to daily, execute time can be left to default ( midnight), and specify the directory to /home/user/backup. You may have to first create this folder manually in Linux.


cd ~
mkdir backup

(The command cd ~ is a shortcut to your home directory ie /home/jimmy/)

Next step is a database dump. You can use the instructions from Moodle docs but there are a few problems with their instructions.


cd /my/backup/directory
mv moodle-database.sql.gz moodle-database-old.sql.gz
mysqldump -h example.com -u myusername --password=mypassword -C -Q -e --create-options mydatabasename > moodle-database.sql
gzip moodle-database.sql

First, the password is exposed in the script, not the best for security. Second, the database backups only go back one day. Finally, the backups are stored on the same server Moodle is running on so failure on the server could wipe Moodle and the backups. I want to suggest a better way – storing up to a week of database dumps, a daily back up offsite, and a daily delete of old files. Here is how to do it.

Copy Moodle back ups off site

In my previous post, I showed how to back up the Moodle database and courses backed up automatically every night to the server running Moodle. All good but what if you lose access to the server? It happens.

You need to move a copy of your backup to a different location. It needs to be automatic and daily as you never know when it may go down.

You can use rsync, sshpass and rsa keys to sync to another Linux machine. The problem is you need another Linux box which is running at backup time. I used an always running obsolete Linux netbook for this and you could also use another VPS at a different location, But there is a better way.

Dropbox gives you 2 Gb of storage. It is free and always available, So how do you automatically back up to Dropbox?

My instructions for this come from Linux Additive Tips A shell script called dropbox_uploader.sh is used to create a crontab entry which will load your files to a dropbox account. Back up your database dump, course backups. moodledata and moodle code every night and know even if your Production server disappears completely, you can rebuild.

Just one last thing. There is really no need to keep all your backups and even a 2Gb Dropbox is going to fill up. Add this script to your database dump script to remove backups older than 3 days:

find /home/user/backup/ -type f -name '*.mbz' -mtime +3 -exec rm {} \;