Skip to content

Instantly share code, notes, and snippets.

@anzeljg
Last active January 5, 2023 03:21
Show Gist options
  • Save anzeljg/46fddc4eaa5ce05f285a56c442800777 to your computer and use it in GitHub Desktop.
Save anzeljg/46fddc4eaa5ce05f285a56c442800777 to your computer and use it in GitHub Desktop.
Moosh - Bulk Backup Restore Courses

Moosh - Bulk Backup Restore Courses

by Just Another Engineer (Source)

Given a file with content as below per line which can be created based on sudo -u www-data moosh course-list | awk -F "," '{ print $1 "," $3}':

"1234","XX - XXXX/XXX/XX"

This will perform a backup to /var/backups/moodle/(courseshortcode):

while read i; do \
echo $i |\
sed 's/\"//g' |\
sed 's/\//_/g' |\
awk -F ',' '{ print "time sudo -u www-data moosh course-backup -f " "'\''/var/backups/moodle/"$2".mbz'\'' " $1}';\
done < migrated_ok_id

Transfer backup files over:

rsync -ah --progress x@x.x.x:/var/backups/moodle/* /var/backups/moodle/.

Given a folder with backup files as below:

# ls -1 /var/backups/moodle
XXXX-X-XXX_XXXX_XX.mbz

Format filenames to course shortcodes including / removing .mbz:

ls -1 /var/backups/moodle | sed s/\_/\\//g | sed s/\.mbz$//g > /home/x/restore

Get a list of course IDs for the backup courses above based on course shortcodes:

sudo -u www-data moosh course-list | grep -f /home/x/restore | awk -F ',' '{ print $1 "," $3 }' | sed s/\"//g | sed s/\\//_/g | sed s/$/.mbz/g > /home/x/restore_clean

Based on the backup files above and corresponding course ids based on course shortcodes perform the restore:

while read i; do \
echo $i |\
awk -F ',' '{ print "time sudo -u www-data moosh course-restore -o " "'\''/var/backups/moodle/"$2"'\'' " $1}';\
done < restore_clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment