Script to backing up apps to local server
-
Recently, we had to ensure our cloud services had a local backup for compliance reasons.
I wrote a script for our local server to connect to our Elestio instance via ssh, and extract files and database info from the app.
Notes:
- The database extraction only applies if you're using a database
- Your app's specific files are likely mapped to the /opt/app/app_name folder on your local instance, which is what I back up. IF THEY ARE NOT MAPPED, I.E. ONLY IN A DOCKER VOLUME, THIS WILL NOT BACK THEM UP.
- This will back up sensitive info, like your
.env
file which could include passwords, API keys, etc. Ensure your backups are properly secured. - If you don't already have it in place, you'll need to create an SSH keypair on the computer this will run on, and install it locally and on your Elest.io server.
Caveats aside, I'm sharing this here in case it's helpful to anyone. You'll likely need to modify it a bit for your environment.
app=your-app-name-here today=$(date "+%Y-%m-%-d") base="/path/to/root/folder/of/backups" backupFolder="$app/$today" filepath="$base/$backupFolder" mkdir "$filepath" echo "Root path for backup operations: $base" echo "Backing up to folder: $backupFolder" echo "-------------============= Export operations begin ============--------------" db_container=name_of_mysql_docker_container db_user=database_username_here db_password=database_password_here db=database_to_backup echo "Backing up $app to $filepath" # backing up database from container ssh -T root@your-elestio-url "docker exec $db_container /usr/bin/mysqldump -u $db_user -p$db_password --single-transaction $db" > "$filepath/$app_db-$today.backup.sql" # back up data present in app folder ssh -T root@your-elestio-url "tar -czvf - -C /opt/app/$app ." > "$filepath/$app_files-$today.backup.tar.gz" echo "-------------============= Export operations finished ============--------------"