How to backup the flash and eeprom of a mega2560

I have a 3d printer, looks like a um2 clone, with a mega2560 in it. The maker is our of business. I am pretty sure about its firmware is some modification of Marlin firmware. I want to backup the current flash as well as the eeprom of the mega2560 and try a new version of marlin firmware. In case something bad happen I can still restore the whole system. I assume the maker didn't set the fuse to protect it from reading.

I have ordered a high voltage avr programmer board (similar to stk500), can this do the work? So just use the avrdude?

Do I have to have access to its icsp or I just need its usb/serial?

I'll share my instructions for reading the flash memory from a board, as well as for writing it back to a board in order to restore it. This can be done over the USB/serial connection, just as you would use when uploading a sketch normally.

I don't actually have experience with doing this with EEPROM. In theory, it appears you could use the same procedure, except with the adjustment of changing the -Uflash part of the avrdude commands to -Ueeprom, per the AVRDUDE instructions:
https://www.nongnu.org/avrdude/user-manual/avrdude_4.html#Option-Descriptions


Reading the program from the board into a file

First, we’re going to do a dummy upload in the Arduino IDE in order to get it to help us generate the avrdude command used to read the program from the Arduino board:

  1. Plug your Arduino board that contains the program you want to read into your computer.
  2. Select your board from the Arduino IDE’s Tools > Board menu.
  3. Select your board’s port from the Tools > Port menu.
  4. VERY IMPORTANT: Unplug your board from your computer.
  5. File > Preferences
  6. Check the box next to "Show verbose output during > upload".
  7. Click the OK button.
  8. Sketch > Upload
  9. Wait for the upload to fail.
  10. Scroll up the black console window at the bottom of the Arduino IDE window until you see the avrdude command that was generated for the upload. It will look something like this:
    E:\Arduino\hardware\tools\avr\avrdude -CE:\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM17 -b115200 -D -Uflash:w:C:\Users\per\AppData\Local\Temp\arduino_build_91864/sketch_jan22b.ino.hex:i
    
  11. Select the full text of the upload command.
  12. Press Ctrl+C. This will copy the upload command to the clipboard.
  13. Next, you need to modify the upload command to read the hex file out of your Arduino board:
  14. Start a text editor program.
  15. In the text editor window, press Ctrl+V. This will paste the command into the text editor.
  16. The end of the command will look something like this:
    -D -Uflash:w:C:\Users\per\AppData\Local\Temp\arduino_build_91864/sketch_jan22b.ino.hex:i
    
    That is the part of the command that tells it to write.
    Replace that part of the command with the command that tells AVRDUDE to read:
    -Uflash:r:readfile.hex:i
    
    That will cause the read file to be named "readfile.hex", which will be saved to whichever folder you run the command from. So now the full command looks something like this:
    E:\Arduino\hardware\tools\avr\avrdude -CE:\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM17 -b115200 -D -Uflash:r:readfile.hex:i
    
  17. If the paths in the command contain spaces, wrap the paths in quotes.
  18. Plug your Arduino board into your computer.
  19. Copy and paste the command from the text editor to the command line
  20. If you are using to an ATmega32U4-based board (e.g., Leonardo, Micro, Yun), you’ll need to press and release the reset button on the board immediately after running the command. If you are using a Pro Micro, use a wire to momentarily connect the RST and GND pins. If you are using a Yun, press and release the "32U4 RST" button quickly twice. Wait about a second after resetting the board before running the command.
  21. Run the command.
  22. Wait for the command to finish successfully.

Writing the file to a board

You can follow a similar procedure to write the file to another Arduino board. Remember that this file was compiled specifically for the Arduino board you read it from. You can’t use it with an Arduino board that has a different configuration. For example, if you read it from an Uno, it is compiled for an ATmega328P running at 16 MHz, and won't work correctly on a board with a different microcontroller or clock speed.

Generate model command

First, we’re going to do a dummy upload in the Arduino IDE in order to get it to help us generate the avrdude command used to write the program to the Arduino board.

Note: It doesn't matter whether the upload will succeed or fail. You get the command either way.

  1. Plug the Arduino board you want to write to into your computer.
  2. Select your board from the Arduino IDE’s Tools > Board menu.
  3. Select your board’s port from the Tools > Port menu.
  4. Select File > Preferences from the Arduino IDE's menus.
  5. Check the box next to "Show verbose output during: upload".
  6. Click the OK button.
  7. Select Sketch > Upload from the Arduino IDE's menus.
  8. Wait for the upload to finish/fail.
  9. Scroll up the black console window at the bottom of the Arduino IDE window until you see the avrdude command that was generated for the upload. It will look something like this:
    E:\Arduino\hardware\tools\avr\avrdude -CE:\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM17 -b115200 -D -Uflash:w:C:\Users\per\AppData\Local\Temp\arduino_build_91864/sketch_jan22b.ino.hex:i
    
  10. Select the full text of the upload command.
  11. Press Ctrl+C. This will copy the upload command to the clipboard.

Customize command

Next, you need to modify the upload command to write the hex file you read from your other Arduino board:

  1. Start a text editor program.
  2. In the text editor window, press Ctrl+V. This will paste the command into the text editor. The end of the command will look something like this:
    -D -Uflash:w:C:\Users\per\AppData\Local\Temp\arduino_build_91864/sketch_jan22b.ino.hex:i
    
    That is the part of the command that tells it to write.
  3. Replace the filename in that part of the command with the name of the file you read:
    -Uflash:w:readfile.hex:i
    
    So now the full command looks something like this:
    E:\Arduino\hardware\tools\avr\avrdude -CE:\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM17 -b115200 -D -Uflash:w:readfile.hex:i
    
    If the paths in the command contain spaces, wrap the paths in quotes.

Run command

Now the write command is all prepared. It's time to run it!

  1. Plug the Arduino board you want to write to into your computer.
  2. Select the full text of the write command in your text editor.
  3. Press Ctrl+C. This will copy the write command to the clipboard.
  4. Open a command line terminal. On Windows, you can use the Run dialog, PowerShell, or cmd.
  5. In the command line, press Ctrl+V. This will paste the avrdude command.
  6. If you are uploading to an ATmega32U4-based board (e.g., Leonardo, Arduino Micro), you’ll need to press and release the reset button on the board before running the command. If you are using a Pro Micro, use a wire to momentarily connect the RST and GND pins. If you are using a Yun, press and release the "32U4 RST" button quickly twice. Wait about one second before running the command.
  7. Press Enter to run the command.
  8. Wait for the writing process to finish successfully.

I use AVRDUDES - a GUI for AVRDUDE. My 3D printer is with ATmega2560 and has ICSP header. With Arduino Nano as ISP and AVRDUDES i read and write flash and EEPROM on it.

Could you just swap the Mega for another one and avoid the problem of having to make a backup? To ensure the backup is correct and restore-able you will need another Mega to test the backup on anyway. If you make a backup and have to test it on the original Mega - you could end up with a non-functioning Mega and a useless backup.