VM backup and download

https://unsplash.com/@frank041985

The following code snippet will let you create a backup image of a Linux server, encrypt it, and then make it available for download. It requires PHP, and makes use of PHP's built in HTTP server. You will need sufficient disk space on your VM image to create the download. The file is compressed, so the backup will typically be smaller than the disk space used on the VM.

wget -O /root/server-to-image.sh 'https://raw.githubusercontent.com/pbkwee/s2i/main/server-to-image.sh'
bash /root/server-to-image.sh --help
 Usage: /root/server-to-image.sh --encrypt openssl (default if using --http) | zip (not so secure) | none (default if not using --http)
    --http (serve file on an http url)
    --files (default to / )
We recommend you stop database servers and other processes that may be updating files while you run this script.
  
  If you use the --http option we will put the file on a URL that should be secret.  However we still recommend you use one of the --encrypt options.
# sample output
bash /root/server-to-image.sh
Not using http, not doing backup file encryption
Creating tar file, not encrypted, at /root/backup.s2i/backup-2021-11-24-1637721988.tar.gz
tar: Removing leading `/' from member names
You can access your backup file via scp:
mkdir restore.2021-11-24-1637721988 && cd restore.2021-11-24-1637721988 scp root@$ip:/root/backup.s2i/backup-2021-11-24-1637721988.tar.gz /dev/stdout | tar  --extract --gunzip --numeric-owner --preserve-permissions

The script will create the backup file and let you scp away (encryption is optional here). It also provides an option to let you download that via a URL (in that case it would select an encryption option by default).

If you use the openssl encryption option you will need openssl. This is typically available on most Linux installs. There are also Windows binaries available, e.g. from https://curl.se/windows/

To ensure database consistency we recommend you stop the database server while the files are being copied. Alternatively you could create a database dump prior to creating the backup image.

By Peter Bryant

Peter founded RimuHosting in 2003

Leave a comment